Skip to content

A minimalist wrapper around System.Net.Sockets.Socket.

Notifications You must be signed in to change notification settings

tallesl/net-Socket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logo

Socket

A minimalist wrapper around a .NET's Stream IPv4 TCP Socket. Everything is synchronous.

Disclaimer: I've used this library only on prototypes and small use cases, it never reached production. Even though the code is fairly small, consider that while using it.

Usage

An stupid echo server:

using (var listener = new SocketListener(1337)) // Start listening
{
    for (;;)
    {
        using (var remote = listener.Accept()) // Accepts a connection (blocks execution)
        {
            var data = remote.Receive(); // Receives data (blocks execution)
            remote.Send(data); // Sends the received data back
        }
    }
}

And its client:

using (var socket = new ConnectedSocket("127.0.0.1", 1337)) // Connects to 127.0.0.1 on port 1337
{
    socket.Send("Hello world!"); // Sends some data
    var data = socket.Receive(); // Receives some data back (blocks execution)
}