java - Creating an echo server - server can only respond once -


i'm creating multithreaded chat server supposed create separate thread each connected client. every time client connects, server creates new instance of client handler class, supposed keep track ingoing , outgoing messages from/to specific client.

the first time client connects echo server, server respond echo of clients response. if try send message server second time, client creates ioexception. have created client application myself, know works because can communicate other servers fine. i'm pretty sure problem somewhere in run method of client handler class, can't figure out why it's not working. here's run method in client handler class:

public void run() {     try (         bufferedreader in =                 new bufferedreader(                     new inputstreamreader(clientsocket.getinputstream()));          printwriter out =              new printwriter(clientsocket.getoutputstream());     ) {         long time = system.currenttimemillis();                  out.println("server - " + time + ": " + in.readline());          out.close();          try {             in.close();         } catch (ioexception e) {             system.err.println("couldn't close input stream");         }     } catch(ioexception e) {         system.err.println("got ioexception error while reading or writing from/to client");     } } 

i've guessed i'm supposed have kind of while loop somewhere, of attempts implement have failed. e.g. i've tried change code:

long time = system.currenttimemillis();          out.println("server - " + time + ": " + in.readline()); 

to this:

string inputline; while((inputline = in.readline()) != null) {     long time = system.currenttimemillis();              out.println("server - " + time + ": " + inputline); } 

this solution more or less copy of how oracle site (http://docs.oracle.com/javase/tutorial/networking/sockets/clientserver.html) says it's supposed done.

i think main problem might me not grasping whole concept of server/client communication, push in right direction appreciated.

thanks in advance!

the important bit in oracle article mention part titled - supporting multiple clients.

the basic java socket api blocking api, means call method , method blocks until io event happens. if need wait multiple io events - in case incoming client connection , incoming data - have create multiple threads.

the server shown in article accepts single incomming (client) connection , close once client closes because inputstream on server return null causing loop terminate.

first server needs (which simplified example):

try (serversocket serversocket = new serversocket(portnumber)) {   while (running)   {     socket clientsocket = serversocket.accept();     new thread(new clienthandler(clientsocket)).start();   } }    

note: starting thread each client connection demonstrates point vast on simplification of managing connection load on server.

the client code can remain is.

that's basics pointed out leaves thread management in developers hands - leads trouble because people wrong. because of java's socket apis have been extended create nio api - jakob jenkov has written tutorials.

it's worth looking @ netty, think easier use nio.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -