![]() |
|
[C#] Asynchronous multi-client/server classes (make rats, loggers, whatever) - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] Asynchronous multi-client/server classes (make rats, loggers, whatever) (/showthread.php?tid=5791826) Pages: Pages
1
2
|
Asynchronous multi-client/server classes (make rats, loggers, whatever) - gigajew - Jan 28, 2018 I've been playing around with Sockets for a while, and after studying Aeonhack's code, I realized that he made some mistakes where he can't receive more than the current buffer size that he had set. So I've created my own (which, it's not perfect - it has a lot of flaws), but it gets the job done. Maybe with some memory leaks, but if you know a thing or two you can fix that easily. With something like this, it would be easy to create your own rat, logger or whatever really. Edit: going to change some things and update this soon. Hang tight Example server: Example output: Code [Server] Server is now listeningSocketClient source: Spoiler (Click to View) SocketServer source: Spoiler (Click to View) RE: Asynchronous multi-client/server classes (make rats, loggers, whatever) - Ninja - Jan 28, 2018 I like what you did, but what if you receive more than 1024 bytes? Then it will not fit in the buffer. And what about all other socket methods you are hiding here? You would be better off inheriting from Socket and overloading the methods. Also, one minor detail: the convention for events and delegates dictates that you should use "object sender" instead. https://msdn.microsoft.com/en-us/library/dd833085(v=vs.95).aspx RE: Asynchronous multi-client/server classes (make rats, loggers, whatever) - gigajew - Jan 28, 2018 It continuously writes to a stream until Socket.Available equals 0. Then it invokes received event, and resets the stream to 0 again for a new transmission. So 1024 is really just an arbitrary value, you can receive more or less. RE: Asynchronous multi-client/server classes (make rats, loggers, whatever) - Ninja - Jan 28, 2018 (Jan 28, 2018 07:01 PM)gigajew Wrote: It continuously writes to a stream until Socket.Available equals 0. Then it invokes received event, and resets the stream to 0 again for a new transmission. So 1024 is really just an arbitrary value, you can receive more or less. Oh, my bad. I didn't read the source fully. Still, it would be better if you would inherit from socket and override the appropriate methods to add events, instead of making a wrapper for it. Or you would have to add all overloads for the methods you have now so they match the socket methods, else you are limiting functionality. EDIT: Just remembered I did something similar as well. I was mainly focussed on making it easier to use the methods asynchronously, though I added events as well. You can find it here: https://hackforums.net/showthread.php?tid=5735967 RE: Asynchronous multi-client/server classes (make rats, loggers, whatever) - BahNahNah - Jan 28, 2018 You dont need Try/catch on endreceve. Just get the SocketError and check its Success. https://msdn.microsoft.com/en-us/library/ms145145(v=vs.110).aspx RE: Asynchronous multi-client/server classes (make rats, loggers, whatever) - gigajew - Jan 28, 2018 (Jan 28, 2018 09:24 PM)BahNahNah Wrote: You dont need Try/catch on endreceve. Just get the SocketError and check its Success. I got a ObjectDisposedException in EndReceive on ServerSocket sometimes RE: Asynchronous multi-client/server classes (make rats, loggers, whatever) - chrizator - Jan 30, 2018 Great share OP, very interesting. If somebody really considers using this for their own project, make sure NOT to create one receive buffer (byte array) for each established connection. Instead, use buffer pooling in order to avoid heap fragmentation due to object pinning. Especially when dealing with hundreds of connections, this becomes essential. There are some open-source projects: github.com/tenor/ServerToolkit RE: Asynchronous multi-client/server classes (make rats, loggers, whatever) - Ninja - Jan 30, 2018 (Jan 30, 2018 12:16 AM)chrizator Wrote: Great share OP, very interesting. If somebody really considers using this for their own project, make sure NOT to create one receive buffer (byte array) for each established connection. Instead, use buffer pooling in order to avoid heap fragmentation due to object pinning. Especially when dealing with hundreds of connections, this becomes essential. I don't know much about buffer pooling and what not, but this is what I found online when I googled it. Quote:C# sharps memory management is actually quite good, so instead of having a pool of buffers, you could just allocate exactly what you need and stick it into a queue. Once you are done with buffer just let the garbage collector handle it. Full answer: https://stackoverflow.com/a/281954 RE: Asynchronous multi-client/server classes (make rats, loggers, whatever) - chrizator - Jan 30, 2018 (Jan 30, 2018 08:37 AM)Ninja Wrote: I don't know much about buffer pooling and what not, but this is what I found online when I googled it. Hm. Seems like opinions differ when talking about buffer pooling. A while ago I read this post: ahuwanya.net/blog/post/Buffer-Pooling-for-NET-Socket-Operations Since then I always used buffer pooling for all of my projects. I'm going to cry if all that work turns out to be unnecessary. 😭 Looking forward to hear other opinions! RE: Asynchronous multi-client/server classes (make rats, loggers, whatever) - Ninja - Jan 30, 2018 (Jan 30, 2018 11:10 AM)chrizator Wrote: Hm. Seems like opinions differ when talking about buffer pooling. I actually found a tutorial earlier when I was researching buffer pooling and stumbled upon this: https://www.wintellect.com/pooling-buffers-for-better-memory-management/ Which uses a class called the BufferManager from the Windows Communication Foundation. RE: Asynchronous multi-client/server classes (make rats, loggers, whatever) - gigajew - Feb 8, 2018 (Jan 28, 2018 09:24 PM)BahNahNah Wrote: You dont need Try/catch on endreceve. Just get the SocketError and check its Success. Thanks I didn’t know about this overload RE: Asynchronous multi-client/server classes (make rats, loggers, whatever) - Ninja - Feb 8, 2018 (Jan 28, 2018 09:52 PM)gigajew Wrote: I got a ObjectDisposedException in EndReceive on ServerSocket sometimes (Feb 8, 2018 05:53 PM)gigajew Wrote: Thanks I didn’t know about this overload 2 different answers to the exact same post. you high? RE: Asynchronous multi-client/server classes (make rats, loggers, whatever) - gigajew - Feb 9, 2018 (Feb 8, 2018 11:10 PM)Ninja Wrote: 2 different answers to the exact same post. you high? I didn’t know what he meant first, and to answer your question; very RE: Asynchronous multi-client/server classes (make rats, loggers, whatever) - michalmonday - Jul 18, 2018 What would be optimal way to implement this code into GUI? The goal would be to press a button which sends a message to all clients or a single specific client. Is there some way to still make it event based and have something like: Code namespace GUI_ServerEdit: I just found this thing: https://www.codeguru.com/csharp/csharp/cs_network/sockets/article.php/c8781/Asynchronous-Socket-Programming-in-C-Part-II.htm#Client1 It has GUI and it's really easy to accomplish what I indended. Idk how it compares with this one and what possible flaws it has but it just seems to work properly during my test. RE: Asynchronous multi-client/server classes (make rats, loggers, whatever) - gigajew - Jul 18, 2018 Ill probably be revamping the code soon, keep your ears and eyes open |