0

I am currently working on a server-client communication. On server-side, I have a Thread listening continuously for any incoming data from the client.

while ((foo = (Foo) objectInputSteam.readObject()) != null) {...}

Sometimes I also need to send data from the server to the client. (Not as a direct response.) After sending some data to the client over the same socket, an exception will be thrown once new data reaches the server (0xAC). As far as I know, this happens because the inputreader is reading while sending data over the same socket. Is there any way to interrupt the listening thread while sending the data to the client or do I need to create a second socket on a different port for outgoing traffic?

Marvin-wtt
  • 449
  • 6
  • 16
  • 1
    You might consider moving to a more event-driven approach, rather than trying to block. For example, [this answer provides some discussion](https://stackoverflow.com/a/5970109/1277259) – KevinO Nov 15 '18 at 16:13
  • "As far as I know, this happens because the inputreader is reading while sending data over the same socket" - what are you basing that on? – davmac Nov 15 '18 at 16:22
  • I based the reason for the problem on this post: https://stackoverflow.com/a/2395269/8372579 – Marvin-wtt Nov 15 '18 at 16:26
  • @MarvinX that post does not say that there is any issue with reading while sending data over the same socket. It says that an `ObjectInputStream` expects its input to come from a single `ObjectOutputStream`. – davmac Nov 15 '18 at 16:39
  • So the actual problem is the client sending from different ObjectOutoutStreans? – Marvin-wtt Nov 15 '18 at 16:46
  • @MarvinX You can only wrap an OutputStream once safely esp with ObjectOutputStream. If you are trying to wrap it more than once, this won't work for sockets, files or any other stream. – Peter Lawrey Nov 15 '18 at 16:55

0 Answers0