Passing primitives through sockets

I've just started with Java networking, and I have a few questions. Say I've sent a packet, and I know that the first two bytes in that packet combine to make an int. Is this the best way of sending and recieving the data, or is there an easier way?

Client:

publicclass ClientPrim{

Socket socket;

BufferedOutputStream out;

public ClientPrim(){

try{

socket =new Socket("localhost", 9090);

out =new BufferedOutputStream(socket.getOutputStream());

int anInt = 30000;

int anotherInt = 60000;

byte[] bytes =newbyte[4];

bytes[0] = (byte) (anInt>>8);

bytes[1] = (byte) (anInt&0xff);

bytes[2] = (byte) (anotherInt>>8);

bytes[3] = (byte) (anotherInt&0xff);

out.write(bytes, 0, bytes.length);

out.flush();

out.close();

}catch (Exception e){

e.printStackTrace();

}

}

publicstaticvoid main(String [] args){

ClientPrim c =new ClientPrim();

}

}

Server:

publicclass ServPrim{

private ServerSocket server;

public ServPrim(){

try{

server =new ServerSocket(9090);

while (true){

Socket con = server.accept();

BufferedInputStream in =new BufferedInputStream(con.getInputStream());

byte[] bytes =newbyte[4];

in.read(bytes, 0, bytes.length);

int firstBig = bytes[0]>=0?bytes[0]:bytes[0]+256;

int firstSmall = bytes[1]>=0?bytes[1]:bytes[1]+256;

int secondBig = bytes[2]>=0?bytes[2]:bytes[2]+256;

int secondSmall = bytes[3]>=0?bytes[3]:bytes[3]+256;

int firstInt = (firstBig<<8) + firstSmall;

int secondInt = (secondBig<<8) + secondSmall;

System.out.println(firstInt+" "+secondInt);

con.close();

}

}

catch (IOException e){

e.printStackTrace();

}

}

publicstaticvoid main(String[] args){

ServPrim s =new ServPrim();

}

}

With all the bit operations it seems very messy, surely theres an easier way? I've also managed to send the data via serialized wrapper classes, but will this have a large effect on the speed? I'd rather put in a bit of extra effort and have a fast program

Thanks

[4324 byte] By [vyper_uka] at [2007-10-2 0:26:56]
# 1

Take a look at java.io.DataOutputStream and java.io.DataInputStream. Nice toys like readInt() in there.

Careful with read(): if you try to read() four bytes you can get one, two, three, or four bytes. TCP/IP can split up and combine writes into packets any way it feels like. DataInputStream has readFully() which will loop doing read() until it gets the requested amount of data.

There is an idiom to do byte to unsigned byte conversion:

byte b;

int n = b & 0xff;

sjasjaa at 2007-7-15 16:41:16 > top of Java-index,Archived Forums,Socket Programming...
# 2
Oh, and don't worry about performance of simple operations like shifting. E.g. 100 Mbit ethernet delivers at most 10 Mbytes/second (and that speed is difficult to sustain in real life.) Compare 10 million with the 1-2 billion instructions your CPU can execute per second.
sjasjaa at 2007-7-15 16:41:16 > top of Java-index,Archived Forums,Socket Programming...
# 3
Thats great, exactly what I was looking for! Thanks a lot :)
vyper_uka at 2007-7-15 16:41:16 > top of Java-index,Archived Forums,Socket Programming...