Jens Willmer

Tutorials, projects, dissertations and more..

Simple TCP/IP client/server application

For a university lecture I had to write a simple application to demonstrate a client/server communication over TCP/IP. I know there are many demos in the world wide web and now there is one more ;-)

Code

Below I post you the code and at the end of the post you will find a ZIP-file with the project - have fun.

Server
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Server
{
    class Program
    {
        const int port = 8001;
        const string ip = "127.0.0.1";
        const int maxBuffer = 100;

        static void Main(string[] args)
        {
            try
            {
                IPAddress ipAddress = IPAddress.Parse(ip);
                TcpListener tcpListener = new TcpListener(ipAddress, port);
                tcpListener.Start();

                Console.WriteLine(string.Format("The server is running at port {0}..", port));
                Console.WriteLine(string.Format("The local End point is: {0}", tcpListener.LocalEndpoint));

                Console.WriteLine("\nWaiting for connection..");
                using (Socket socket = tcpListener.AcceptSocket())
                {
                    Console.WriteLine(string.Format("Connection accepted from: {0}", socket.RemoteEndPoint));

                    byte[] receiveBuffer = new byte[maxBuffer];
                    int usedBuffer = socket.Receive(receiveBuffer);

                    Console.WriteLine("\nRecieved..");
                    for (int i = 0; i < usedBuffer; i++)
                        Console.Write(Convert.ToChar(receiveBuffer[i]));

                    Console.WriteLine("\n\nSent acknowledgement");
                    socket.Send(new ASCIIEncoding().GetBytes("The string was recieved by the server."));

                    Console.ReadLine();
                }

                tcpListener.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("Error: {0}", e.StackTrace));
            }
        }
    }
}

Client

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace Client
{
    class Program
    {
        const int port = 8001;
        const string ip = "127.0.0.1";
        const int maxBuffer = 100;

        static void Main(string[] args)
        {
            try
            {
                using (TcpClient tcpClient = new TcpClient())
                {
                    Console.WriteLine("Connecting..");
                    tcpClient.Connect(ip, port);
                    Console.WriteLine("Connected");

                    Console.Write("\nEnter the string to be transmitted: ");
                    String inputString = Console.ReadLine();
                    Stream networkStream = tcpClient.GetStream();

                    byte[] sendBuffer = new ASCIIEncoding().GetBytes(inputString);
                    Console.WriteLine("Transmitting..\n");
                    networkStream.Write(sendBuffer, 0, sendBuffer.Length);

                    Console.WriteLine("Receive acknowledgement from server..");
                    byte[] receiveBuffer = new byte[maxBuffer];
                    int k = networkStream.Read(receiveBuffer, 0, maxBuffer);

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

                    Console.ReadLine();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("Error: {0}", e.StackTrace));
            }
        }
    }
}