Wednesday, December 9, 2015

c# Simple TCP Client Server

This is a simple implementation of a TCP client server relationship. In this samle, the TCP server listens to a port and if any client is trying to connect to the server , the server accepts and reads the message from the socket.


TCP Server


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

/**
 * @Author : JITHINRAJ.P
 * 
 * 
 * */
namespace TCP_server
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                IPAddress ipAd = IPAddress.Parse("127.0.0.1");

                TcpListener myList = new TcpListener(ipAd, 5150);

                myList.Start();

                Console.WriteLine("The server is running at port 8001...");
                Console.WriteLine("The local End point is  :" +
                                  myList.LocalEndpoint);
                Console.WriteLine("Waiting for a connection.....");

                while (true)
                {
                    Socket s = myList.AcceptSocket();

                    Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

                    byte[] b = new byte[100];
                    while (true)
                    {
                    int k = s.Receive(b);
                    Console.WriteLine("Recieved...");
                    for (int i = 0; i < k; i++)
                        Console.Write(Convert.ToChar(b[i]));

                    ASCIIEncoding asen = new ASCIIEncoding();
                    s.Send(asen.GetBytes("The string was recieved by the server."));
                    Console.WriteLine("\nSent Acknowledgement");
                    //s.Close();

                    if (k == 0)
                    {
                        s.Close();
                        break;
                    }
                    }
                }
                myList.Stop();
                Console.ReadLine();
            }
            catch (Exception e)
            {

            }
        }
    }
} 


TCP Client 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;

/**
 * @Author : JITHINRAJ.P
 * 
 * 
 * */

namespace TCP_Client
{
    class Program
    {
        private static TcpClient client;
        static void Main(string[] args)
        {
            client = new TcpClient();

            Console.WriteLine("Connecting to server");


            client.Connect("127.0.0.1" , 5150);

            Console.WriteLine("Connected");

            Console.WriteLine("enter The string to be  transmitted");

            String textToTransmit = Console.ReadLine();

            Stream tcpStream = client.GetStream();

            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] ba = asen.GetBytes(textToTransmit);
            Console.WriteLine("Transmitting.....");

            tcpStream.Write(ba, 0, ba.Length);

            byte[] bb = new byte[100];
            int k = tcpStream.Read(bb, 0, 100);

            for (int i = 0; i < k; i++)
                Console.Write(Convert.ToChar(bb[i]));

            client.Close();

        }
    }
}

2 comments: