• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a question about I/o and socket

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic