C# Server, Client, Duplex mode

Markus Freitag 3,786 Reputation points
2023-01-28T10:20:44.1266667+00:00

Hello, I'm looking for a good example to exchange this between server and client.

Both should be able to make unsolicited inquiries. Duplex operation.

Who can help me, who can post a good example?

IP="127.0.0.1"  Port="8029"  Timeout=5000
<root>
   <message_request id="2123" type="process" />
</root>
<root>
   <message_response id="2123" type="coloring of the product 2123" />
</root>

Reconnect on disconnect Error message after 5 failed attempts

Thanks for your help!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,292 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dimple Rane 906 Reputation points
    2023-01-28T11:41:37.14+00:00

    here is an example of a basic duplex communication setup using the WCF (Windows Communication Foundation) framework in C#:

    Server-side:

    c#

    using System;
    using System.ServiceModel;
    [ServiceContract(CallbackContract = typeof(IServerCallback))]
    interface IServer
    {
        [OperationContract]
        void ProcessRequest(string request);
    }
    interface IServerCallback
    {
        [OperationContract]
        void SendResponse(string response);
    }
    class Server : IServer
    {
        public void ProcessRequest(string request)
        {
            // Do some processing on the request
            string response = "Coloring of the product 2123";
            // Send the response back to the client
            var callback = OperationContext.Current.GetCallbackChannel<IServerCallback>();
            callback.SendResponse(response);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Set up the server
            var binding = new NetTcpBinding();
            var address = new Uri("net.tcp://127.0.0.1:8029/Server");
            var host = new ServiceHost(typeof(Server));
            host.AddServiceEndpoint(typeof(IServer), binding, address);
            host.Open();
            Console.WriteLine("Server running...");
            Console.ReadLine();
        }
    }
    

    Client-side:

    C#

    using System;
    using System.ServiceModel;
    
    [CallbackBehavior(UseSynchronizationContext = false)]
    class Client : IServerCallback
    {
        public void SendResponse(string response)
        {
            // Handle the response
            Console.WriteLine("Response: " + response);
        }
    
        static void Main(string[] args)
        {
            // Set up the client
            var binding = new NetTcpBinding();
            var address = new Uri("net.tcp://127.0.0.1:8029/Server");
            var factory = new DuplexChannelFactory<IServer>(new Client(), binding, address);
            var client = factory.CreateChannel();
    
            // Send a request to the server
            client.ProcessRequest("Process request for product 2123");
    
            Console.ReadLine();
        }
    }