1

I'm trying to communicate through sockets using TCP. The data that needs to be sent is a drawing, whilst it is being drawn. So the option would be to send all the points, or only shapes (series of points).

Since it would be nice to have it being drawn instantly, sending points seems better. It's only for local use, so a lot of data shouldn't be an issue. Now the issue I'm having is understanding how exactly the socket works. Currently my code is as follows:

    while(true){

        try {
            Thread.sleep(10);
         }
         catch (InterruptedException e) {}

        switch(connectionStatus){
        case CONNECTED:
            if(isHost){
                try {
                    oos.writeObject(myObject);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                try {
                    myObject = (myObjectType) ois.readObject();
                    mainFrame.repaint();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            break;
      }
}

But needless to say, this seems rather inefficiënt as it's running constantly. Is there a way to only write to the ObjectOutputStream (oos) when new data is there? I guess to read you have to poll though. Does reading also clear the ObjectOutputStream?

Edit

To be clear: I want to send multiple Point-objects through a socket. So every time a Point gets added to eg the server, it should send this point to the client.

Now, what do I need to put inside the oos.writeObject()? The single Point, or a List of Points? And how are they retrieved from the ois.readObject()?

I'm a bit confused, because the writing to the ObjectOutputStream could be fast or slow. Se reading the ObjectInputStream - the way I see it - would or cause a big delay (if it reads a value every ~15ms and points get added faster than that) or cause lots of lag.

3 Answers 3

1

Is there a way to only write to the ObjectOutputStream (oos) when new data is there?

Yes. Whenever the user draws something, push data down the ObjectInputStream.

I guess to read you have to poll though.

That is incorrect. Typically, reading from an open stream is a blocking operation: if you attempt to read something and there's nothing there, the read method will simply block until new data is available.

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

3 Comments

So if I'm understanding correctly, eg when a new Point gets added, I just push that Point onto the ObjectOutputStream, using oos.writeObject(new Point(20, 20))? What happens if multiple Points have been written to the oos before the receiver has read them?
Assuming you are correctly handling multithreading (if there's any multithreading in your program at all), then yes, you just push an object down the stream. If multiple points have been written (serially! you must write objects to the stream in sequence, rather than parallel), they will be read in sequence once the receiver starts reading them. At the meantime, data will accumulate in a buffer between the sender and receiver; that's implemented by the TCP/IP stack and, normally, you wouldn't have to worry about it.
Not sure about the multithreading part... It's just from one 'program' to another, so I don't think I need it. Nevertheless I just pushed the Points to the ObjectOutputStream at the sender's desire, and used the while(true)-part from above for reading out the ObjectInputStream and it seems to work! Though instead of using Points I made a Class delivering start- and endpoints of the line. SO for now I'm settled and it seems to work without lag, thanks!
0
  1. For writing, you need to employ threading and synchronization technique in order to write only when data is available. One thread to notify that new data has become available, another to wait and be notified and continues execution when it is told that data has come;
  2. Reading doesn't clear ObjectOutputStream. In fact, you can use two threads to handle input and output streams concurrently.
  3. Reading an object is an synchronous operation, meaning your thread waits until the object is ready.

Comments

0

I wrote a library (which you can find on maven) that will take away some the complexity of implementing threading and synchronization yourself:

https://github.com/xtrinch/socket-live

Consists of three main components (which later result into three running threads):

SocketConnection.java: main thread, run by the user of the library, which makes sure the connection is always open

SocketRead.java: read thread which continuously attempts to read incoming messages if any

SocketWrite.java: write thread which writes any messages in write queue to socket

You also have the option to disable the read thread, if you don't need it.

Library will make sure the connection stays open at all times, reconnect upon being closed, and it's been battle tested :)

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.