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.:^): :(sweat):
Here is the code.
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();
}
}
}
}
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?
Re: ConnectException : Connection Refused
I have Comodo FireWall.
Both computers are not connected via a network. We are separated physically and have different ISPs.
Quote:
Do you have permissions to connect remotely on each computer?
I don't know exactly :(shake):
Re: ConnectException : Connection Refused
Quote:
I have Comodo FireWall.
On each computer, allow the firewall to accept connections for the port you are using, then test the application.
1 Attachment(s)
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.
Attachment 3792
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.
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.
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.
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?