3

Is it possible to have a server and a client with 1 socket connection and send data in both directions at the same time? I mean both the server and the client reads and writes on the same time (2 threads in each process)

Edit: I need "true" two-way communications and not the request / response kind of communication. Both the client and the server must be able to write at the same time.

Edit2: God Dame! It works. Silly me I had a deadlock between client/server because of a write in the reader thread! :) Thanks

Stig
  • 1,974
  • 2
  • 23
  • 50
  • Not completely sure if this is what you are asking, but it is at least related and might be of interest to you: [another java socket full duplex questio](http://stackoverflow.com/questions/6265731/do-java-sockets-support-full-duplex) – Mark Wilkins Dec 21 '11 at 20:37
  • do you have example how to do it. I need such things. – Diyko Jul 01 '14 at 15:04

2 Answers2

1

Yes, it seems use Threads each for sending and receiving on both sides. So one socket connection can do that.

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
1

TCP connection is a full duplex byte stream, so yes, you can read from and write to the same socket at the same time, though you certainly don't have to resort to threads to do two-way communication.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • 1
    I need "true" two-way communications and not the request / response kind of communication. Both the client and the server must be able to write at the same time. – Stig Dec 21 '11 at 20:58
  • 1
    Without threads, you cann't achieve full-duplex functionality. Either you can send or you can wait for a response to come. You cann't do both the things together. So without Threads it will be half duplex. One can either send or receive (or wait to receive) at any given time. – nIcE cOw Dec 21 '11 at 21:05
  • I would run it threaded anyway. Also there will be a problem interleaving when there are nothing to read (though this could be handled with timeouts) – Stig Dec 21 '11 at 21:18
  • Sigh ... kids these days ... your sockets don't have to blocking, and people wrote many high-performance network apps without threads. – Nikolai Fetissov Dec 21 '11 at 21:31