java - sending multiple byte array over the socket -


i want send multiple byte array client , server ?

i able send/receive 1 byte array client , send / receive 1 byte array server :

my code :

server :

 socket sock=null;   bytearrayoutputstream input=null;   outputstream out=null;     inputstream in=null;   try{       serversocket server_sock=new serversocket(2972);       sock=server_sock.accept();      in =         sock.getinputstream();     out=sock.getoutputstream();     }catch(ioexception e){       system.out.println(e.getmessage());   }  string word="";    //1-receive    try{      bytearrayoutputstream serverinput=new bytearrayoutputstream();  int len=0; byte[] buf=new byte[1000];         while ((len = in.read(buf))>=0) {               serverinput.write(buf, 0, len);          }        sock.shutdowninput();          word=new string(serverinput.tobytearray());     system.out.println("client send 1"+word);      }catch(exception e){       system.out.println(e.getmessage());   }      string st="server king"; try{     out.write(st.getbytes());     out.flush();  }catch(exception e){       system.out.println(e.getmessage());   } 

client :

 socket sock=null;     outputstream out=null;     inputstream in=null;     try{       sock=new socket("127.0.0.1",2972);       }catch(ioexception e){       system.out.println(e.getmessage());   }   string word="hellow world"  ;     try{     in =         sock.getinputstream();     out=sock.getoutputstream();     }catch(ioexception e){       system.out.println(e.getmessage());   }  //1- send    try{         system.out.println("your string is"+word+"converted byte"+word.getbytes());      out.write(word.getbytes());     out.flush();  sock.shutdownoutput();     }catch(exception e){       system.out.println(e.getmessage());   }      try{  bytearrayoutputstream serverinput=new bytearrayoutputstream();  int len=0; byte[] buf=new byte[1000];         while ((len = in.read(buf))>=0) {               serverinput.write(buf, 0, len);           }     system.out.println("server send 1 "+new string(serverinput.tobytearray()));      system.out.println("your string is"+word+"converted byte"+word.getbytes());      }catch(exception e){       system.out.println(e.getmessage());   } 

this code working fine 1 submitting client , server not work when want send / receive more byte array ?

it working when use shutdown because both client , server reading , writing data.

therefore, can not use socket channel again ... there alternative solution? ...that not lead deadlock.

the problem have don't have way when 1 byte array ends , next 1 starts. (in "one array" solution, end of byte array corresponds end of stream. , of course, once stream has been ended / closed, cannot reopened without creating new socket, etcetera.)

the simple way solve follows, using dataoutputstream , datainputstream pairs wrapped around respective socket streams:

  • to send byte array:

    1. convert data bytes.

    2. send byte array size using dataoutputstream.writeint(int) method.

    3. send byte array using dataoutputstream.write(byte[]) method.

  • to receive byte array:

    1. receive byte array size using datainputstream.readint() method.

    2. allocate byte array of required size.

    3. receive bytes byte array using datainputstream.read(byte[], int, int) method ... repeatedly until you've gotten of bytes.

by sending size of byte array @ front, tell receiver how many bytes read. can repeat process many times need. sender can indicate receiver there no more byte arrays send closing socket stream.

note - pseudo-code. assume capable of turning working java.

don't forget insert bufferedinputstreams , bufferedoutputstreams respective stream chains ... reduce system call overheads.


Comments