Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Socket Programming over the Internet

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Location
    Negombo, Sri Lanka
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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:
    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);}
    	}
    }
    Last edited by djmaxtor; July 24th, 2012 at 02:45 AM.


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Location
    Negombo, Sri Lanka
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Socket Programming over the Internet

    Quote Originally Posted by Laura2 View Post
    I wrote a simple chat program which perfectly runs on the local host.
    What did you mean my friend..?

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Socket Programming over the Internet

    Never mind. That was a spammer. Using AI stuff to copy posts, and it copied your first line, and post semi-relevant stuff, along with possibly links or stuff. That user has been banned.

    Anyway, I wasn't aware that ports could be negative.

    It looked like you said port -9999.

    Perhaps the app isn't granted admin permission on that network.

    Your server is the one online, right? Perhaps a possibility is that it's looking for port -9999 or 9999 on its own network, not yours.
    Last edited by javapenguin; June 24th, 2012 at 03:18 PM.

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Location
    Negombo, Sri Lanka
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Re: Socket Programming over the Internet

    Quote Originally Posted by javapenguin View Post
    Never mind. That was a spammer. Using AI stuff to copy posts, and it copied your first line, and post semi-relevant stuff, along with possibly links or stuff. That user has been banned.

    Anyway, I wasn't aware that ports could be negative.

    It looked like you said port -9999.

    Perhaps the app isn't granted admin permission on that network.

    Your server is the one online, right? Perhaps a possibility is that it's looking for port -9999 or 9999 on its own network, not yours.
    I think you have misunderstood that I had used -9999 (a negative port). As you said I too wasn't aware that a port could be negative.
    As shown in the source code I have used port 9999 for my chat application.

    According to my app, the server is the one who remains online and listening to the port 9999. When a client tries to connect to the specific public IP of the server through port 9999, the server accepts the connection and grants the particular service to the client.

    After configuring Port-Forwarding this website indicates that port 9999 remains open for the specific public IP where the server listens to the specific port. Although the port remains open, the client cannot connect to the server.

    In such a situation how can I create a connections among clients and the server..?

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Socket Programming over the Internet

    djmaxtor
    I suppose this IP "112.134.190.41" is the given IP of your provider. What I see in your client program is a "hardwired" code s=new Socket("112.134.190.41",9999);. 2 Questions:
    Is this IP static or dynamic?
    How you configure your router (i.e. gateway to the web) and your local computer?

    By the way, it's better to code Socket s=ss.accept(); than Socket s; ....s=ss.accept(); Reason is that your server can accept MORE incoming requests and only 1 is available and is probably still occupied...

  6. #6
    Junior Member
    Join Date
    Jun 2012
    Location
    Negombo, Sri Lanka
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Socket Programming over the Internet

    Quote Originally Posted by Voodoo View Post
    djmaxtor
    I suppose this IP "112.134.190.41" is the given IP of your provider. What I see in your client program is a "hardwired" code s=new Socket("112.134.190.41",9999);. 2 Questions:
    Is this IP static or dynamic?
    How you configure your router (i.e. gateway to the web) and your local computer?

    By the way, it's better to code Socket s=ss.accept(); than Socket s; ....s=ss.accept(); Reason is that your server can accept MORE incoming requests and only 1 is available and is probably still occupied...
    Thank you so much for your reply my friend,
    The given IP is an Dynamic IP. Since I'm not good in Networking, as I know the assigned IP address remain unchanged until I restart my router. So I just need to check whether I can connect two or more socket applications over the internet.

    When configuring my router I followed the following procedure
    Login to my router configuration page--> Advanced Setup-->NAT-->Virtual Server Setup--> Then entered the information as follows

    Then I added an inbound and outbound rule for port 9999 in windows firewall.

    Can you please help me to sort out this problem? So that I can continue my project work without any doubts.
    Thanks in advance

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Socket Programming over the Internet

    djmaxtor,
    it's a very complicated matter. It depends fully on your Router. Because I don't know your Router so recommend you to look at the manuals or ask its tech.support. Normally you have to forward the port and it depends on the feature of your router. Here is the Port Forwarding Guides Listed by Manufacturer and Model - PortForward.com
    Your codes are fine. Now it depends only on the Port-Forwarding. Good luck!

Similar Threads

  1. Socket Programming and GUI
    By oyunmax in forum Java Networking
    Replies: 3
    Last Post: June 15th, 2011, 03:01 PM
  2. socket programming
    By advjava in forum Member Introductions
    Replies: 3
    Last Post: April 5th, 2011, 11:21 PM
  3. [SOLVED] Problem in socket programming
    By sinni in forum Java Networking
    Replies: 1
    Last Post: March 15th, 2011, 10:26 AM
  4. [SOLVED] Socket Programming Problem
    By relixus in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 9th, 2010, 09:35 PM
  5. Java socket programming
    By lucy in forum Java Networking
    Replies: 1
    Last Post: April 26th, 2010, 09:13 PM