Simple Client / Server app, not working?
I am using VS.NET 2005 beta 2, and I'm having a problem setting up a simple console client / server application using ServerSockets and Sockets.
Using my code, the server gets up and running, and receives a connection from the client.However,for some reason I cannot send information to the server and receive information back after this happens. Here is my code:
SERVER
=================
privatestaticclass sessionThread extends Thread { private Socket socket; public sessionThread(Socket s) { socket = s; } publicvoid run() { BufferedReader input; PrintWriter output; try { input =new BufferedReader(new InputStreamReader(socket.getInputStream())); output =new PrintWriter(socket.getOutputStream()); while ((s = input.readLine()) !=null) { /*thisfor some reason never happens! */ output.println(s); } socket.close(); } } } publicstaticvoid main(String[] args) throws Exception { ServerSocket sSocket =new ServerSocket(3000, 20); Socket socket; while ((socket = sSocket.accept()) !=null) new sessionThread(socket).start(); sSocket.close(); } |
If you look in the sessionThread.run() procedure, the commented code that says "This will never happen" is as far as the code on the server side gets to. For some reason, it never reads anything from the client past when the connection is made. Here is the client code:
CLIENT
=================
publicstaticvoid main(String[] args) throws Exception { Socket socket; BufferedReader stdin, input; PrintWriter output; socket =new Socket(args[0], 3000); stdin =new BufferedReader(new InputStreamReader(System.in)); input =new BufferedReader(new InputStreamReader(socket.getInputStream())); output =new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); while ((s = stdin.readLine()) !=null) { output.println(s); /* Again, will never happen! */ result = input.readLine(); System.out.println(result); } socket.close(); } |
Again, the commented code will never happen as it never receives anything from the server past the connection.
Can anyone tell me why this is happening?
Thanks!
Yaron

