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(); } } }