c# - How to handle reading and writing to the same socket -


i establish connection socket server , start reading , writing data enormously. exception occurs immediately:

an exception of type 'system.invalidoperationexception' occurred in mscorlib.dll not handled in user code

additional information: stream in use previous operation on stream.

how can detect

_reader.readlineasync(); 

is receiving data , should wait writing , vice versa?

this class used client sends received data server server.

public class tcpclientworker2  {     private tcpclient _client;     private thread _t;     private networkstream _networkstream;     private streamwriter _writer;     private streamreader _reader;     public tcpclientworker2()     {         var iphostinfo = dns.resolve(dns.gethostname());         var ipaddress = iphostinfo.addresslist[0];          _client = new tcpclient();         _client.connect(ipaddress, 10000);         _t = new thread(run);     }      public async void run()     {         _networkstream = _client.getstream();         _reader = new streamreader(_networkstream);         _writer = new streamwriter(_networkstream) { autoflush = true };         while (true)         {             var data = await _reader.readlineasync();             //perform callback (send data server)         }     }      public void start()     {         _t.start();     }     public async task send(string text)     {         await _writer.writelineasync(text);     } } 

from source confirmation : https://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream%28v=vs.110%29.aspx?f=255&mspperror=-2147217396

read , write operations can performed simultaneously on instance of networkstream class without need synchronization. long there 1 unique thread write operations , 1 unique thread read operations, there no cross-interference between read , write threads , no synchronization required.

we can see in code there 1 thread reading, there no confirmation in code there 1 thread writing. part should investigate further.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -