Socket Programming in C#
page 5 of 6
by Joydip Kanjilal
Feedback
Average Rating: 
Views (Total / Last 10 Days): 46670/ 80

Implementing a Simple Server – Client Application using Sockets

In this section we will discuss how to implement a simple client-server application using Sockets in C#. There will be two distinct applications, i.e., a Server application and a Client application. The Server application will connect to a port and be in the listen mode waiting for a Client to connect. Once the Client is connected, it will send a test message to the Server application using a StreamWriter. This text message will then be displayed in the Server application's console.

Here is the source code of the Server application.

Listing 3: The SocketServer class

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
 
class SocketServer
{
  public static void Main()
  {
    StreamWriter streamWriter;
    StreamReader streamReader;
    NetworkStream networkStream;
    TcpListener tcpListener = new TcpListener(5555);
    tcpListener.Start();
    Console.WriteLine("The Server has started on port 5555");
    Socket serverSocket = tcpListener.AcceptSocket();
    try
    {
      if (serverSocket.Connected)
      {
        while (true)
        {
          Console.WriteLine("Client connected");
          networkStream = new NetworkStream(serverSocket);
          streamWriter = new StreamWriter(networkStream);
          streamReader = new StreamReader(networkStream);
          Console.WriteLine(streamReader.ReadLine());
 
        }
      }
      if (serverSocket.Connected)
        serverSocket.Close();
      Console.Read();
    }
    catch (SocketException ex)
    {
      Console.WriteLine(ex);
    }
  }
}

Refer to the code snippet given above. The Server application starts the port 5555; displays a relevant message and waits for the incoming requests from the Client to connect to it. Now, when you run this application, the message "The Server has started on port 5555" will be displayed on the Server application's console. As soon as a Client gets connected to the same port, the message "Client connected" is displayed on the console. Here is the output when you execute this application.

Figure 4: The SocketServer console when started

Next, we will take a look at the source code of the Client application that will connect to the Server application using the same port, i.e., 5555. Here is the code.

Listing 4: The SocketClient class

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
 
class SocketClient
{
  static void Main(string[]args)
  {
    TcpClient tcpClient;
    NetworkStream networkStream;
    StreamReader streamReader;
    StreamWriter streamWriter;
    try
    {
      tcpClient = new TcpClient("localhost", 5555);
      networkStream = tcpClient.GetStream();
      streamReader = new StreamReader(networkStream);
      streamWriter = new StreamWriter(networkStream);
      streamWriter.WriteLine("Message from the Client...");
      streamWriter.Flush();
    }
    catch (SocketException ex)
    {
      Console.WriteLine(ex);
    }
    Console.Read();
  }
}

When you execute the Client application, here is how the output will look like at the Server application’s console.

Figure 5: The SocketServer console when the client is connected

Note that the message "Client connected" and the text sent by the client, "Message from the Client…", is displayed at the Server application’s console.


View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-10-16 1:34:02 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search