multithreading - Java Multiple threads using a client socket -




i'm creating server based chat program has multiple worker threads each handling client on socket. has single server socket passes off client worker thread.

    public static void main(string[] args) {     executorservice executor = executors.newfixedthreadpool(5);     serversocket serversocket = null;     try {         serversocket = new serversocket(4001);         system.out.println("listening server");         while (true) {             socket socket = serversocket.accept();             system.out.println("connected");             random rand= new random();             int port=4001+rand.nextint(5);             worker worker = new worker(port);             executor.execute(worker);             system.out.println("thread started");             new printwriter(socket.getoutputstream(), true).println(port);             socket.close();             system.out.println("closed");             //  break;         }     } catch (ioexception e) {         e.printstacktrace();     }  }  public class worker implements runnable {  private int port; public worker(int i) {     port=i; } @override public void run() {     worker(); } private static socket socket; private static printwriter out; private static bufferedreader in;  private void worker() {     try {         serversocket serversocket = new serversocket(port);         socket = serversocket.accept();         out = new printwriter(socket.getoutputstream(), true);         in = new bufferedreader(new inputstreamreader(socket.getinputstream()));         string line;         while ((line = in.readline()) != null) {             system.out.println("received: " + line);             switch (line) {                 case ("button press"):                     system.out.println("handled button");                     out.println("button acknowledged");                     break;                 case ("give me data"):                     system.out.println("handled data");                     out.println("have data");                     break;             }         }     } catch (ioexception e) {         e.printstacktrace();     } {         try {             out.close();             in.close();             socket.close();         } catch (ioexception e) {             e.printstacktrace();         }      } } 

this works fine, issue when have automated request client check messages , user provides input @ same type. causes conflict actual methods take couple of seconds run , if more input received request won't handled because in middle of method. example:

private static bufferedreader in; private static printwriter out; public static void main(string[] args) {     main main=new main();     main.createwindow();     try {         socket init = new socket(inetaddress.getlocalhost(), 4001);         int port              =integer.parseint                 (new bufferedreader                     (new inputstreamreader(init.getinputstream())).readline());         init.close();         socket socket=new socket(inetaddress.getlocalhost(), port);         out = new printwriter(socket.getoutputstream(), true);         in = new bufferedreader(new inputstreamreader(socket.getinputstream()));       while(true){         try {             thread.sleep(5000);             out.println("give me data");             if(in.readline().equals("have data")){                 system.out.println("data recieved");             }else{                 system.out.println("data not recieved");             }         } catch (interruptedexception e) {             e.printstacktrace();         }      }     } catch (ioexception e) {         e.printstacktrace();     } }   private void createwindow(){     jframe frame =new jframe("this button");     container pane=getcontentpane();     jbutton button=new jbutton("this button");     button.addactionlistener(this);     pane.add(button);     frame.settitle("messaging");     frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.setcontentpane(pane);     frame.setvisible(true);     frame.setsize(400, 350); }  @override public void actionperformed(actionevent e) {     system.out.println("button press");     try {         out.println("button press");         if(in.readline().equals("button acknowledged")){             system.out.println("button complete");         }else{             system.out.println("button fail");         }     } catch (ioexception e1) {         e1.printstacktrace();     } } 

if button pressed whilst server fetching data conflicts , wrong response sent. how deal this? if create separate socket handle automated checks that's double amount of threads server has cope with. there better solution? , please can try explain new area me.

your problem have 2 threads interacting socket, main thread , swing event handling thread. means 2 threads can queue 2 different things, , responses may picked wrong thread. simplest way want put socket interaction in 1 thread.

there 2 ways this. 1 main thread queue periodic automated checks swing event handling thread. however, that's bit complicated , possibly buggy actual swing threading model different what's documented.

the other way swing event handling thread queue button presses main thread. in case, queue button press main thread using proper synchronization. main loop of main thread check button presses , send them through socket , wait proper response.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -

python - Read npy file directly from S3 StreamingBody -