How do I send and receive an integer array from client to server in Java socket programming?

To answer the question in your title, I would wrap the SocketOutputStream in a BufferedOutputStream in a DataOutputStream and use the latter's writeInt() method repeatedly. Or you could use ObjectOutputStream in place of DataOutputStream and serialize the array: objOutStream. WriteObject(theArray) To read it in again on the other end, just wrap the SocketInputStream in (1) a DataInputStream and use readInt() repeatedly, or (2) a ObjectInputStream and use readObject() (If you don't have to interoperate with other languages Object*Stream is easier on you).

To answer the question in your title, I would wrap the SocketOutputStream in a BufferedOutputStream in a DataOutputStream and use the latter's writeInt() method repeatedly. Or you could use ObjectOutputStream in place of DataOutputStream, and serialize the array: objOutStream. WriteObject(theArray).

To read it in again on the other end, just wrap the SocketInputStream in (1) a DataInputStream and use readInt() repeatedly, or (2) a ObjectInputStream and use readObject(). (If you don't have to interoperate with other languages, Object*Stream is easier on you).

It complicates the whole process. Why not wrap it in a Collection or some sort of List?I. E: ObjectOutputStream oos = new ObjectOutputStream(...); oos.

WriteObject(integerCollection); ObjectInputStream ois = new ObjectInputStream(...); Collection integerCollection = (Collection)ois.readObject().

You can wrap your array in an object that can be serialized into an output stream. TCP: Things should be pretty simple in this case. The transport layer will take care of fragmenting your object and getting it right at the other end of the link.

UDP: Things can get a little bit complicated; If the object you are trying to serialize is bigger than the UDP buffers (in terms of bytes) then you might not be able to get the data through. In this situation, you can send your data in chunks that are smaller than the default UDP buffer size.Regards.

Using ArrayList a=new ArrayList(n) //n represents size or List a=new List() we can send to a server.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions