comunicazione tramite protocollo udp

Ciao a tutti.
Il mio problema riguarda la comunicazione tramite protocollo udp tra Arduino e il mio computer(tramite programma in Java). Il punto è che riesco ad inviare dati da pc-->Arduino ma non il contrario Arduino-->pc. Ho guardato tanto in giro ma non sono riuscito a trovare niente che mi potesse soddisfare.
Lascio i codici se mi potete aiutare.

PROGRAMMA ARDUINO

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 2, 140);

unsigned int localPort = 8888; // local port to listen on

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back

EthernetUDP Udp;

void setup() {

Ethernet.begin(mac, ip);

Serial.begin(9600);

Udp.begin(localPort);
}

void loop() {

int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i=0; i < 4; i++) {
Serial.print(remote*, DEC);*

  • if (i < 3) {*

  • Serial.print(".");*

  • }*

  • }*

  • Serial.print(", port ");*

  • Serial.println(Udp.remotePort());*

  • Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);*

  • Serial.println("Contents:");*

  • Serial.println(packetBuffer);*

  • delay(2000);*

  • char ReplyBuffer[] = "a";*

  • Udp.beginPacket(Udp.remoteIP(), localPort);*

  • Udp.write(ReplyBuffer);*

  • Udp.endPacket();*

  • }*

  • delay(10);*
    }
    --------------------------------------------------------------------------------------
    PROGRAMMA JAVA
    import java.io.*;
    import java.net.*;
    public class QuoteClient
    {

  • public static void main(String[] args) {*

  • String hostname = "192.168.2.140";*

  • int port = 8888;*

  • try {*

  • InetAddress address = InetAddress.getByName(hostname);*

  • DatagramSocket socket = new DatagramSocket(port);*

  • while (true) {*

  • byte[] buf = null;*

  • String inp = "ciao";*

  • buf = inp.getBytes();*

  • System.out.println(buf);*

  • String ad =""+address.getAddress();*

  • DatagramPacket request = new DatagramPacket(buf, 4, address, port);*

  • socket.send(request);*

  • //DatagramSocket serverSocket = new DatagramSocket(port);*

  • byte[] buffer = new byte[8];*

  • System.out.println("1"+ad);*

  • DatagramPacket response = new DatagramPacket(buffer, buffer.length);*

  • System.out.println("2 ");*

  • socket.receive(response);*

  • System.out.println("3 ");*

  • String sentence = new String( response.getData());*

  • System.out.println("RECEIVED: " + sentence);*

  • System.out.println("2 "+response);*

  • socket.receive(response);*

  • System.out.println("3");*

  • String quote = new String(buffer, 0, response.getLength());*

  • System.out.println(quote);*

  • System.out.println(response);*

  • System.out.println();*

  • //Thread.sleep(10000);*

  • }*

  • } catch (SocketTimeoutException ex) {*

  • System.out.println("Timeout error: " + ex.getMessage());*

  • ex.printStackTrace();*

  • } catch (IOException ex) {*

  • System.out.println("Client error: " + ex.getMessage());*

  • ex.printStackTrace();*

  • }*
    _ /*catch (InterruptedException ex) {_

  • ex.printStackTrace();*
    _ }*/_

  • }*
    }
    Ringrazio quelli che riusciranno a darmi una mano

>marcobotte: in conformità al REGOLAMENTO della sezione Italiana del Forum, punto 13, il cross-posting è proibito (anche tra lingue diverse) e tu hai già posto la stessa domanda QUI.

Poi, NON avendolo tu ancora fatto, ti chiedo cortesemente di presentarti IN QUESTO THREAD (spiegando bene quali conoscenze hai di elettronica e di programmazione ... possibilmente evitando di scrivere solo una riga di saluto :wink:) e di leggere con MOLTA attenzione tutto il REGOLAMENTO.

Per il futuro ricorda che, tutto il codice, DEVE trovarsi racchiuso tra due tag: [code] _il _tuo_ codice_ [/code] così da non venire interpretato e non dare adito alla formazione di caratteri indesiderati o cattiva formattazione del testo (regolamento, punto 7).

Infine, questo tuo thread duplicato viene chiuso e quindi cancellato. Ti prego pertanto di continuare dove avevi cominciato. Grazie.

Guglielmo