0

I'm trying to create a client/server connection and am still quite new to java as well. So the error I am getting tells me that the socket is closed. Following some work, I've managed to write the given code below. I do believe there is something wrong with the way I pass the socket to the connection class, if I had to guess, that causes the socket object to possibly be closed?

I've tried adding waits just in case the server thread hadn't been executed but that didn't seem to affect anything. Maybe I should launch the server with its own launcher in its own command prompt, but I thouht this should work just fine to test the client and server.

I can't seem to find out why my socket is closed before I send my message. Any insights would be greatly appreciated!

Thanks!

Error

java.net.SocketException: Socket is closed at java.net.Socket.getInputSTream(Unknown Source) at Connection.run(Connection.java:17) 

Server.java

//main calling snippet. import java.lang.Thread; public class Server { public static void main(String[] args) { if(args.length != 1) { System.err.println("Usage: java Server <port number>"); System.exit(1); } int port = Integer.parseInt(args[0]); Thread server = new KServer(port); server.start(); //added waits just to make sure the thread was executed? //thinking this might be my problem long t = System.currentTimeMillis() + 5000; while (System.currentTimeMillis() < t) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } KClient client = new KClient("127.0.0.1",port); while (!(client.openConn())) { System.out.println("Failed to connect. Retrying..."); } client.send("Hello World"); client.closeConn(); } } 

KServer.java

//the actual server class that manages listening and threading the sockets import java.net.*; import java.io.*; public class KServer extends Thread { private int port; private ServerSocket sSock; public KServer(int thisPort) { port = thisPort; try { sSock = new ServerSocket(port); } catch (IOException e) { e.printStackTrace(); } } public void run() { while(true) { try (Socket cSock = sSock.accept();) { Thread con = new Connection(cSock); con.start(); } catch (IOException e) { e.printStackTrace(); } } } } 

Connection.java

//Manages sending and receiving messages import java.net.Socket; import java.io.*; public class Connection extends Thread { Socket socket; public Connection(Socket s) { socket = s; } public void run() { String msg; BufferedReader in; try { in = new BufferedReader(new InputStreamReader(socket.getInputStream())); while((msg = in.readLine()) != null) { System.out.println(msg); } } catch (IOException e) { e.printStackTrace(); } } } 

KClient.java

//manages the clients connection life to the server import java.net.*; import java.io.*; public class KClient { private Socket sock; private String dest; private int port; private OutputStreamWriter out; public KClient(String dst,int prt) { dest = dst; port = prt; } public boolean openConn() { try { sock = new Socket(dest,port); out = new OutputStreamWriter(sock.getOutputStream(),"ISO-8859-1"); } catch (Exception e) { e.printStackTrace(); return false; } return true; } public void send(String msg) { try { out.write(msg); out.flush(); } catch (IOException e) { e.printStackTrace(); } } public void closeConn() { try { out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } 
0

    1 Answer 1

    2

    Don't use try-with-resources to accept the socket. It wil close the accepted socket, which needs to stay open so the handling thread can use it. The handling thread is responsible for closing it.

    4
    • Can you explain what happens? Does the the fact I used it as a resource kill the object outside of that try block? So the socket is destroyed before trying to call getInputStream()?
      – Chemistpp
      CommentedFeb 28, 2016 at 20:30
    • 1
      Thanks for being cool about it. You said it closes it, but why? Does it close it because it's destroyed? There's a bit more to it than it closed it. No one makes you answer this question, I figured you like to discuss these things or you wouldn't be answering questions on SO. Didn't mean to waste your time. Thanks for your answer
      – Chemistpp
      CommentedFeb 28, 2016 at 20:38
    • It closes it because that is what it's for. It has no other purpose. I already said all that. Look it up if you don't believe it. I don't know why you're using language features when you don't know what they mean. And asking me to repeat myself continually isn't 'cool'. Don't be surprised if you get a reaction.CommentedFeb 28, 2016 at 21:59
    • 2
      @EJP I think thtat you dont need to be rude like that. In fack your answer is not clear and understanding!CommentedMar 12, 2018 at 15:51

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.