6

I'm using async Socket to make connections between client and server using MSDN Docs:

https://msdn.microsoft.com/en-us/library/fx6588te(v=vs.100).aspx

https://msdn.microsoft.com/en-us/library/bew39x2a(v=vs.100).aspx

I started with Synchronous and later with Asynchronous. After completed my first application successfully, I started to search in GitHub repositories with more code and more explanations. I found this:

https://github.com/perrybutler/csharpsockets

And it's okey, it work, but it has a memory leak, and I started Profiling, I found this:

enter image description here

(Possible memory leak)

enter image description here

enter image description here

enter image description here

(With Cycle Detected statments everywhere. Idk what they mean. I suppose that the application or the GC forced it to stop and marked it as a Cycle).

I think something is wrong, because if you send like 1MB of data, the memory should only increase by 1MB, but later decrease. But this is not happening. I started Disposing everything without causing an Exception, but this didn't help much.

I started searching this in StackOverflow and in other places, and I found this:

.NET Does NOT Have Reliable Asynchronouos Socket Communication?

However, I don't really understand what is being discussed there.

I also found this:

https://www.codeproject.com/Articles/83102/C-SocketAsyncEventArgs-High-Performance-Socket-Cod

I read that the Sockets needs to confirm when it already send the data between the BeginSend/EndSend & BeginReceive/EndReceive because if not, it will wait to this, and start increasing the memory (until Cycle Detected?)

Maybe with the CodeProject I shared I should get this? I don't know, where I have to start.

I need a guide for this. I will try to Profile the CodeProject.

4

1 Answer 1

0

I had the same problem, my wrong code:

NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);

Fixed code:

using (NetworkStream stream = client.GetStream())
{
    stream.Write(data, 0, data.Length);
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.