VisualBuilder
  Home > Csharp > Tutorials > Sockets - C# Tutorial
Tell a friend
Link to us
Total Members
      Members: 84606
     
Sitemap Forum Chat
Home
C# Tutorial Home
1 . Introduction to C sharp
2 . Control Statement: Selection Statement in C sharp
3 . Control Statements – Loops Statement
4 . Methods
5 . Namespaces
6 . Introduction to Classes:
7 . Inheritance:
8 . Polymorphism
9 . Properties
10 . Indexers
11 . Structs
12 . Interfaces
13 . Delegates:
14 . Exception Handling:
15 . Attributes
16 . Enums
17 . Encapsulation
18 . Parameter Passing in C sharp
19 . Method Overloading
20 . Database Interaction Using C sharp
21 . Operator Overloading in C sharp -1
22 . Operator Overloading in C sharp -2
23 . Operator Overloading in C sharp -2
24 . Sockets
25 . DNS [Domain Name System]
26 . Working with Files
27 . Generating Help File in C sharp
28 . Code Access Security
29 . Multi-Threading
30 . Globalization and Localization -1
31 . Working with Registry in C sharp
32 . Globalization and Localization -2
33 . Windows Service
34 . Web Service
35 . Consuming Web Services
36 . Creating Proxy Object of Web Service
37 . Creating an XML document
38 . Reading XML document in C sharp
39 . Using XMLWriter class to Write XML document in C sharp
40 . Assembly Information : Getting Permission set of the assembly
41 . Creating your own Permission Set
42 . Using C sharp Socket
 
Csharp Group Home
Csharp Discussion (7)
Csharp Members (2250)
Csharp Resources
Csharp Source Code (35)
Csharp Articles (0)
Csharp Blogs
Csharp Jobs
Csharp Components (5)
Csharp Books
Csharp Websites (25)
Csharp News (17)
Csharp Q & A (4)
- Csharp Ask Question
- Csharp Questions
- Csharp Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (432 )
Source Code (3217 )
Articles (11 )
Components (1589 )
News (880 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise


Csharp Tutorial
 Sockets
  << Prev: Operator Overloading in C sharp -2 Next: DNS [Domain Name System] >>
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:


 



  << Prev: Operator Overloading in C sharp -2 Next: DNS [Domain Name System] >>
Csharp Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
MSN
Video Surveillance
Hosted Exchange, SDSL
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising