user2995514
user2995514

Reputation: 1

Client is unable to connect to server; an IOException is thrown. Simple client/server issue

I'm a beginner (as you can probably tell) and I'm having issues establishing a connection from my very simple client to my very simple server. My server starts up fine as it creates a serversocket that listens on port 43594. However, when I run my client to try to establish a connection, an IOException is thrown by my client as it tries to connect.

I'm doing java in my spare time as a hobby, so I'd really appreciate if someone could help me understand what's going on, where I'm going wrong (or if I'm even going right any where) so as to help me improve.

Here is my Server code:

package src.org;

import java.io.FileInputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

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

public class Server {

    private static final Logger logger = Logger.getLogger(Server.class.getName());

    private final static void createSocket(int portNumber) throws IOException
    {
        ServerSocket serverSocket = new ServerSocket(portNumber);
        Socket clientSocket = serverSocket.accept();
        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    }

    public static void main(String... args)
    {
        logger.info("Starting server...");
        Properties properties = new Properties();
        logger.info("loading settings...");
        try
        {
            properties.load(new FileInputStream("settings.ini"));
            Constants.GAME_NAME = properties.getProperty("name");
            Constants.PORT = Integer.valueOf(properties.getProperty("port"));
        } catch(Exception ex)
        {
            ex.printStackTrace();
        }
        logger.info("Creating sockets...");
        try
        {
            logger.info("Socket created. Listening on port: " + Constants.PORT);
            createSocket(Constants.PORT);
        } catch(Exception ex)
        {
            logger.log(Level.SEVERE, "Error creating sockets.", ex);
            System.exit(1);
        }
    }

}

Which (to my knowledge) seems to be doing its job.

And here's what I believe to be the culprit class, the client class:

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

public class Client {

    //private static final String hostName = Constants.HOST_NAME;

    //private static final int portNumber = Constants.PORT;

    private static final String hostName = "localhost";

    private static final int portNumber = 43594;

    public static void main(String... args)
    {
        try (
            Socket socket = new Socket(InetAddress.getLocalHost(), portNumber); // using localhost at the moment
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        ) {
            System.out.println("Client socket created.");
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            String fromServer, fromUser;

            while((fromServer = in.readLine()) != null)
            {
                System.out.println("Server:" + fromServer);
                fromUser =  stdIn.readLine();
                if(fromUser != null) {
                    System.out.println("Client: " + fromUser);
                    out.println(fromUser);
                }
            }
        } catch(UnknownHostException ex) {
            System.err.println("Unknown host: " + hostName);
            System.exit(1);
        } catch(IOException ioe) {
            System.err.println("Couldn't get i/o from: " + hostName);
            System.out.println("Error:" + ioe.getMessage());
            System.exit(1);
        }
    }
}

When I ping localhost I get a response; the port on both sides is 43594 and the host is just local. The command line response from the client is:

Client socket created Couldn't get i/o from: localhost Error: connection reset Press any key to continue...

I'm sorry in that I know this would be a very simple fix to many of you, but I can't seem to find an answer or fix it myself. Any insight or help would be greatly appreciated. Cheers.

Sorry if I've left out any other important pieces of information.

Upvotes: 0

Views: 3468

Answers (1)

user207421
user207421

Reputation: 311054

You've left out much of the code. The part in the server that sends data on the accepted socket. So the method that calls accept() just exits, the socket is garbage-collected, and closed, and the client keeps doing I/O to the other end of the connection, which is invalid,so you get an exception.

Upvotes: 1

Related Questions