Skip to content

Services Over TCP

tylerje edited this page Oct 23, 2014 · 1 revision

From Wikipedia

The Transmission Control Protocol (TCP) is one of the core protocols of the Internet protocol suite (IP), and is so common that the entire suite is often called TCP/IP. TCP provides reliable, ordered, error-checked delivery of a stream of octets between programs running on computers connected to a local area network, intranet or the public Internet. It resides at the transport layer.

Most communication between computers over the Internet or wide area network (WAN) and within the local area network (LAN) happens over TCP over IP. Many protocols operate over TCP/IP, including SMTP (email) and HTTP (web). Every one of those protocols generally has some overhead costs that are eliminated by ServiceWire. Instead, ServiceWire writes and reads directly to the TCP socket opened between the client and the server.

Using TCP sockets directly from .NET can be tricky. ServiceWire does the tricky part for you.

Your use of TCP with ServiceWire

Here's the code you would write to use create, host and use a service over TCP.

In a shared assemly--your interface and data contracts

 public interface IMyTester
 {
    Guid GetId(string source, double weight, int quantity);
    TestResponse Get(Guid id, string label, double weight, out int quantity);
    List<string> GetItems(Guid id, int[] vals);
 }
 
 [Serializable]
 public struct TestResponse
 {
    public Guid Id { get; set; }
    public string Label { get; set; }
    public long Quantity { get; set; }
 }

On the server--your interface implementation and hosting

 public class MyTester : IMyTester
 {
    private string longLabel = string.Empty;
    private const int totalKilobytes = 140;
    private Random rand = new Random(DateTime.Now.Millisecond);
 
    public MyTester()
    {
       var sb = new StringBuilder();
       for (int i = 0; i < totalKilobytes; i++)
       {
          for (int k = 0; k < 1024; k++) sb.Append(((char)rand.Next(32, 126)));
       }
       longLabel = sb.ToString();
    }
 
 
    public Guid GetId(string source, double weight, int quantity)
    {
       return Guid.NewGuid();
    }
 
    public TestResponse Get(Guid id, string label, double weight, out int quantity)
    {
       quantity = 44;
       return new TestResponse { Id = id, Label = longLabel, Quantity = quantity };
    }
 
    public List<string> GetItems(Guid id, int[] vals)
    {
       var list = new List<string>();
       list.Add("42");
       list.Add(id.ToString());
       list.Add("MyTest");
       list.Add(longLabel);
       return list;
    }
 }
 
 static void Main(string[] args)
 {
    // logger and stats are optional 
    // there is a null implementation by default
    var logger = new Logger(logLevel: LogLevel.Debug);
    var stats = new Stats();
 
    var port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]);
    var ipEndpoint = new IPEndPoint(IPAddress.Any, port);
 
    var mytester = new MyTester();
 
    var tcphost = new TcpHost(ipEndpoint, logger, stats);
    tcphost.AddService<IMyTester>(mytester);
 
    tcphost.Open();
 
    Console.WriteLine("Press Enter to stop the dual host test.");
    Console.ReadLine();
 
    tcphost.Close();
 
    Console.WriteLine("Press Enter to quit.");
    Console.ReadLine();
 }

On the client--easy

 private static void Main(string[] args)
 {
    var ip = ConfigurationManager.AppSettings["ip"];
    var port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]);
    var ipEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
 
    using (var client = new TcpClient<IMyTester>(ipEndPoint)
    {
       for (int i = 0; i < 10; i++)
       {
             var id = client.Proxy.GetId("test1", 3.314, 42);
             int q2 = 4;
             var response = client.Proxy.Get(id, "mirror", 4.123, out q2);
             var list = client.Proxy.GetItems(id, new int[] { 3, 6, 9 });
       }
    }
    
    Console.ReadLine();
 }
And that's all there is to it.
Clone this wiki locally