News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

Socket Programming in C#

Started by dhilipkumar, May 21, 2009, 07:39 PM

Previous topic - Next topic

dhilipkumar

Socket Programming in C#

Understanding Sockets and Ports

A socket is the end point of a two way communication between two systems running over a network.

When two or more processes communicate over a network, they communicate using Sockets. A port implies an unsigned integer that uniquely identifies a process running over a network. The following are some of the well known port numbers distinguished by the type of services that they provide:


·         HTTP                 80

·         Telnet                23

·         SMTP                 25

·         SNPP                 444

·         DNS                  53

·         FTP (Data)          20

·         FTP (Control)      21 


Synchronous and Asynchronous Communications


.NET have support for both synchronous and asynchronous communication using sockets.When working in the synchronous mode, a method call blocks itself unless the operation is complete in all respects. In the other mode of operation, i.e., the asynchronous mode, a method returns even before its turnaround time has elapsed.

Note that the Socket class in the System.Net.Sockets namespace contains both synchronous and asynchronous methods. As an example, while the Connect() and Receive() methods are meant for synchronous operation, the BeginConnect() and EndConnect() methods as well as the BeginReceive() and EndReceive() methods are their asynchronous counterparts.


aspalliance

dhilipkumar

#1
Displaying the IP address of Hotmail using Sockets

etHostName() is a static method of the Dns class. Here is a simple program that illustrates how you can display the IP address of the site, web site using this class.


using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace TestSocket
{
  class Program
  {
    static void Main(string[]args)
    {
      try
      {
        IPHostEntry IPHost = Dns.GetHostEntry("www.hotmail.com");
        IPAddress[]ipAddress = IPHost.AddressList;
        StringBuilder strIpAddress = new StringBuilder();

        for (int i = 0; i < ipAddress.Length; i++)
        {
          strIpAddress.Append(ipAddress[].ToString());
        }

        Console.WriteLine("The IP Address is: " + strIpAddress.ToString());
      }
      catch (SocketException ex)
      {
        Console.WriteLine("Error Occured! " + ex);
      }

      Console.Read();
    }
  }
}

When you execute the above program, the IP address of  site. In this case it displayed as 211.206.123.219. However, if you are not connected to the network, the method fails and a SocketException is thrown. The exception is caught in the catch block shown in the above program.



dhilipkumar

Implementing a Simple Server – Client Application using Sockets

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.


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);
    }
  }
}


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.

nomad


nomad

Does the above socket program correct .. Connection part is okay disconnection part require some attention ! and System.IO is missed at namespace inclusion

dhilipkumar

hi NOMAD

if u find other missing namespace , you can update here...

thanks for u r reply

dennis

A Socket is an End-Point of To and From communication link between two programs running on the same network . That code works asynchronously for send/receive data between client and server. By that code any one can transfer data easily as when he want.