java - DatagramSocket Packets IPAddress being changed by router? -


i have created server run on desktop send , receive messages clients. clients app run on android. of now, both programs start up, , client able send initial connection server. desktop connected internet via router, have port-forwarded router receive info.

the server able receive messages sent client. however, server unable respond. did quick check on received packet (server side), , says ipaddress packet ip router. mean unable send response, since of packets ip changed local address of router (because of port-forwarding)?

ill post code below, although im not sure help. comments.

server:

public class quoteserverthread extends thread {  protected datagramsocket socket = null; datagrampacket dgp = null; protected boolean running = true; arraylist<connecteduser> users = new arraylist<>(); string state; int port; int userid;  public quoteserverthread(string name, string state) throws ioexception {     super(name);     port = 4445;     userid = 1;     socket = new datagramsocket(port);     this.state = state;     this.state = "listening";     system.out.println("server socket listening on port: " + port); }  public void run() {     while (running) {         try {             byte[] buf = new byte[256];              // receive request             final datagrampacket packet = new datagrampacket(buf, buf.length);             socket.receive(packet);             if(packet.getdata() == null){                 system.out.println(packet.getdata());                 return;             }             else{                 system.out.println(packet.getdata());             }             dgp = packet;             string message = new string(packet.getdata(), 0, packet.getlength());              if(state.equals("listening")) {                 if(message.contains("newuser")){                     connecteduser newuser = new connecteduser(userid, socket);                     users.add(newuser);                     system.out.println("connected users: " + users.size());                      string msg = "id/" + userid;                      inetaddress ipaddress = packet.getaddress();                     int _port = packet.getport();                     system.out.println(ipaddress + "," + _port);                     datagrampacket out = new datagrampacket(msg.getbytes(), msg.length(), ipaddress, _port);                     socket.send(out);                      userid++;                 }                 else if(message.contains("id/")){                     string receivedmessage[] = message.split("/");                      system.out.println(receivedmessage[0] + ", " + receivedmessage[1] + ", " + receivedmessage[2]);                 }                 else{                     system.out.println(message);                 }             }          } catch (ioexception e) {             e.printstacktrace();             running = false;         }     }     socket.close(); } 

}

client:

public class quoteserverthread extends thread {  protected datagramsocket socket = null; protected boolean running = true; textview chatwindow; textview notification; edittext chatbox; mainactivity ma; scrollview sv;  public quoteserverthread(mainactivity ma, textview chatwindow, edittext chatbox, scrollview sv, textview notification) throws ioexception {     this("quoteserverthread");     this.ma = ma;     this.chatwindow = chatwindow;     this.sv = sv;     this.chatbox = chatbox;     this.notification = notification; }  public quoteserverthread(string name) throws ioexception {     super(name);     socket = new datagramsocket(); }  public void run() {     while (running) {         try {             byte[] buf = new byte[256];              // receive request             final datagrampacket packet = new datagrampacket(buf, buf.length);             socket.receive(packet);              if(packet.getdata() == null){                 system.out.println("data null!");                 return;             }              string message = new string(packet.getdata(), 0, packet.getlength());              final string notif = message;             notification.post(new runnable() {                 public void run() {                     notification.settext("server " + "__msg: " + notif);                 }             });               if(ma.state.equals("connected")) {                 final string chattext = chatwindow.gettext().tostring() + "\n" + "sender: " + message;                 chatwindow.post(new runnable() {                     public void run() {                         chatwindow.settext(chattext);                         sv.post(new runnable() {                             public void run() {                                 sv.fullscroll(view.focus_down);                                 chatbox.post(new runnable() {                                     public void run() {                                         chatbox.requestfocus();                                     }                                 });                             }                         });                     }                 });             }             else if(ma.state.equals("waitingconnection")){                 string msgsplit[];                 if(message.contains("id/")){                     msgsplit = message.split("/");                      final string id = msgsplit[1];                     ma.clientid = integer.getinteger(id);                       notification.post(new runnable() {                         public void run() {                             notification.settext("connected server " + "__msg: " + id);                             ma.state = "connected";                         }                     });                 }             }          } catch (ioexception e) {             e.printstacktrace();             running = false;         }     }     socket.close(); } 

}

the issue here client not receiving response. state problem again, believe port forwarding on router changing packets address local ipaddress (my phone sending mobile network). ideas on how can fix this?

i think seeing effects of network address translation (nat) , port-forwarding. viewpoint of android client communicating router. router instead of processing messages offloads processing server (port forwarding).

on server see destination address. since android client talking router, destination ip address see of router.

if want reply, should take source address of datagram , send reply address. when datagram passing router nat change source address of router.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -