0

I'm having a problem sending a job (an integer array) from client to server in two different packages over a socket connection. Any ideas please?

I can explain further if my question is not clear enough.

3
  • What problem? And what do you mean by "packages"? Commented Mar 19, 2009 at 21:22
  • Please post an example of the offending code. It will help tremendously in giving you guidance. Commented Mar 19, 2009 at 21:26
  • What do you mean by needing two different packages? Do you mean packets? If so, you should need to worry about the number or size of the packets. Commented Mar 19, 2009 at 22:01

4 Answers 4

4

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry could you be more technical by translating the answer you send into code or pseudocode please?
2

Do you have to send it as an array? 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();

Comments

0

What protocol are you using to send this data over your link? 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,

Comments

-3

using

ArrayList a=new ArrayList(n)    //n represents size

or

List a=new List()

we can send to a server

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.