I'm making a lab for my university and I'm supposed to used UDP to transfer some arrayList through client to server using sockets. I h开发者_开发百科ave search so much but still I cant manage to make it work.
Till now i have in the client side
ArrayList <Integer> arr = new ArrayList<Integer>();
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
ObjectOutput oo = new ObjectOutputStream(bStream);
//here i add some code to fill the array..
oo.writeObject(arr);
byte [] buf=bStream.toByteArray();
upd.sendPacket(buf, clientSocket); //this is a method for sending packets
And in the server side i got
ByteArrayInputStream baos;
ObjectInputStream oos;
$ this is where implement the code
else if (str.equals("max")) {
System.out.println("waitng for object to come");
upd.receivePacket(serverSocket, receiveData); //here i receive the datagram packet
baos = new ByteArrayInputStream(receiveData);
oos = new ObjectInputStream(baos);
ArrayList<Integer> arr = (ArrayList<Integer>)oos.readObject();
System.out.println(arr);
}
I would really appreciate if you could give me some hint or just explain allitle bit about the logic of how to perform this.
PS. Please don't tell me about using RMI or any other method to do this, I need to do this only using UDP. Thank you
UDP is message-based, not stream-based like TCP. UDP does not guarantee delivery. It does not guarantee that receiver gets the messages (datagrams) in the same order they were sent. You are supposed to partition your data into chunks and sequence them properly, so the receiver can re-assemble the chunks back into whatever is being sent. The exercise is probably a lead-up to how TCP handles all these details :)
Maximum size of UDP datagram is 64K, though all practical applications try to limit packet size to at most the MTU (1500 for Ethernet minus 20 bytes of IPv4 header minus 8 bytes of UDP header = 1472)
That said, you probably want to add a little header to each datagram to indicate the size of the data payload and its order in the application "stream".
I don't work in Java, so I'll refer you to examples for API usage.
The pattern on the server side is something like:
byte[] buf = new byte[1024];
DatagramSocket s = new DatagramSocket(1234);
DatagramPacket p = new DatagramPacket(buf, buf.length);
s.receive(p);
and on the client side:
DatagramSocket c = new DatagramSocket();
InetSocketAddress addr = new InetSocketAddress("hostname",1234);
DatagramPacket newPacket = new DatagramPacket(buf, 0, buf.length, addr);
c.send(newPacket);
Of course you have to care how to encode your data and to make sure it is complete at the receiving side, because UDP does not take care of this! That means you need to implement your own consistency protocol. The sending side can never be sure, if data was received at the other end as well. For transferring data like ArrayLists, i would rather use TCP socket communication. Please also note that the packet size is limited. See DatagramPacket#setSendBufferSize(int size) for details.
精彩评论