swing - Java UDP packets not reaching Server program from Client program -


i have been trying learn networking in java , have begun making simple client/server program simple gui. server has jtextarea , client has jtextfield, jtextarea , jbutton. way meant work user on client side types message in jtextfield , clicks jbutton(send). invokes senddata method takes message client typed, puts in datagrampacket , sends packet server on predefined port. server on startup creates thread loops continuously allowing passively listen packets using datagramsocket.recieve(datagrampacket) method. if packet recived, updates jtextarea of gui data in packet. however, seems though packets not reaching server. have tested on lan network , working. however, when used on internet packets seem getting lost. have tried 2 different people have had ports forwarded , sure forwarded. client , server both consist of 3 simple classes. appreciated. sorry long description , code dump. note: using udp because want learn , know tcp can better things chat.

client class of client:

import java.io.ioexception; import java.net.datagrampacket; import java.net.datagramsocket; import java.net.inetaddress; import java.net.socketexception;   public class client {  datagramsocket socket; panel panel;  public client(panel panel) {     this.panel = panel;     try {         socket = new datagramsocket();     } catch (socketexception e) {         e.printstacktrace();         panel.textarea.settext("socket not created.");          }         }     public void senddata(byte[] data, inetaddress ipaddress){     datagrampacket packet = new datagrampacket(data, data.length, ipaddress,     27015);     try {         socket.send(packet);         panel.textarea.settext("client: package being sent server...");     } catch (ioexception e) {         e.printstacktrace();         panel.textarea.settext("package not sent");     }  }     } 

panel class of client:

import java.awt.dimension; import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.net.inetaddress; import java.net.unknownhostexception;  import javax.swing.jbutton; import javax.swing.jpanel; import javax.swing.jtextarea; import javax.swing.jtextfield;    public class panel extends jpanel implements actionlistener {  jbutton send; jtextfield textfield; jtextfield textarea; client client; inetaddress ipaddress;  public panel(){       this.setvisible(true);            send = new jbutton("send");     send.addactionlistener(this);     send.setvisible(true);      textfield = new jtextfield();     textfield.setvisible(true);     dimension dim = new dimension(300,20);     textfield.setpreferredsize(dim);      textarea = new jtextfield();     dimension dim2 = new dimension(300,100);     textarea.setpreferredsize(dim2);      this.add(textfield);     this.add(send);     this.add(textarea, flowlayout.left);      client = new client(this);    } public void actionperformed(actionevent e) {     if (e.getsource() == send){         system.out.println("send pressed");          string message = textfield.gettext();         try {             ipaddress = inetaddress.getbyname("localhost");         } catch (unknownhostexception e1) {             e1.printstacktrace();             textarea.settext("hostname not resolved");         }         client.senddata(message.getbytes(), ipaddress);         textarea.settext("client: " + message);     }  }    } 

serverthread class of server:

import java.io.ioexception; import java.net.datagrampacket; import java.net.datagramsocket; import java.net.inetaddress; import java.net.socketexception; import java.net.unknownhostexception;  import javax.swing.jpanel;   public class serverthread extends thread{  private datagramsocket socket; panel panel;  public serverthread(panel panel) {     this.panel = panel;     try {         socket = new datagramsocket(27015);         panel.settextarea("the server has begun listening on port 3659...");     } catch (socketexception e) {         e.printstacktrace();         panel.settextarea("server cant open socket , listen on port 3659");     } }  public void run(){     while(true){         byte[] data = new byte[1024];         datagrampacket packet = new datagrampacket(data, data.length);          try {             socket.receive(packet);         // waits packet arrive             panel.settextarea("server: package recieved client...");         } catch (ioexception e) {             e.printstacktrace();             panel.settextarea("socket cant recieve packets");         }          string message = new string(packet.getdata());         panel.settextarea("client: " + message);      }  }  } 

note: both server , client, main class makes new jframe , new instance of panel class , sets contents pane. did not include panel class server not include significant code.

thanks again.

sounds network security configurations prevents receiving udp packets. need configure network such accepts udp packets on specified ports. also, have configure firewall accept udp packets on specified ports well.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -