I wrote a simple chat program which perfectly runs on the local host. But, when the client tries to connect to the server via the Internet using the public IP and the port 9999, it does not work as it did on LAN.
As many people have suggested, I did Port-Forwarding as shown in the following figure.
After configuring Port-Forwarding this website indicates that port 9999 remains open for the specific public IP (I run the server on 192.168.1.100 on private network). Although the port remains open, the client cannot connect to the server. Then I disabled the Windows firewall and tried the same process. But the problem still persists.
Please can someone tell me how can I solve this problem..?
Thanks in advance
Sorry for my bad English
Here is the code of the Chat application..
Server:
import java.io.*; import java.net.*; import java.util.*; class Server{ ServerSocket ss; ArrayList al=new ArrayList(); Server(){ listen(9999); } public static void main(String[] args){ new Server(); } public void listen(int port){ try{ ss=new ServerSocket(port); while(true){ Socket s=ss.accept(); PrintWriter pw=new PrintWriter(s.getOutputStream(),true); al.add(pw); new ServerThread(this,s); } }catch(Exception e){System.out.println(e);} } public void sendToAll(String msg){ try{ synchronized(al){ for(int i=0;i<al.size();i++){ PrintWriter pw=(PrintWriter)al.get(i); pw.println(msg); pw.flush(); } } }catch(Exception e){System.out.println(e);} } } class ServerThread extends Thread{ Server server; Socket soc; ServerThread(Server server, Socket soc){ this.server=server; this.soc=soc; start(); } public void run(){ try{ BufferedReader br=new BufferedReader(new InputStreamReader(soc.getInputStream())); while(true){ String msg=br.readLine(); server.sendToAll(msg); } }catch(Exception e){System.out.println(e);} } }
Client:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.net.*; import java.io.*; class Client extends JFrame implements Runnable{ JPanel p1=new JPanel(new BorderLayout()); JTextField tf=new JTextField(); JTextArea ta=new JTextArea(); JLabel lbl1=new JLabel("Message"); JButton b1=new JButton("Send"); PrintWriter pw; BufferedReader br; Socket s; Client(){ p1.add("West",lbl1);p1.add("Center",tf);p1.add("East",b1); setLayout(new BorderLayout()); add("Center",ta);add("South",p1); try{ s=new Socket("112.134.190.41",9999); pw=new PrintWriter(s.getOutputStream(),true); br=new BufferedReader(new InputStreamReader(s.getInputStream())); }catch(Exception e){System.out.println(e);} b1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){send(tf.getText());}}); new Thread(this,s.toString()).start(); setSize(300,400); setVisible(true); } public void send(String msg){ try{ pw.println(msg); pw.flush(); }catch(Exception e){System.out.println(e);} } public static void main(String args[]){ new Client(); } public void run(){ try{ while(true){ ta.append(Thread.currentThread().toString()+br.readLine()+"\n"); } }catch(Exception e){System.out.println(e);} } }