-3

Whenever I try to receive a message from my Server, I get the exception below. This happens at the line rcvdId = inFromServer.readLine(); in the Client. The server does receive the variables I am sending. The variable serialToNucleo.nucleoAnswer even contains a string. I know the code will not fully work yet, as I am only sending one variable, but I am working on that.

Client:

try {

    Socket skt = new Socket("192.168.1.9" , 6789);

    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(skt.getInputStream()));
    PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
    System.out.print("Sending string: '" + vraagTemp + " and " + vraagId+"'\n");
    out.print(vraagTemp + "\n");
    out.print(vraagId + "\n");
    out.close();
    rcvdId = inFromServer.readLine();
    rcvdTemp = inFromServer.readLine();

    WriteToDb writeToDb = new WriteToDb();
    writeToDb.SendDataToDb(rcvdId,rcvdTemp);

    skt.close();

}

Server:

while (true) {

    Socket skt = serverSocket.accept();
    BufferedReader inFromClient = new BufferedReader(new InputStreamReader(skt.getInputStream()));
    PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
    ontvServer = inFromClient.readLine();
    id = inFromClient.readLine();
    System.out.println("Received: " + ontvServer);

    if (id != null && ontvServer != null)
    {
        serialToNucleo.SetupComm(ontvServer, id);
    }
    else
    {
        Thread.sleep(100);
    }

    if (serialToNucleo.nucleoAnswer!= null) {
        out.print(serialToNucleo.nucleoAnswer + "\n");
        id = null;
        ontvServer = null;
    }
    else
    {
        Thread.sleep(100);
    }

}

Stacktrace from the exception thrown

java.net.SocketException: socket closed at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at ChatClient.main(ChatClient.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
user207421
  • 305,947
  • 44
  • 307
  • 483
ThaSam
  • 13
  • 7
  • 2
    Are you receiving any sort of exception? – Logan Nov 25 '16 at 22:46
  • "socket closed" is the exception it catches – ThaSam Nov 25 '16 at 22:52
  • can you give the exact name? That's not very helpful. – Logan Nov 25 '16 at 22:54
  • java.net.SocketException: socket closed – ThaSam Nov 25 '16 at 22:55
  • When you ask about an exception, **always** post the exact and complete stack trace. – JB Nizet Nov 25 '16 at 23:19
  • Post the stack trace. Edit it into your question. – user207421 Nov 25 '16 at 23:21
  • I am all new to this java, so you have to excuse me but i dont know what exactly you mean by that and how to get it? – ThaSam Nov 25 '16 at 23:22
  • The stack trace is printed by `exc.printStackTrace()`, where `exc` is the exception you caught. – user207421 Nov 25 '16 at 23:25
  • http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors, http://stackoverflow.com/questions/6822968/print-the-stack-trace-of-an-exception – JB Nizet Nov 25 '16 at 23:25
  • Added the exceptions to the main question. Thx JB Nizet – ThaSam Nov 25 '16 at 23:33
  • Fixed your incorrect title and post and stack trace. NB 'there is a problem with connection to the server' is not part of the exception. It is an incorrect message printed somewhere (not shown) by your application. Don't guess about exceptions. Print the exception, not what you think it means. – user207421 Nov 25 '16 at 23:45

1 Answers1

1

'Socket closed' means that you closed the socket and then continued to use it.

It has nothing to do with 'connection lost'.

It happens here:

out.close();

Closing either the input or output stream of a socket closes the socket. Change to out.flush().

NB Your server code leaks sockets like a sieve.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • When i debugged my java program, the program ended before it reached the line of code where i should be closing the socket. I am new to all this and trying real hard here. Been pondering on this program for days. – ThaSam Nov 25 '16 at 23:25
  • There is no 'line of code' here where the server 'should close the socket'. This only reinforces the conclusion that this isn't the real code. – user207421 Nov 25 '16 at 23:30
  • The server doesnt close the connection. The client does. It is the client that receives the exception. – ThaSam Nov 25 '16 at 23:34
  • The server should close the socket when it is finished with it. This is a resource leak. – user207421 Nov 25 '16 at 23:36
  • The out.close() has been changed to out.flush(). Now the Client program just halts at rcvdId = inFromServer.readLine(); . The exception is gone. When i debugged the server it seems to have send the variables without a problem but the client doesnt seem to receive it. – ThaSam Nov 26 '16 at 00:19
  • You need `println()` instead of `print()`, and the server should close the accepted socket when it has finished with that connection. Not the listening socket. You need to look at the Custom Networking section of the Java Tutorial. – user207421 Nov 26 '16 at 00:50
  • I have used tutorials to make this program. Sadly enough they used the .print version. This appears to be all what i needed to do. Will close the connection at the server side, so there wont be anymore leaks. – ThaSam Nov 26 '16 at 01:16
  • But did you use the Java tutorial I mentioned? 99% of the Internet is garbage, and networking tutorials are no exception. I've rarely seen one without a major error, and as a matter of fact that *includes* the one I mentioned. (They fixed it on my request some years ago.) – user207421 Nov 26 '16 at 01:18