Read bytes from socket on Java -


i'm executing code , when create byte array called buffer, it's initialized 0 when .read() function doesn't wait read because buffer full. when declare , itialize buffer , print if after that, full of zeros on read doesn't wait recive data socket.

i want recive bytes c server sending char [] message

public class th extends thread  {     public static serversocket sc;     public static byte[] buffer = new byte[256];      public th(){         sc = null;     }      public th (serversocket usersc){         sc = usersc;     }      public void run() {         system.out.println("en la clase th");         socket serveraddr;         boolean condicion = true;         datainputstream in;   int p;      (p = 0; p < 256; p++) system.out.print(buffer[p] + " ");         /*try {             serveraddr = sc.accept();             in = new datainputstream(serveraddr.getinputstream());         }         catch (exception e){             system.err.println("error creando socket");         }*/         try {             system.err.println("-1---------------------------------");             serveraddr = sc.accept();             in = new datainputstream(serveraddr.getinputstream());             system.err.println("0---------------------------------");             system.err.println("1---------------------------------");              in.read(buffer);             (p = 0; p < 256; p++) system.out.print(buffer[p] + " ");             int = in.read(buffer);             if (a < 0) {                exit();             }             (p = 0; p < 256; p++) system.out.print(buffer[p] + " ");             string res = new string(buffer);             system.out.println(res);             system.out.println("no espera");             serveraddr.close();         }         catch (ioexception e){             system.err.println("error creando socket");         }     } } 

i'm executing code , when create byte array called buffer, it's initialized 0

correct, assuming mean 'initialized zeros'.

so when .read() function doesn't wait read because buffer full.

incorrect, indeed , utter nonsense. primitive arrays in java initialized zeroes, suggest make i/o impossible, isn't. suggestion doesn't begin make sense.

when declare , initialize buffer , print after that, full of zeros on read doesn't wait recive data socket.

again nonsense. actual problem read() returning -1, indicating peer has disconnected. ignoring return value.

if positive should use instead of 256 bound print loops. there no guarantee read() fills buffer.

nb serveraddr socket, not 'address'. don't use misleading variable names. first person mislead yourself.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -