• Post Reply Bookmark Topic Watch Topic
  • New Topic

a question about I/o and socket  RSS feed

 
Blues Yang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Client end write 4 int bytes
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
int a = 2;
out.writeInt(a);
after Server end read into array, why it turn into one byte???
DataInputStream in = new DataInputStream(Socket.getInputStream());
int length2;
byte[] data1 = new byte[4000];
length2 = in.read(data1,0,4);
System.out.println(length2);
Why length2 is 1???
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
did u try printing data1[0],data1[1], data1[2], data1[3]??
try it and see if you really got only one byte or not? if you did got only one byte then the output of your code is right else there is something missing...
try to put while loop like...
int i=-1;
int j=0;
while ( (i=in.read(data1,j,4)) != -1 ) {
System.out.println(i);
j+=4;
}
regards
maulin.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!