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.

z3nth10n
  • 2,341
  • 2
  • 25
  • 49
  • 4
    Did you ever solve this issue? I'm having the same problem – Swifty Jan 17 '19 at 08:09
  • Yes, using another implementation. That was mine, I only did this to understand Sockets. But I'd recomend you to use some lib. – z3nth10n Jan 17 '19 at 20:09
  • Can you recommend something? – Swifty Jan 18 '19 at 08:23
  • 1
    See this: https://github.com/kerryjiang/SuperSocket or https://github.com/nterry/AwesomeSockets or https://github.com/safakgur/socket-awaitable or https://github.com/kerryjiang/SuperSocket.ClientEngine or https://github.com/kerryjiang/WebSocket4Net or https://github.com/mrousavy/GenericProtocol or https://github.com/lukasz-pyrzyk/XGain. Hope this helps! – z3nth10n Jan 18 '19 at 16:16

1 Answers1

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);
} 
Ruslan
  • 1