1024
1024

Reputation: 23

a bizarre Connection refused: connect error in Java client

I have encountered the following error:

java.net.ConnectException: Connection refused: connect

I'll show some code below but first, a description because it baffles me. I wrote a very basic chat program that can act as either a server or client depending on the initial button you press (host or connect). Tested on both localhost and over the internet with friends, works perfectly, as intended. I've started writing a second software and using almost the same code, but having more classes, I get this error when trying to connect to a host of the same program. So, prog A can connect to prog A fine, but B cannot connect to B. One might assume firewall issues, but here's the plot twist: B can connect to A and A can connect to B again perfectly. But B cannot connect to another copy of itself. I used the official oracle tutorials for TCP and worked perfectly on the chat program referred to as A. Depending on the press of a button, A will run one of its main class' methods (to start a server or client). B on the other hand, will create a new object of a class, either a Host

public class Host
{
static ServerSocket serverSocket = null;
static Socket clientSocket = null;
static PrintWriter out = null;
static BufferedReader in = null;
static String inputLine;
public Host() throws IOException
    {
    serverSocket = new ServerSocket(4444);
    System.out.println("Server created, waiting for guest.");
    clientSocket = serverSocket.accept();
    System.out.println("Guest connected.");
    out = new PrintWriter(clientSocket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    while ((inputLine = in.readLine()) != null)
        {
        //if (inputLine.equals("q")) {break;}
        System.out.println("Message recieved:" + inputLine);
        }
    }
}

or a Guest

public class Guest
{
static Socket socket = null;
static PrintWriter out;
static BufferedReader in = null;
static String inputLine;
public Guest() throws IOException
    {
    System.out.println("Connecting to host");
    try
        {socket = new Socket("localhost", 4444);
         out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        }
    catch (UnknownHostException err) {System.out.println("unknown host"); System.exit(3);}
    catch (IOException err) {System.out.println(err); /*System.exit(4);*/}
    System.out.println("Connected to host");

    while ((inputLine = in.readLine()) != null)
        {
        //if (inputLine.equals("q")) {break;}
        System.out.println("Message recieved:" + inputLine);
        }
    }
}

I have the proper imports, I just didn't paste them here. Both programs try to connect to localhost and only A works, this other one does not. Again, creating a Guest, I can connect to the chatprogram's server. A chatprogram client can connect to this server. But this client cannot connect to this server.

Has anyone experienced anything similar? Is there an obvious solution, something I'm missing? I'm really clueless here, I literally copy/pasted server/client codes.

Edit: stack trace.

java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at mainPack.Guest.<init>(Guest.java:21)
at mainPack.GameStarter.grandMain(GameStarter.java:101)
at mainPack.GameStarter.<init>(GameStarter.java:27)
at mainPack.GameStarter.main(GameStarter.java:34)

Upvotes: 1

Views: 1984

Answers (3)

Ryan Stewart
Ryan Stewart

Reputation: 128749

In your Host, you only accept one connection, and then the server stops listening. If something is tying up that connection, nothing else would be able to connect to the Host. An accepted connection should immediately be delegated to another thread for processing so that the Host can loop and listen on the server socket again. See "Supporting Multiple Clients" in the Socket tutorial.

Upvotes: 2

Darrell Teague
Darrell Teague

Reputation: 4272

Need more details as to how this is being run and what the error is exactly (I suspect client returns a network-level connect error when attempting to reach the host) but in general there is only one port being used: 4444. I presume the error (not posted) is that this port is in use by the first programs that are communicating on that port on the same machine.

Further, the static references only allow one instance of the various sockets to exist in a given JVM at a time. These cannot be concurrently used by multiple threads possibly implemented by other client classes using these same (one) static resources.

Upvotes: 0

xagyg
xagyg

Reputation: 9711

Remove your static declarations from the variables in Guest. Same for Host (although if you have only one host it should be ok).

Update

e.g. pseudo-code

create server socket
while true (or quit received) {
    accept connection
    spawn thread to handle communication
}

Upvotes: 0

Related Questions