0

I've got a very simple multithreaded server that just prints back the client's input. The problem I'm having is that the client is crashing out after more than one use of outToServer.writeBytes().

My source code for the client is here:

public class Client {

    public void run() throws Exception{
        String sentence;

        Socket clientSocket = new Socket("localhost", 25565);

        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

        while (true){
            sentence = inFromUser.readLine();
            if(!sentence.equalsIgnoreCase("exit")){
                outToServer.writeBytes(sentence + '\n');
            } else {
                break;
            }
        }

        clientSocket.close();
    }
}

I've done some research on the error and it might be my college network killing the connection, but it doesn't make much sense given that it allows the first connection.

Also, here's the error:

java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at java.io.DataOutputStream.writeBytes(Unknown Source)
    at com.cs.Client.run(Client.java:21)
    at com.cs.Main.main(Main.java:14)

At line 21 in Client.java is the line with writeBytes in it

JamEngulfer
  • 747
  • 4
  • 11
  • 33
  • 1
    What is the exception? – Jean Logeart Sep 18 '14 at 14:33
  • Please post the exception. If it is not caught and at least logged, the that's the real problem. Odds are it tells you (almost) exactly what went wrong. Also, it is a novice error (but very common one) to assume that a network connection is available after it is obtained, for as long as you want it. It isn't, so if you want to really have a robust program, don't solve your problems as if it were. – Edwin Buck Sep 18 '14 at 14:37
  • Whoops. I knew I forgot something. I'll add the exception now – JamEngulfer Sep 18 '14 at 14:41
  • http://stackoverflow.com/questions/2126607/official-reasons-for-software-caused-connection-abort-socket-write-error – Jean Logeart Sep 18 '14 at 14:44
  • Yes, I saw that post, hence the statement in my question: "I've done some research on the error and it might be my college network killing the connection". However I posted the question anyway in case it was something with my code. Anyway, (to quote my question) "it doesn't make much sense given that it allows the first connection" – JamEngulfer Sep 18 '14 at 14:48

1 Answers1

0

Opening a connection is not like opening a door. A connection is a virtual concept and not a guaranteed path.

So when you open a connection, basically you have reconfigured some operating system managed area of memory to know that when you write data into a particular memory location, it needs to copy that data on the wire.

This means that opening a connection is not always a shared event (as in the remote machine might not fully guarantee a path to the program, or even might not be aware that a path to the remote program was requested).

So, in network programming, despite APIs that are worded to imply otherwise, you don't have a functional connection until you get the first response from the remote machine.

I'd see if you can fire up wireshark and see if you can capture the data prior to send, and I'd check any connection parameters, and attempt to verify connection reachability independently of the program.

The above procedure will help you quickly identify which network componet is at fault from the client's point of view; however, 90% of the time, it is something really trivial, like a software firewall blocking the desired port.

Also, you can use telnet to provide similar functionality, but connecting to a non-standard port.

telnet hostname 25565

Good luck, and your code seems pretty reasonable, I'd spend a little time making sure that you aren't focusing on the code when the environment might be at fault.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • Might also want to flush the output stream. But the exception indicates that flushing the output stream is not the problem you are currently encountering. – Edwin Buck Sep 18 '14 at 15:03