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

C# 

Asynchronous multi-client/server classes (make rats, loggers, whatever)

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 listening
[Client] Connected to server
[Server] 127.0.0.1:53060 connected
[Client] Sent 11 bytes to server
[Server] 127.0.0.1:53060 said Hello world
[Server] Sent 19 bytes to 127.0.0.1:53060
[Client] Server said: Hello back to you!!

SocketClient source:
Spoiler (Click to View)

SocketServer source:
Spoiler (Click to View)
[Image: Syr3DlV.png]
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...s.95).aspx
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.
[Image: Syr3DlV.png]
(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
You dont need Try/catch on endreceve. Just get the SocketError and check its Success.
https://msdn.microsoft.com/en-us/library....110).aspx
Lotta blood to let, peace to make, give a fuk about a check
(Jan 28, 2018 09:24 PM)BahNahNah Wrote:  You dont need Try/catch on endreceve. Just get the SocketError and check its Success.
https://msdn.microsoft.com/en-us/library....110).aspx

I got a ObjectDisposedException in EndReceive on ServerSocket sometimes
[Image: Syr3DlV.png]
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
(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.

There are some open-source projects:
github.com/tenor/ServerToolkit

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
(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.
Full answer: stackoverflow.com/a/281954

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!
(Jan 30, 2018 11:10 AM)chrizator Wrote:  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!

I actually found a tutorial earlier when I was researching buffer pooling and stumbled upon this:
https://www.wintellect.com/pooling-buffe...anagement/
Which uses a class called the BufferManager from the Windows Communication Foundation.
(Jan 28, 2018 09:24 PM)BahNahNah Wrote:  You dont need Try/catch on endreceve. Just get the SocketError and check its Success.
https://msdn.microsoft.com/en-us/library....110).aspx

Thanks I didn’t know about this overload
[Image: Syr3DlV.png]
(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?
(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
[Image: Syr3DlV.png]
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_Server
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void button1_Click(object sender, EventArgs e)
       {
           clients[0].Send(Encoding.UTF8.GetBytes("Button 1 was pressed, praise the Lord"));
       }
   }
}


Edit: I just found this thing: https://www.codeguru.com/csharp/csharp/c...tm#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.
Ill probably be revamping the code soon, keep your ears and eyes open
[Image: Syr3DlV.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by SigmaApr 28, 2021 06:50 PM 0 87 Apr 28, 2021 06:50 PM
Last Post: Sigma
[Python] by SympthDec 4, 2020 02:04 AM 15 327 Dec 4, 2020 02:04 AM
Last Post: Sympth
[C#] by PaskowskyMay 9, 2020 11:33 AM 6 428 May 9, 2020 11:33 AM
Last Post: SaketSaket