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

C# 

eSock - Socket wrapper

Im still learning sockets so this is definitely not perfect.

Currently supports sending:
-uShort
-Integer
-String
-Byte array
-Boolean

Wrapper: Pastebin

Simple Usage

Server:
Code
static void Main(string[] args)
{
    eSocket.Server _server = new eSocket.Server(1337);
    _server.DataRetrievedCallback += _server_DataRetrievedCallback;
    if (_server.Start())
      Console.WriteLine("Server staretd");
    else
      Console.WriteLine("Server failed to start");
    while (true)
      Console.ReadLine();
}

static void _server_DataRetrievedCallback(eSocket.Server.eSockClient c, eSocket.PacketStructure packet, eSocket.Server.PacketError er)
{
    Console.WriteLine("Message: {0}", new Message(packet.PacketBuffer).getMessage());
}

class Message : eSocket.PacketStructure
{
    public Message(string message)
      :base(message.Length, 0)
    {
      WriteString(4, message, Encoding.UTF8);
    }
    public Message(byte[] pack)
      :base(pack)
    { }
    public string getMessage()
    {
      return ReadString(4, Length - 4, Encoding.UTF8);
    }
}

Client:
Code
static void Main(string[] args)
{
    Console.WriteLine("Connecting...");
    eSocket.Client _client = new eSocket.Client();
    if (!_client.Connect("127.0.0.1", 1337))
    {
      Console.WriteLine("Failed to connect");
      Console.ReadKey();
      return;
    }
    Console.WriteLine("Connected!");
    while(true)
    {
      string inp = Console.ReadLine();
      _client.Send(new Message(inp));
    }
}

class Message : eSocket.PacketStructure
{
    public Message(string message)
      :base(message.Length, 0)
    {
      WriteString(4, message, Encoding.UTF8);
    }
    public Message(byte[] pack)
      :base(pack)
    { }
    public string getMessage()
    {
      return ReadString(4, Length - 4, Encoding.UTF8);
    }
}
Lotta blood to let, peace to make, give a fuk about a check
I've always loved your work!

I just started so don't know too much :(, but noticed your ClientDisconnectCallback is null when catching (Socket > Server> RetrieveCallback)
This error is caused by disconnecting the client and having the server still open.

Adding a check fixes it, but might be something to look into
Code
if(ClientDisconnectCallback != null)
    ClientDisconnectCallback(new eSockClient(cSock), SE);
(May 23, 2015 07:55 AM)ShittyAdvice Wrote:  I've always loved your work!

I just started so don't know too much :(, but noticed your ClientDisconnectCallback is null when catching (Socket > Server> RetrieveCallback)
This error is caused by disconnecting the client and having the server still open.

Adding a check fixes it, but might be something to look into
Code
if(ClientDisconnectCallback != null)
    ClientDisconnectCallback(new eSockClient(cSock), SE);

Thanks, i must have missed that one.
Lotta blood to let, peace to make, give a fuk about a check
(May 23, 2015 02:57 PM)BahNahNah Wrote:  Thanks, i must have missed that one.

You missed "that one"?

DEBUG MEMBERS DON'T MISS ANYTHING

I AM INPUTTING FOR YOUR IMMEDIATE REMOVAL
[Image: jlscvfi.png]
(May 23, 2015 03:01 PM)NETExplorer Wrote:  You missed "that one"?

DEBUG MEMBERS DON'T MISS ANYTHING

I AM INPUTTING FOR YOUR IMMEDIATE REMOVAL

Lucky i missed it before i was in debug xD
Lotta blood to let, peace to make, give a fuk about a check
You still have the problem related to the buffer being over-written if too many clients connect at once, as discussed here

An example implementation of a Socket Server which does not have this problem is shown in the MSDN example that shows how to use SocketAsyncEventArgs. (It uses the BufferManager class)
I refactored your code a bit and fixing some naming convention issues.

pastebin.com/VPi1RfJd

Although you are not doing anything with bufferSize in public Server(int port, int bufferSize = 1024, bool structured = true)

Did you just forget about it?
(May 23, 2015 03:49 PM)Serux Wrote:  I refactored your code a bit and fixing some naming convention issues.

pastebin.com/VPi1RfJd

Although you are not doing anything with bufferSize in public Server(int port, int bufferSize = 1024, bool structured = true)

Did you just forget about it?

Yeah, they where initially split up into septate functions.


(May 23, 2015 03:27 PM)Nikey646 Wrote:  You still have the problem related to the buffer being over-written if too many clients connect at once, as discussed here

-snip-

So for each new connection, the buffermanager needs to be recreated?
Code
m_bufferManager = new BufferManager(receiveBufferSize * [b]numConnections[/b] * opsToPreAlloc, receiveBufferSize);

Is it a high possibility that the buffer will be overwritten?

possable solution?:
Make a new class that contains a socket and a buffer for each client.
Create a new instance of the class in the AcceptCallback for each new client that connects
pass this class through to the retrieveCallback, using a seprate buffer for each client

Would this be a viable option?
Lotta blood to let, peace to make, give a fuk about a check
(May 23, 2015 04:07 PM)BahNahNah Wrote:  Yeah, they where initially split up into septate functions.



So for each new connection, the buffermanager needs to be recreated?
Code
m_bufferManager = new BufferManager(receiveBufferSize * [b]numConnections[/b] * opsToPreAlloc, receiveBufferSize);

Is it a high possibility that the buffer will be overwritten?

possable solution?:
Make a new class that contains a socket and a buffer for each client.
Create a new instance of the class in the AcceptCallback for each new client that connects
pass this class through to the retrieveCallback, using a seprate buffer for each client

Would this be a viable option?

I'm not sure how exactly the BufferManager works, but i do know when creating the BufferManager, you should do it once inside of the Server's Constructor, and the size of the BufferManager should be BufferSize MaxConnections Operations, with the last one being for Read/Write (so 2), and the second parameter will be how large the BufferSize should be. The downside of this approach is that a static number of clients can connect. So if the BufferManager only has room for 20 clients, then no more then 20 clients can be connected at once.

Personally, i think your suggested solution is the easier way to go, while still being maintainable, and infact one of the other solutions MSDN has in an example is exactly that. As Shown here.
(May 23, 2015 05:20 PM)Nikey646 Wrote:  I'm not sure how exactly the BufferManager works, but i do know when creating the BufferManager, you should do it once inside of the Server's Constructor

-snip-

Thanks for the feedback.

I expanded the eSockClient class to have a buffer and an instance is now created when the server accepts a socket, so each socket has a separate buffer now.
Lotta blood to let, peace to make, give a fuk about a check


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by NullSystemDec 2, 2019 08:43 AM 1 237 Dec 2, 2019 08:43 AM
Last Post: HFDone
[C#] by JUSTINJun 8, 2019 07:39 PM 28 1,005 Jun 8, 2019 07:39 PM
Last Post: doxerhacker
[Python] by VassiliosMar 30, 2019 07:33 AM 3 201 Mar 30, 2019 07:33 AM
Last Post: authorSS