Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By quad64bit

Thread: ConnectException

  1. #1
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default ConnectException

    I can't figure out why do I get this exception:
    java.net.ConnectException: Connection refused: connect
    Exception in thread "main" java.lang.NullPointerException
    at Client.main(Client.java:31)

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    
    public class Server
    {
    	public static void main(String[]args) throws IOException
    	{
    		ServerSocket serverSocket = null;
    		Socket clientSocket = null;
    		PrintWriter out = null;
    		BufferedReader in = null;
    		
    		try
    		{
    			serverSocket = new ServerSocket(4441);
    			clientSocket = serverSocket.accept();
    			out = new PrintWriter(clientSocket.getOutputStream(),true);
    			in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    		}
    		catch(Exception e)
    		{
    			System.out.println(e);
    		}
    		
    		System.out.println(in.readLine());
    		
    		serverSocket.close();
    		clientSocket.close();
    		in.close();
    		out.close();
    	}
    }
    Java Code:
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    
    public class Client
    {
    	public static void main(String[]args) throws IOException
    	{
    		Socket echoSocket = null;
    		PrintWriter out = null;
    		BufferedReader in = null;
    		
    		try
    		{
    			echoSocket = new Socket("81.218.161.218",7);
    			out = new PrintWriter(echoSocket.getOutputStream(),true);
    			in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
    		}
    		catch(Exception e)
    		{
    			System.out.println(e);
    		}
    		
    		BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    		String userInput;
    		
    		out.println("I'm the client");
    		
    		out.close();
    		in.close();
    		stdIn.close();
    		echoSocket.close();
    		
    	}
    }
    Thanks in advanced.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: ConnectException

    Are you trying to run these on the same computer? 81.218.161.218 looks like an outside world address, if on the same machine, what does the loop-back address do? (127.0.0.1)

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: ConnectException

    81.218.161.218 is my ip, I get the same error for "localhost" and "127.0.0.1".
    Thanks for replaying.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: ConnectException

    Ok, and you are running both the client and the server on the same computer?

    If that's the case, then your port numbers need to match. On most OS' ports 1-1024 are reserved for OS, so use something like 1234 or 5555, or something else. Then both your server and client need to use the same port.

    I see your server socket is port 4441 and your client is 7. Don't use 7.
    tnrh1 likes this.

  5. #5
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: ConnectException

    Thank you so much.
    Diffrent ports was the problem.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

Similar Threads

  1. ConnectException : Connection Refused
    By daxserver in forum Networking
    Replies: 8
    Last Post: 05-28-2012, 04:39 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. ConnectException?
    By chyrl in forum Networking
    Replies: 1
    Last Post: 08-03-2010, 05:05 AM
  4. ConnectException not found
    By tamayo in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 08:23 AM
  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
  •