CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2012
    Posts
    2

    Question Socket Programming over the Internet

    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:
    Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    class Server{
    	
    	ServerSocket ss;
    	Socket s;
    	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){
    				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:
    Code:
    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);}
    	}
    }

  2. #2
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: Socket Programming over the Internet

    Hi,

    If your server runs on 192.168.1.100, why your client tries to connect to 112.134.190.41?
    Code:
    s=new Socket("112.134.190.41",9999);
    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  3. #3
    Join Date
    Jun 2012
    Posts
    2

    Re: Socket Programming over the Internet

    Quote Originally Posted by AlbertGM View Post
    Hi,

    If your server runs on 192.168.1.100, why your client tries to connect to 112.134.190.41?
    Code:
    s=new Socket("112.134.190.41",9999);
    Albert.
    192.168.1.100 is the private address that the router has assigned to my desktop PC where the server runs. So 192.168.1.100 is possible as long as I try it on LAN.
    But if I need to connect to the server from a different network via the Internet, isn't it necessary to mention the public IP? That's why the client tries to connect to 112.134.190.41.

  4. #4
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: Socket Programming over the Internet

    Understood.
    I guess it's just a communication problem, which I'm not sure if I can answer.
    Nevertheless, as far as I know, the reason why you can't connect could be:
    • The internet route from from your request to your server may encounter a router (or a proxy) where port 9999 is not open. For instance, to access internet from my work, there are only a few of ports available (80, 443,...). I couldn't access from work to my own router at port 9999, despite my router would be open at that port, because I can't go through proxy using that port.
      In this case there is nothing you can do, because it depends on how routers/proxies are configured. You'll have to open other port in your router.
    • Your router is not able to redirect request to your server.
      In this case, you'll have to find how configure your own router.

    If neither of reasons are right, try to ask in a communications or network programming forum.

    I hope you understand what I mean, despite my english

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured