Click here to Skip to main content
15,892,643 members
Articles / Programming Languages / C#

Custom socket communication

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
13 Dec 2012CPOL4 min read 29.6K   1.7K   30  
Custom socket communication between two .NET projects
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using Utils;

namespace ConsoleReciver
{
    class Program
    {
        static void Main(string[] args)
        {
            SocketReciever<SocketData,ResponseData> socketReciver = new SocketReciever<SocketData,ResponseData>("localhost", 8000);
            socketReciver.OnConnectionChanged += new SocketReciever<SocketData,ResponseData>.ConnectionChanged(socketReciver_OnConnectionChanged);
            socketReciver.OnNewData += new SocketReciever<SocketData,ResponseData>.NewData(socketReciver_OnNewData);
            socketReciver.Start(5000);
            String readLine = Console.ReadLine();
            while (readLine != "exit")
            {
                socketReciver.SendResponse(new ResponseData(readLine, DateTime.Now));
                readLine = Console.ReadLine();
            }
            socketReciver.Stop();
        }

        static void socketReciver_OnNewData(SocketData data)
        {
            Console.WriteLine("Data: " + data.Number.ToString() + " : " + data.Time.ToString());
        }

        static void socketReciver_OnConnectionChanged(bool connected)
        {
            Console.WriteLine("Connection: " + connected.ToString());
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Slovenia Slovenia
Senior C# .NET developer in gaming industry

Specialties
C#, XML, WebServices, WCF

Comments and Discussions