Creating TCP/IP Server and Client in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NitinSawant
    Contributor
    • Oct 2007
    • 271

    Creating TCP/IP Server and Client in C#

    Hi I'm newbie in C#,

    pls tell me how to create simple TCP/IP Server and Client to send and recieve text using C#
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Originally posted by Nitin646
    Hi I'm newbie in C#,

    pls tell me how to create simple TCP/IP Server and Client to send and recieve text using C#
    Well, that's a pretty broad question. You need to look into using:
    System.Net.Sock ets.TcpListener
    System.Net.Sock ets.TcpClient

    To get you started, here's some code I had worked on before for the server.
    If you have this running, a client can make a request on the port specified and it will return the text you specify ("Hello World")

    To make a client, look up TcpClient. You should be able to find something.
    [CODE=cpp]
    class TcpIpServer
    {
    private readonly int port = 48888;
    private readonly IPAddress ip = IPAddress.Parse ("127.0.0.1" );

    private TcpListener listener;
    [STAThread]
    static void Main(string[] args)
    {
    TcpIpServer ts = new TcpIpServer();
    ts.Start();
    }

    public TcpIpServer()
    {
    this.listener = new TcpListener(thi s.ip, this.port);
    }

    public void Start()
    {
    this.listener.S tart();
    Socket s;
    Byte[] incomingBuffer;
    Byte[] response;
    int bytesRead;

    while (true)
    {
    s = this.listener.A cceptSocket();

    incomingBuffer = new Byte[100];
    bytesRead = s.Receive(incom ingBuffer);

    response = Encoding.Unicod e.GetBytes("Hel lo World");
    s.Send(response );
    }
    }
    }
    [/CODE]

    Comment

    • NitinSawant
      Contributor
      • Oct 2007
      • 271

      #3
      thanx,

      hey you have posted the code for server only,
      what about client???

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Try this article.

        That should get you to where you need.

        Comment

        • NitinSawant
          Contributor
          • Oct 2007
          • 271

          #5
          Dear sir ,insertAlias
          Thanx a lot for replying

          Comment

          Working...