java - not able to send byte array from server to client,able to send from client to server -
java - not able to send byte array from server to client,able to send from client to server -
i have task this. create client , server socket interaction accepts byte info , converts byte info data received @ server in string , send response confirmation of info conversation success/unsuccess info passed prepare info length format validation should done @ server end.
as e.g.
there fields ur sending server like,
field 1 - number field 2 - string field 3 floating number i.e. 108.50
after conversion byte string : 152|any msg|108.50
in byte this,
10101|1001010010000000011000000000|1110111011
i have tried next programs this
server.java
public class server extends thread {     private serversocket serversocket;      public server(int port) throws ioexception     {         serversocket = new serversocket(port);         //serversocket.setsotimeout(100000);     }      public void run()     {         while(true)         {              seek             {                 socket server = serversocket.accept();                 byte message[]=null;                 datainputstream in =                         new datainputstream(server.getinputstream());                  bytearrayoutputstream buffer = new bytearrayoutputstream();                  int nread;                 byte[]   info = new byte[16384];                 while ((nread = in.read(data, 0, data.length)) != -1) {                     buffer.write(data, 0, nread);                 }                                 system.out.println("on line"); //this doesnt printed                 buffer.flush();                  data= buffer.tobytearray();                  system.out.println(data);                 string convertmsg = new string(data);                 system.out.println("msg converted "+convertmsg);                 dataoutputstream out =                         new dataoutputstream(server.getoutputstream());                 system.out.println("below dataoutputstream");                 out.write("success".getbytes());                 server.close();             }catch(sockettimeoutexception s)             {                 system.out.println("socket timed out!");                 break;             }catch(ioexception e)             {                 e.printstacktrace();                 break;             }         }     }     public static void main(string [] args)     {         int port = 4003;          seek         {             thread t = new server(port);             t.start();         }catch(ioexception e)         {             e.printstacktrace();         }     } }    client
public class client {     public static void main(string args[]) throws ioexception     {         int userinput =1;         while(userinput==1)         {             string servername = "192.168.0.8";             int port = 4003;              seek             {                 system.out.println("connecting " + servername                         + " on port " + port);                 socket client = new socket(servername, port);                 system.out.println("just connected "                         + client.getremotesocketaddress());                 outputstream outtoserver = client.getoutputstream();                 dataoutputstream out =                         new dataoutputstream(outtoserver);                 system.out.println("above out.wirte()");                 out.write("any msg".getbytes());                   inputstream infromserver = client.getinputstream();                 datainputstream in =                         new datainputstream(infromserver);                 bytearrayoutputstream buffer = new bytearrayoutputstream();                  int nread;                  system.out.println("converting array "+in);                 byte[]   info = ioutils.tobytearray(in);                   system.out.println(data);//this line doesnt printed                 //system.out.println("server says " + in.readutf());                 client.close();             }catch(ioexception e)             {                  e.printstacktrace();             }             system.out.println("enter userinput ");             datainputstream dis = new datainputstream(system.in);             string s = dis.readline();             userinput = integer.parseint(s);         }     }  }    if send info client server in bytes,it reads , prints it.also line "enter userinput " gets printed , if user enters '1' programme continues. problem programme given above. if seek send info server stating "success"(meaning info has been converted bytes string successfully) programme stucks , cursor doesnt go below line in comments "this line doesnt printed".there no error printed , none of programme terminates.i new socket programming , dont understand much networking. help appreciated.
you're reading input until end of stream, peer isn't closing connection, end of stream never arrives.
i suggest read , write lines, or utilize writeutf() , readutf().
 java sockets network-programming 
 
Comments
Post a Comment