Skip to content Skip to sidebar Skip to footer

How To Use Socket To Send And Receive Message From Server?

Hi guys so I have a task where I'm suppose to send a message to a server, and then get a reply back. I keep getting failed 1recvfrom failed: EBADF (Bad file descriptor) error and I

Solution 1:

printWriter.close();

Closing the input or output stream of a socket closes the other stream and the socket. Remove both occurrences.

if(bufferedReader.readLine()!=null)

This doesn't make sense. You are throwing the line away and then reading the next line if it wasn't null. You need to store the result into a variable.

You probably need to use printWriter.println() instead of printWriter.write(). Probably the peer is reading lines too, and you aren't sending one (no line terminator).

If reply == null it means the peer has closed the connection. Not 'no message yet'. An empty string may be a protocol error but it also is not 'no message yet'.

Post a Comment for "How To Use Socket To Send And Receive Message From Server?"