3

I am unable to do any networking in Java when i run a simple networking program i am unable to get a connection, even when using the local host, The Error:

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at EchoClient.main(EchoClient.java:8)

This is the program:

import java.io.*;
import java.net.*;
import java.util.*;

public class EchoClient{
    public static void main(String[] args) {
        try{
            Socket client = new Socket(InetAddress.getLocalHost(), 1234);
            InputStream clientIn = client.getInputStream();
            OutputStream clientOut = client.getOutputStream();
            PrintWriter pw = new PrintWriter(clientOut);
            BufferedReader br = new BufferedReader(new InputStreamReader(clientIn));
            Scanner stdIn = new Scanner(System.in);
            System.out.println("Input Message:");
            pw.println(stdIn.nextLine());
            System.out.println("Recieved Message:");
            System.out.println(br.readLine());
            pw.close();
            br.close();
            client.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

I use windows 7, i have turned of windows firewall and i dont have an anti virus.

6
  • It seems you need a server to connect to! Have you started the "EchoServer"? Commented Feb 16, 2011 at 7:53
  • It can be useful: All about sockets: download.oracle.com/javase/tutorial/networking/sockets/… Commented Feb 16, 2011 at 8:05
  • Side note: you should call those close() methods in a "finally" block to ensure that they happen even if there's an exception in the main block. Otherwise, your program will leak resources. By the way, close() always performs a flush(), so you don't need to flush explicitly if you close in the finally block. Commented Feb 16, 2011 at 8:17
  • @Andrew Swan: The close() is done after the client wants response - which will lead to "deadlock"... Commented Feb 16, 2011 at 8:21
  • just run this in your cmd prompt netstat -aon | findstr "1234" , to see any process running on port 1234 Commented Feb 16, 2011 at 8:21

3 Answers 3

1

As I wrote in my comment, check that the server (something like EchoServer?) is up and running.


But there are other problems when you succeed connecting. pw.println(stdIn.nextLine()); might not send the content to the server, you need to do a pw.flush(); to really send the content or you can create your PrintWriter with autoflushing:

 pw = new PrintWriter(clientOut, true);

If you need a EchoServer I just wrote one that works with your client if you add the flushing that I described above:

public class EchoServer {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(1234);

        while (true) {
            // accept the connection 
            Socket s = ss.accept();

            try {
                Scanner in = new Scanner(s.getInputStream());
                PrintWriter out = new PrintWriter(s.getOutputStream(), true);

                // read a line from the client and echo it back
                String line;
                while ((line = in.nextLine()) != null) 
                    out.println(line);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                s.close();
            }
        }
    }
}

You can try it with telnet localhost 1234.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem is not with the pw.flush(); the problem is with Socket client = new Socket(InetAddress.getLocalHost(), 1234); If i delete every thing else except from that i still get an error
And you have started the server? If you do not have a server you can try the code for a server that I provided - I have also tried it with your client (+flushing) and it works!
1

Double check that your local EchoServer is up and running on port 1234. If there is no server operating on that port, you won't be able to connect.

Comments

1

My solution in Windows 7 was to turn on "Simple TCP/IP Services". It's described here: http://www.windowsnetworking.com/articles_tutorials/windows-7-simple-tcpip-services-what-how.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.