java - Server Socket - Sending a test message to detect connection -


i'm trying use server sockets set connection between client , server. i'm not using java.nio.

the problem i'm sending test message, , detecting whether if successful in sending message (the client still connected), if not, client disconnected.

shown here:

    try     {         scanner in = new scanner(socket.getinputstream());         bufferedreader in_2 = new bufferedreader(new inputstreamreader(socket.getinputstream()));          while(stopthread)         {                 if(in_2.ready())                 {                     string message = in_2.readline();                     dt = new datetime();                      printstream out = new printstream(socket.getoutputstream());                     server.detect(message, datasets, out);                      datasets.add(message);                     gui.textarea_1.append(message + "\r\n");                     gui.textarea_1.setcaretposition(gui.textarea_1.getdocument().getlength());                 }                 else                 {                     printstream out = new printstream(socket.getoutputstream());                     out.println("testing connection \r\n");                     if(out.checkerror())                     {                         try                         {                             socket.close();                         }                          catch (ioexception e)                          {                             e.printstacktrace();                         }                         stopthread = false;                         gui.textarea.append(username + " disconnected \r\n");                         gui.textarea.setcaretposition(gui.textarea.getdocument().getlength());                         server.inputdataform(username, dt, datasets);                     }                     thread.sleep(3000);                 }          } 

the problem thread.sleep(3000) interfering getting data, since after 3 seconds, huge amount of data (because stopped thread 3 seconds).

now, proposed anonymous class in else statement.

class runthread implements runnable {      void run()      {           //put else statement here      } } 

but stopthread = false not constant, i'm trying control.

other threads i've searched puts variables inside main inside anonymous class, need stopthread stop while loop if client disconnected.

does have idea?

thanks!

why don't make class implements runnable has method stop();

public class myrunner implements runnable(){     mutableboolean stop = false;     public void run(){...}     public void stop(){         stop = true;     } } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -