Java Datagram Sockets not receiving packets -


i'm attempting use java datagrams create packet stream between server , client. problem that, although receive confirmation packets being sent, lost before reach client listener set up. have right there's timeout after 5 seconds, happens every time run it.

class dgserver extends thread {     private datagramsocket server;      public dgserver() throws ioexception     {         server = new datagramsocket();     }      public void run()     {         try         {             server.connect(app.local, 4200);             system.out.println("server starting...");              int = 0;              while (server.isconnected() && (i < 256))             {                 byte[] buffer = new byte[1];                 buffer[0] = (byte) ++i;                 datagrampacket packet = new datagrampacket(buffer, buffer.length, app.local, 4200);                 system.out.println("sedning " + + " client...");                  server.send(packet);                  thread.sleep(500);             }         }         catch (exception e)         {             e.printstacktrace();         }         system.out.println("server finished!");          if (! server.isclosed())             server.close();     }  }  class dgclient extends thread {     private datagramsocket client;      public dgclient() throws socketexception     {         client = new datagramsocket();     }      public void run()     {         try         {             client.connect(app.local, 4200);             client.setsotimeout(5000);             system.out.println("client starting...");              int = 0;              while (client.isconnected() && (i < 256))             {                 byte[] buffer = new byte[1];                 datagrampacket packet;                 packet = new datagrampacket(buffer, 1, app.local, 4200);                 //system.out.println("sedning " + + " server...");                  client.receive(packet);                  buffer = packet.getdata();                           system.out.println("client received:\t" + packet.getdata()[0]);                  thread.sleep(500);             }         }         catch (exception e)         {             e.printstacktrace();         }         system.out.println("client finished!");          if (! client.isclosed())             client.close();     }  } 

you may choose skim on second class. they're same, replaces server.send, client.receive. also, class not designed important. so, lot of code(like, exception handling), written simplistically.

is there can prevent loss of packets? have port forwarded on computer(not should matter, i'm using localhost, app.local in case wondered).

also, side question. had set single class, coded send packet, turn around , receive one. threw exception because 'icmp port unreachable'. know why happens?

ok first off, think testing both server , client @ same time, don't have idea whether 1 fails.

you should use either netcat (nc) or wireshark test client

with netcat, can run following command

nc -l -u -p 4200 -vv 

this tell netcat listen (-l) on udp (-u) on port (-p 4200) , verbose (-vv)

this way you'll able check if client can connect anything.

you can use same program check if server can receive connections known working program

nc -u [target ip] 4200 

there netcat cheatsheet here

you can check netcat netcat diagnose if purely network issue. maybe firewalls/nat aren't configured correctly


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -