Results 1 to 9 of 9
- 05-10-2012, 07:59 PM #1
Member
- Join Date
- Apr 2012
- Location
- Vijayawada, India
- Posts
- 22
- Rep Power
- 0
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.
.gif)
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(); } } } }
- 05-10-2012, 09:02 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
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?
- 05-11-2012, 10:02 AM #3
Member
- Join Date
- Apr 2012
- Location
- Vijayawada, India
- Posts
- 22
- Rep Power
- 0
Re: ConnectException : Connection Refused
I have Comodo FireWall.
Both computers are not connected via a network. We are separated physically and have different ISPs.
I don't know exactlyDo you have permissions to connect remotely on each computer?
- 05-11-2012, 04:50 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: ConnectException : Connection Refused
On each computer, allow the firewall to accept connections for the port you are using, then test the application.I have Comodo FireWall.
- 05-11-2012, 05:44 PM #5
Member
- Join Date
- Apr 2012
- Location
- Vijayawada, India
- Posts
- 22
- Rep Power
- 0
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.
- 05-15-2012, 02:50 PM #6
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
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.
- 05-15-2012, 04:21 PM #7
Member
- Join Date
- Apr 2012
- Location
- Vijayawada, India
- Posts
- 22
- Rep Power
- 0
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.
- 05-16-2012, 05:52 AM #8
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
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.
- 05-28-2012, 04:39 PM #9
Member
- Join Date
- Apr 2012
- Location
- Vijayawada, India
- Posts
- 22
- Rep Power
- 0
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
-
Connection refused
By rafalo235 in forum NetworkingReplies: 0Last Post: 01-27-2012, 06:18 PM -
java.net.connectexception connection refused connect
By gagangujral in forum New To JavaReplies: 2Last Post: 04-25-2011, 05:56 PM -
Java issues: ConnectException: connection refused: connect
By spongeb00b in forum New To JavaReplies: 1Last Post: 02-14-2011, 11:41 AM -
Error opening socket: java.net.ConnectException: Connection refused: connect
By somersst in forum Java ServletReplies: 3Last Post: 01-26-2009, 05:30 PM -
java.net.ConnectException: Connection refused
By Albert in forum Advanced JavaReplies: 2Last Post: 07-03-2007, 04:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks