Socket connection

In this chapter you will learn:

  1. How to find out if a Socket is blocked or connected
  2. Bind Socket with an IPEndPoint
  3. Display the connected client IP address
  4. Socket connection
  5. Echo Client with UTF8 Encoding

Is Socket blocking or connected

using System;/*j a  va2s .  co m*/
using System.Net;
using System.Net.Sockets;

class MainClass
{
   public static void Main()
   {
      IPAddress ia = IPAddress.Parse("127.0.0.1");
      IPEndPoint ie = new IPEndPoint(ia, 8000);

      Socket test = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

      Console.WriteLine("Blocking: {0}", test.Blocking);

      test.Blocking = false;
      Console.WriteLine("new Blocking: {0}",test.Blocking);
      Console.WriteLine("Connected: {0}", test.Connected);

      test.Close();
   }
}

The code above generates the following result.

Bind Socket with an IPEndPoint

using System;/*from  ja  va2 s  . c  om*/
using System.Net;
using System.Net.Sockets;

class MainClass
{
   public static void Main()
   {
      IPAddress ia = IPAddress.Parse("127.0.0.1");
      IPEndPoint ie = new IPEndPoint(ia, 8000);

      Socket test = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

      Console.WriteLine("Blocking: {0}", test.Blocking);

      test.Bind(ie);
      IPEndPoint iep = (IPEndPoint)test.LocalEndPoint;
      Console.WriteLine("Local EndPoint: {0}",iep.ToString());

      test.Close();
   }
}

The code above generates the following result.

Display the connected client IP address

using System;/*from  jav a  2s .  c o m*/
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

class MainClass
{
   public static void Main()
   {
      string data;
      IPEndPoint ip = new IPEndPoint(IPAddress.Any, 9999);

      Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);

      socket.Bind(ip);
      socket.Listen(10);

      Socket client = socket.Accept();
      IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
      Console.WriteLine("Connected with {0} at port {1}",newclient.Address, newclient.Port);

 
   }
}

Socket connection

using System;/*  ja v a 2 s .co m*/
using System.Net;
using System.Net.Sockets;
using System.Text;

class MainClass
{
   public static void Main()
   {
      IPAddress host = IPAddress.Parse("192.168.1.1");
      IPEndPoint hostep = new IPEndPoint(host, 8000);
      Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

      try
      {
         sock.Connect(hostep);
      } catch (SocketException e) {
         Console.WriteLine("Problem connecting to host");
         Console.WriteLine(e.ToString());
         sock.Close();
         return;
      }

      sock.Close();
   }
}

Echo Client with UTF8 Encoding

using System;/*  ja  v  a  2  s . c o m*/
using System.Net;
using System.Net.Sockets;
using System.Text;

class MainClass
{
  const int echoPort = 7;

  [STAThread]
  static void Main( string[] args )
  {
    Socket s = new Socket( AddressFamily.InterNetwork, 
      SocketType.Stream, 
      ProtocolType.Tcp );
    s.Connect( new IPEndPoint( IPAddress.Loopback, echoPort ) );

    UTF8Encoding enc = new UTF8Encoding();
    s.Send( enc.GetBytes( "test message" ) );
    Byte[] buff = new Byte[ 1024 ];
    s.Receive( buff );
    System.Console.WriteLine( enc.GetString( buff ) );
  }
}

Next chapter...

What you will learn in the next chapter:

  1. Find out AddressFamily, SocketType and ProtocolType for a Socket
Home » C# Tutorial » Network
Cookie
CredentialCache
HostEntry
IPHostEntry
IP address from host name
IPEndPoint from IPAddress
IPEndPoint Port
IPEndPoint properties
IPHostEntry
IP Address Parser
Predefined IP Address
NetworkCredential
Ping
Udp Client
Socket connection
Socket creation
Socket sendTo
TcpClient
TcpListener
UdpClient Receive
UdpClient Sending
Uri
WebClient
Response Headers
WebProxy
WebRequest