In socket-based network programming, you do not directly access the network interface device to send and receive packets. Instead, an intermediary file descriptor is created to handle the programming interface to the network. The special file descriptors used to reference network connections are called sockets . The socket defines the following:

  • A specific communication domain, such as a network connection or a Unix Interprocess Communication (IPC) pipe

  • A specific communication type, such as stream or datagram

  • A specific protocol, such as TCP or UDP


After the socket is created, it must be bound to either a specific network address or port on the system, or to a remote network address and port. Once the socket is bound, it can be used to send and receive data from the network.

Network Addresses


After the socket is created, it must be bound to a network address/port pair. UNIX offers an IP-specific address structure, sockaddr_in , which uses the following elements. Using the sockaddr_in structure requires placing the appropriate IP address and port values in the proper data element.



  1. sin_family An address family, defined as a short type

  2. sin_port A port number, defined as a short type

  3. sin_addr An address, defined as a long type (4-byte) IP address

  4. sin_data 8 bytes of padding.


 


.NET defines two classes in the System.Net namespace to handle various types of IP address information.The classes are IPAddress and IPEndPoint IPAddress object is used to represent a single IP address. This value can be used in the various socket methods to represent the IP address.


IPAddress Methods:-










































Method


Description


Equals

Compares two IP addresses.

GetHashCode

Returns a hash value for an IPAddress object.

GetType

Returns the type of the IP address instance.

HostToNetworkOrder

Converts an IP address from host byte order to network byte order.

IsLoopBack

Indicates whether the IP address is considered the loopback address.

NetworkToHostOrder

Converts an IP address from network byte order to host byte order.

Parse

Converts a string to an IPAddress instance.

ToString

Converts an IPAddress to a string representation of the dotted decimal format of the IP address.

IPEndPoint: object is used when binding sockets to local addresses, or when connecting sockets to remote addresses. The following two constructors are used to create IPEndPoint instances:



  • IPEndPoint (long address , int port )

  • IPEndPoint(IPAddress address , int port )


PEndPoint Methods:


































Method


Description


Create

Creates an EndPoint object from a SocketAddress object

GetType

Returns the type of the IPEndPoint instance

Equals

Compares two IPEndPoint objects

Serialize

Creates a SocketAddress instance of the IPEndPoint instance

ToString

Creates a string representation of the IPEndPoint instance

GetHashCode

Returns a hash value for an IPEndPoint object

 


 


 


Example: Demonstrate Socket Programming

Form4.cs


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


using System.Net;


 


namespace _CSharpApplication


{


public partial class Form4 : Form


{


public Form4()


{


InitializeComponent();


}


 


private void button1_Click( object sender, EventArgs e)


{ //Introduction to IP Addresses.


IPAddress _address1 = IPAddress .Parse( "192.168.1.1" );


IPAddress _address2 = IPAddress .Broadcast;


IPAddress _address3 = IPAddress .Loopback;


IPAddress _address4 = IPAddress .None;


IPAddress _address5 = IPAddress .Any;


IPHostEntry _hostName = Dns .GetHostEntry( Dns .GetHostName());


IPAddress _myMachine = _hostName.AddressList[0];


if ( IPAddress .IsLoopback(_address3))


lstAddresses.Items.Add( "Loopback address is: " + _address3.ToString());


else


lstAddresses.Items.Add( "Error obtaining the LoopbackAddress" );


lstAddresses.Items.Add( "Local IP address is: " + _myMachine.ToString());


 


if (_myMachine == _address3)


lstAddresses.Items.Add( "Loopback Address Is Same AsLocal Address.\n" );


else


lstAddresses.Items.Add( "The loopback address is not the local address.\n" );


lstAddresses.Items.Add( "Given Address is: " + _address1.ToString());


lstAddresses.Items.Add( "Broadcast address: " + _address2.ToString());


lstAddresses.Items.Add( "ANY address is: " + _address5.ToString());


lstAddresses.Items.Add( "NONE address is: " + _address4.ToString());


}


 


private void button2_Click( object sender, EventArgs e)


{ //Introduction To IPEndPoint


IPAddress test1 = IPAddress .Parse( "192.168.1.1" );


IPEndPoint _endPoint = new IPEndPoint (test1, 8000);


 


lstIPEndPoint.Items.Add( "IPEndPoint is ::" + _endPoint.ToString());


lstIPEndPoint.Items.Add( "AddressFamily is ::" +_endPoint.AddressFamily);


lstIPEndPoint.Items.Add( "Address is :: " + _endPoint.Address);


lstIPEndPoint.Items.Add( "Port is :: " + _endPoint.Port);


lstIPEndPoint.Items.Add( "Min port number is :: " + IPEndPoint .MinPort);


lstIPEndPoint.Items.Add( "Max port number is :: " +IPEndPoint .MaxPort);


_endPoint.Port = 80;


lstIPEndPoint.Items.Add( "Changed IPEndPoint is :: " + _endPoint.ToString());


SocketAddress _socketAddress = _endPoint.Serialize();


lstIPEndPoint.Items.Add( "SocketAddress is :: " + _socketAddress.ToString());


}


}}


 


 


Output:


 





 


Clicking on both the buttons the output will be:


 


                    

Copyright © 2013 VisualBuilder. All rights reserved