Results 1 to 9 of 9
  1. #1
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Question ConnectException : Connection Refused

    I opened two instances of the program on my system and connected each other by 127.0.0.1 and the connection is successful.
    But when I tried it on two different computers on different networks the connection is refused.

    I don't know why the connection is refused.

    Here is the code.

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class test
    {
    	static Socket socket=new Socket();
    	static ServerSocket serversocket;
    	static ObjectOutputStream socketout = null;
    	static ObjectInputStream socketin = null;
    	
    	public static void main(String[] args) throws Exception
    	{
    		System.out.println("Select an option :");
    		System.out.println("1 ) Server");
    		System.out.println("2 ) Client");
    		BufferedReader r1=new BufferedReader(new InputStreamReader(System.in));
    		String s1=r1.readLine();
    		int i1=Integer.parseInt(s1);
    		switch(i1)
    		{
    			case 1:
    				server();
    				break;
    			case 2:
    				client();
    				break;
    			default:
    				System.out.println("Select a valid option");
    		}
    	}
    	
    	static void server()
    	{
    		try
    		{
    			serversocket=new ServerSocket(0);
    			serversocket.setSoTimeout(60000);
    			URL ip = null;
    			try
    			{
    				ip = new URL("http://api.externalip.net/ip/");
    			}
    			catch (MalformedURLException e)
    			{
    				e.printStackTrace();
    			}
    			BufferedReader in=new BufferedReader(new InputStreamReader(ip.openStream()));
    			String localip=in.readLine();
    			int p=serversocket.getLocalPort();
    			System.out.println(localip);
    			System.out.println(p);
    			socket=serversocket.accept();
    		}
    		catch(SocketException e)
    		{
    			e.printStackTrace();
    		}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    		try
    		{
    			socketout=new ObjectOutputStream(socket.getOutputStream());
    			socketout.flush();
    			socketin=new ObjectInputStream(socket.getInputStream());
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    		try
    		{
    			String str = null;
    			try
    			{
    				str = (String) socketin.readObject();
    			} 
    			catch (ClassNotFoundException e)
    			{
    				e.printStackTrace();
    			}
    			str=str.toUpperCase();
    			socketout.writeObject(str);
    			socketout.flush();
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    		finally
    		{
    			try
    			{
    				socketin.close();
    				socketout.close();
    			}
    			catch(Exception e)
    			{
    				e.printStackTrace();
    			}
    			
    		}
    	}
    	static void client()
    	{
    		InetAddress host=null;
    		String s=null;
    		BufferedReader r1=new BufferedReader(new InputStreamReader(System.in));
    		try
    		{
    			System.out.println("Enter IP : ");
    			try 
    			{
    				s=r1.readLine();
    			}
    			catch (IOException e) 
    			{
    				e.printStackTrace();
    			}
    			host=InetAddress.getByName(s);
    		}
    		catch(UnknownHostException e)
    		{
    			e.printStackTrace();
    		}
    		System.out.println("Enter port : ");
    		int port = 0;
    		try
    		{
    			port = Integer.parseInt(r1.readLine());
    		}
    		catch (NumberFormatException e1) 
    		{
    			e1.printStackTrace();
    		}
    		catch (IOException e1) 
    		{
    			e1.printStackTrace();
    		}
    		try
    		{
    			socket=new Socket(host,port);
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		try
    		{
    			socketin=new ObjectInputStream(socket.getInputStream());
    			socketout=new ObjectOutputStream(socket.getOutputStream());
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    			return;
    		}
    		String test="abcdefghijklmnopqrstuvwxyz";
    		try
    		{
    			socketout.writeObject(test);
    			socketout.flush();
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    		
    		String response = null;
    		try
    		{
    			try 
    			{
    				response=(String)socketin.readObject();
    			}
    			catch (ClassNotFoundException e)
    			{
    				e.printStackTrace();
    			}
    			System.out.println(response);
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    		finally
    		{
    			try
    			{
    				socketin.close();
    				socketout.close();
    			}
    			catch(Exception e)
    			{
    				e.printStackTrace();
    			}
    			
    		}
    	}
    }

  2. #2
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: ConnectException : Connection Refused

    Is there a firewall? Are both computers connected via a network? Do you have permissions to connect remotely on each computer?

  3. #3
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: ConnectException : Connection Refused

    I have Comodo FireWall.
    Both computers are not connected via a network. We are separated physically and have different ISPs.

    Do you have permissions to connect remotely on each computer?
    I don't know exactly

  4. #4
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: ConnectException : Connection Refused

    I have Comodo FireWall.
    On each computer, allow the firewall to accept connections for the port you are using, then test the application.

  5. #5
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: ConnectException : Connection Refused

    I have closed Comodo and added a tcp allow rule for port 10001 in windows firewall. Edited the code to connect on 10001 at server and client.
    And ran two instances on my system and tried to connect through external ip but the same exception is thrown.
    But if i give localhost it connects successfully.

    I have taken a screenshot.
    ConnectException : Connection Refused-socket.jpg

  6. #6
    homesting is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: ConnectException : Connection Refused

    Through the URL("http://api.externalip.net/ip/") obtained IP address which is external IP(117.200,10.73).Could you confirm if together the network by router or not,Because the router will be to dispather data,so inner ip address like 192.168.1.* .you can configure router to point your inner IP when connection port 10001.

  7. #7
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: ConnectException : Connection Refused

    When I test it with 127.0.0.1 and 192.168.1.2 both of the situations connected successfully.

    But when I connect to a machine on Internet I get ConnectException.

  8. #8
    homesting is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: ConnectException : Connection Refused

    You can assign router port(10001) to mchine,ie:10001->192.168.1.2,then you can through external IP(117.200,10.73) connect to machine.

  9. #9
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: ConnectException : Connection Refused

    I figured out the problem. I could not connect remotely on any system.
    But, I installed Remobo private network software and made a virtual network on the systems and tried to connect. The sockets connected successfully.

    Is there any way to connect them without using Remobo?
    Last edited by daxserver; 05-28-2012 at 07:56 PM.

Similar Threads

  1. Connection refused
    By rafalo235 in forum Networking
    Replies: 0
    Last Post: 01-27-2012, 06:18 PM
  2. java.net.connectexception connection refused connect
    By gagangujral in forum New To Java
    Replies: 2
    Last Post: 04-25-2011, 05:56 PM
  3. Replies: 1
    Last Post: 02-14-2011, 11:41 AM
  4. Replies: 3
    Last Post: 01-26-2009, 05:30 PM
  5. java.net.ConnectException: Connection refused
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-03-2007, 04:49 PM

Posting Permissions

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