java - Infinit Loop when socket client disconnect -


i had implemented java multithreading socket server. it's working fine , create new thread every new client connection. have bug when tcp connection cutted client, server enter in infinit loop , got message every loop:

error : bad frame  !!! : null 

i tried debug execution , change if else condition same bug exist when client disconnect.

here server code

public class tcpsockserver implements runnable {      socket sock;     static int counter = 0;     private static int timeout = 50000;     private static int max_timeout = 100000;     long lastreadtime;       public tcpsockserver(socket sock) {         this.sock = sock;     }       public static void main(string[] args) {           try {             serversocket serversock = new serversocket(12000);              system.out.println("tcpsockserver : listening port 12000 ...");              while (true) {                 socket newsock = serversock.accept();                 counter++;                 inetaddress addr = newsock.getinetaddress();                 system.out.println("tcpsockserver : connection number : "+ counter);                 system.out.println("tcpsockserver : connection made "                         + addr.gethostname() + " : (" + addr.gethostaddress()                         + ")");                 newsock.setsotimeout(timeout);                 newsock.setkeepalive(true);                 new thread(new tcpsockserver(newsock)).start();             }          } catch (ioexception e) {             system.err.println("trackiz: main : error connection failed");             e.printstacktrace();         }      }      @override     public void run() {          int clientid = counter;         try {              bufferedreader instream = new bufferedreader(new inputstreamreader(                     sock.getinputstream()));             stringbuilder instring = new stringbuilder();             string frame = null;             printstream outstream = new printstream(sock.getoutputstream());              while (true) {                 if (instring.append((string) instream.readline()) == null) {                     system.out.println("tcpsockserver : client not connected");                     sock.close();                     break;                 } else {                     lastreadtime = system.currenttimemillis();                     frame = instring.tostring();                      if (cond1(frame)) {                         ......                     }else{                      system.err.println("\nerror : bad frame  !!! : "+frame); // why enter else in infinit loop                     }                     outstream.println(provt.sendcommand("tcpsockserver : ack client"));                     instring = null;                     instring = new stringbuilder();                  }             }             instream.close();             outstream.close();             sock.close();          } catch (sockettimeoutexception e) {             if (!isconnectionalive()) {                 system.out.println("\nconnection terminated client !"                         + clientid);                 logger.log(level.severe, "tcpsockserver : connection terminated client");                 try {                     sock.close();                 } catch (ioexception e1) {                     // todo auto-generated catch block                     e1.printstacktrace();                 }             } else {                 // todo sendheartbeat();                  system.out.println("sending heartbeat ...");             }              e.printstacktrace();         } catch (ioexception e) {             system.err.println("tcpsockserver : connection timeout. try reconnect !");             e.printstacktrace();         }         }      public boolean isconnectionalive() {         return system.currenttimemillis() - lastreadtime < max_timeout;     }  } 

the append method never return null, not right, if-branch never entered:

  if (instring.append((string) instream.readline()) == null) { 

the code should more this:

  string line = instream.readline();   if (line == null) {       ...   } else {       instring.append(line);       ...   } 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -