multithreading - TCP connection thread for reading and writing and receiving event messages in java -


i'm new java , tcp in general.

i have following scenario send request tcp server , receive reply directly, server send unsolicited event message later.

i implemented following client

 import java.io.datainputstream;  import java.io.dataoutputstream;  import java.io.ioexception;  import java.io.outputstream;  import java.net.inet4address;  import java.net.inetaddress;  import java.net.socket;  import java.net.socketexception;  import java.net.unknownhostexception;   import org.apache.commons.lang.stringutils;  import org.apache.commons.logging.log;  import org.apache.commons.logging.logfactory;   public class tcpclient {  private inetaddress connectedaddress; private socket tcpsocket; private int connectedport; private outputstream outstream; private boolean first = true;  private static final log log = logfactory.getlog(tcpclient.class);  public tcpclient(string host, int port) {     try {         this.connectedaddress = inet4address.getbyname(host);         this.connectedport = port;         this.tcpsocket = new socket(connectedaddress, connectedport);         this.outstream = this.tcpsocket.getoutputstream();     } catch (socketexception e) {         e.printstacktrace();     } catch (unknownhostexception e) {         e.printstacktrace();     } catch (ioexception e) {         e.printstacktrace();     } }  public void sendmessage(string message) throws ioexception {     log.debug("sending message \n" + message);     synchronized (this) {         if (!this.tcpsocket.isconnected())             return;         dataoutputstream dos = new dataoutputstream(outstream);         dos.write(message.getbytes());         dos.flush();         if(first){             first = false;             (new thread(new tcplisteningthread())).start();         }     } }  private class tcplisteningthread implements runnable {      public tcplisteningthread() {         // nothing do...     }      @override     public void run() {         try {             while (true) {                 datainputstream dis = new   datainputstream(tcpsocket.getinputstream());                 int len = dis.readint();                 byte[] data = new byte[len];                 dis.read(data);                 string response = new string(data, "us-ascii");                 if (null != response && !stringutils.isblank(response)) {                 log.debug("received response [" + response + "]");                 processxmlresponse(response);                 }             }         } catch (ioexception e) {             e.printstacktrace();         } catch (exception e) {             e.printstacktrace();         }     }  }  } 

this code seems read first response first request sent while removing if(first) condition , calling reading thread every time request sent, , removing while true, code seems able read response every request sent, of course won't able read event messages.

i appreciate help.

thanks :)


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -