Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Socket connection closes  RSS feed

 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the socket connection suddenly close?
Everything seems to be working fine and then suddenly I get an error while creating BufferedReader, PrintWriter.
java.net.SocketException. Socket is closed.
How can I fix this problem?
public class KnockKnockClient
{
Socket sock = null;
public void connectToServer()
{
try
{
sock = new Socket(address, port);
}
catch(UnknownHostException uhe)
{
}
}
public void sendName()
{
String myname = "Alice";
BufferedReader out = null;
PrintWriter in = null;
try
{
out = new PrintWriter(sock.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
out.println(myname);
System.out.println(in.readLine());
}
catch(IOException e)
{
}
}
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the "catch" blocks here are empty, so we don't actually know what's going wrong. At least have an "ex.printStackTrace()" inside the catch blocks so you know what succeeds or fails.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!