At its core networking is asynchronous and that behavior is, to an extent, reflected in the implementation of NetworkComms.Net. In our continuing endeavour to provide the most flexible .net network library on the planet we have also provided features which make the synchronous request for data and the corresponding response really straight forward.

In the following code we send and receive a CustomObject class. For more information on using custom objects please see this tutorial.

On the client side you can request data synchronously from a server as follows:

try
{
    //Create a connectionInfo object that specifies the target server
    //This line assumes the server is on the local machine - 127.0.0.1
    //This line also assumes that the server is listening on port 10000 (check this is the case).
    ConnectionInfo connectionInfo = new ConnectionInfo("127.0.0.1", 10000);

    //Get a connection with the specified connectionInfo
    TCPConnection serverConnection = TCPConnection.GetConnection(connectionInfo);

    //Or we could also create the connection using UDP?
    //UDPConnection serverConnection = UDPConnection.GetConnection(connectionInfo, UDPOptions.None);

    //Send a packet of type RequestCustomObject and wait synchronously until
    //a. The server returns the requested CustomObjectReply packet continaing a CustomObject
    //b. We have waited for atleast 1000ms at which point we timeout and throw an ExpectedReturnTimeoutException
    //Note: There are 6 overrides for SendReceiveObject that should provide the whole range of required features
    //The override we are using here just requests some data without providing an object to send
    CustomObject myCustomObject = serverConnection.SendReceiveObject<CustomObject>("RequestCustomObject", "CustomObjectReply", 1000);

    //Perform further operations on the received object here
}
catch (ExpectedReturnTimeoutException)
{
    //We can decide what to do here if the synchronous send and receive timed out after the specified 1000ms
}

On the server side the configuration is identical regardless of whether the client is making a synchronous or asynchronous request:

//Append a packet handler that will get executed when the server receives a "RequestCustomObject" packetType.
//Note: The expected incoming object type here is irrelevant because the client is not providing an object
//If the client does not provide an object when sending the incoming object is set to GetDefault(Type).
NetworkComms.AppendGlobalIncomingPacketHandler<int>("RequestCustomObject", (packetHeader, connection, input) =>
    {
        //For this short example we just reply with a new CustomObject
        CustomObject myCustomObject = new CustomObject();

        //When this is received by the client it will complete the synchronous request
        connection.SendObject("CustomObjectReply", myCustomObject);
    });

//Start listening for incoming TCP connections
//The 0 (zero) port number is used to select a random port. You can use a fixed port by changing this number.
//If the fixed port is unavailable however you will have to handle a possible CommsSetupShutdownException
Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 0));
//Or if we want to use UDP
//Connection.StartListening(ConnectionType.UDP, new IPEndPoint(IPAddress.Any, 0));

Hopefully this short tutorial demonstrates how you can easily make synchronous network requests for data using our network library. If you found this tutorial useful, have any feedback or questions please leave a comment or post on our forum.