Skip to content
Thijs Elenbaas edited this page Jan 6, 2019 · 7 revisions

Socket settings

When setting up a client connection you can use the default socket setting

 var client = new SimplSocketClient();

But you can also inject your own socket creator [^Note that injecting an instantiated socket is not enough: the socket may expire, and a new one may need to made]:

 var client = new SimplSocketClient(() => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp));

Note that this library is not designed to work with UDP, but you are free to try what works for you. You can also change socket properties that are not part of the constructor:

var client = new SimplSocketClient(() => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp){NoDelay = true});

This setting will disable Nagles algorithm. This is not the best example, because the library will disable it anyway, unless specificied differently in the constructor (see below)

The server socket can be set up in the same manner

 var server = new SimplSocketServer(() => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))

When sending objects using SimplMessage the connection will be exactly the same

 var client = new SimplMessageClient(() => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp){ NoDelay = true})

So far, we looked only at the socket settings, but SimplSocket itself allows for some tuning through settings:

var client = new SimplSocketClient(
    () => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
    keepAlive           :true,
    messageBufferSize   :65536,
    communicationTimeout:10000,
    maxMessageSize      :10485760,
    useNagleAlgorithm   :false);

This allows you to change some important values

  • keepAlive, will send a packet every second, to make sure the socket does not get closed down. It functions also as a check if the client is still connected to the server, triggering an event if it gets disconnected
  • messageBufferSize is the size in which every message gets broken down and send over. Tuning the setting based on the sizes of your message and network properties may allow you to get performance gains
  • communicationTimeout sets the time that connected server or client can wait to respond before throwing an exception
  • maxMessageSize sets the maximum size of any message that can be sent or received.
  • useNagleAlgorithm dermines if the NoDelay property on the socket gets set or not.

Auto discovery

The simplest way to connect a client to server, is by auto discovery

 client.AutoConnect();

Of course, the server needs an IP adress and port

 server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));

The example above assumes that the server we want to connect to has the default name. If you want to use another name because, for example you are running multiple server, you can set another name.

 client.AutoConnect("ServerName");`
and
```csharp
 server.Listen(new IPEndPoint(IPAddress.Loopback, 5000),"ServerName");

IP based connection

Of course, you can connect using the server IP address

 var result = client.Connect(new IPEndPoint(IPAddress.Loopback, 5000));

Alternatively, if you don't want to catch exceptions, you can use

 var result = client.SafeConnect(new IPEndPoint(IPAddress.Loopback, 5000));

Connection status

SimplSockets and SimplMessage will both allow you to trigger events when the client connects

client.Connected += (s, e) =>
{ Console.WriteLine($"The client has connected to {e.IPEndPoint.Address}!"); };

the client disconnects

client.Disconnected += (s, e) =>
{ Console.WriteLine("The client has disconnected!"); };

or the clients tries to set up a connection

client.ConnectionAttempt += (s, e) =>
{ Console.WriteLine($"The client is trying to connect to {e.IPEndPoint.Address}!"); };

Check connection

Rather than using the events it can be easier to check if the client is connected

 bool isConnected = client.IsConnected()

Sometimes you may want to wait for a connection. This can be done synchronously (except for in UWP)

 client.WaitForConnection();

or it can be done asynchronously

 await client.WaitForConnectionAsync();

Example

These features are shown in more detail in this example

Clone this wiki locally