Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

TcpClient/Listener Source Review.

Hi. Just looking for someone to review my code and give some criticism. Everything works fine as far as I can tell, just wondering if I've missed anything.

http://pastebin.com/5yw60P5z

Couldn't paste the code here. Was getting a cloudflare blocking error :(
Just skimming over it but I see a few issues:

When you accept a new connection and begin reading data for it, you're creating a new thread which is going to tank performance and the max number of connections very quickly. You should really be using ReadAsync here which will handle multiple clients on fewer threads.

Code
Thread DataThread = new Thread(tThread);
DataThread.Start();

Additionally, I see you are using ReadAsync in the DataThread, so you're basically doubling up on thread resources which is going to use a crazy amount of resources. You're also handling all data as ASCII strings which is an issue for a number of reasons, you should consider binary serialization or if you're set on strings then UTF8 at least.

Code
DataLength = await CStream.ReadAsync(Cl.Buffer, 0, Cl.Buffer.Length);
                    Cl.DataQueString.Add(Encoding.ASCII.GetString(Cl.Buffer, 0, DataLength));
                    Cl.DataQueBuffer.Add(Cl.Buffer);
                    IncomingData((object)Cl, null);

You also have a number of Thread.Sleeps which is going to be a bottleneck once you start sending a lot of data.


I have to go for now, but I'll continue looking at it later - saw some issues with your ID generation, messbagebox in connection event, etc. which I'll address when I return.
In other words, Fire+Rain, you .NET script kiddie, use the IOCP Model, this is what is meant by using asynchronous sockets in .NET...
It utilizes thread pools and usermode callback from the kernel. It's the perfect threading model there is for windows.
Also, consider posting your code on github next time, easier than a big ass pastebin file.
You're mixing raw threading with the TPL design paradigm which is a terrible idea, in addition to the fact that you're using the Random class wrong, not keeping a reference to your raw threads for management, promoting boxing/unboxing for no reason, among other things.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by elmagekOct 24, 2021 02:50 PM 7 292 Oct 24, 2021 02:50 PM
Last Post: #Kat
[C#] by asdoxOct 13, 2020 03:02 PM 2 388 Oct 13, 2020 03:02 PM
Last Post: REEND
[Shell] by TravelersAug 13, 2017 05:10 PM 6 156 Aug 13, 2017 05:10 PM
Last Post: Travelers