0

I have few questions what I don't understand in answer Can a TCP c# client receive and send continuously/consecutively without sleep? .

1) Where he defined ShutdownEvent, are need be each defined event in Sender and Receiver?

2) Can someone give full example of how need continue code

else if (_stream.Read(_data, 0, _data.Length) > 0)
{
   // Raise the DataReceived event w/ data...
}

also how and where he defined _data.

3) after somehow I setup _data do need setup event as ShutdownEvent.WaitOne(1) ?

sorry for a lot of questions, thanks in advance

Flegy
  • 47
  • 13
  • It's unfortunate that such an example exists, especially with so many up-votes, as it's really a poor way to approach sockets in .NET. My biggest quibbles with it are the use of `DataAvailable` (which is never needed in efficient, correct network I/O code), and dedicated threads for I/O (a proper async implementation has no need for explicitly running threads, though of course the IOCP thread pool does get used for I/O completion routines). IMHO it would be a waste of time to try to walk you though that example. If you really want to understand it, post a comment there with your question(s). – Peter Duniho Jul 16 '17 at 22:59
  • As it happens, I recently posted a different and IMHO much superior example of a basic socket-based client/server implementation (using a chat server as the example application). It shows how to use asynchronous I/O in the modern C# era, and how to deal with graceful shutdowns and forced-resets/closures, in a completely asynchronous (i.e. "without stopping") way. – Peter Duniho Jul 16 '17 at 22:59
  • It's the best example what I find and trying to implement it but still need more explains to get it fully worked. I am developing client who need be able to send and receive data in same time. But sometimes server sends to client 10-15 time and client need be able to handle in the loop. – Flegy Jul 16 '17 at 23:07
  • Sorry, I meant to include the link to my answer in my previous comment. You can find my example client/server implementation here: https://stackoverflow.com/questions/44924744/c-sharp-multithreading-chat-server-handle-disconnect/44942011#44942011. As for needing to be able to send and receive data at the same time, that's implicitly supported in the code example I've provided. If you need help adapting, of course you can post a question about that. But you need to provide a good [mcve] showing what you've tried, and explain what _specifically_ you are having trouble with. – Peter Duniho Jul 16 '17 at 23:17
  • Tried to add your solution but looks as many references are not allowed because I am coding client side in Unity3D. – Flegy Jul 16 '17 at 23:37
  • You may have to use the older `BeginReceive()`, etc. methods if you're using Unity3d. There might be ways to get more modern C# into Unity3d, but I'm sure it doesn't support it by default. You'd have to carefully configure a project in Visual Studio to target an older .NET version and then use that assembly in your Unity3d project. Probably better to just learn the older "Asynchronous Programming Model" (APM) for .NET, which is based on the `BeginXXX()`/`EndXXX()` pairs of methods for operations. Do note that regardless, the example you started out looking at is a poor choice. – Peter Duniho Jul 16 '17 at 23:45

0 Answers0