VisualBuilder
  Home > Csharp > Tutorials > Using C sharp Socket - 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
 Using C sharp Socket
  << Prev: Creating your own Permission Set

The System.Net.Sockets namespace contains the classes that provide the actual .NET interface to the low-level Winsock APIs. This section gives a brief overview of the C# Socket class. The core of the System.Net.Sockets namespace is the Socket class. It provides the C# managed code implementation of the Winsock API. The Socket class constructor is as follows:


Socket(AddressFamily af , SocketType st , ProtocolType pt )


As you can see, the basic format of the Socket constructor mimics the original Unix socket() function. It uses three parameters to define the type of socket to create:



  • An AddressFamily to define the network type

  • A SocketType to define the type of data connection

  • A ProtocolType to define a specific network protocol


Each of these parameters is represented by a separate enumeration within the System.Net_.Sockets namespace. Each enumeration contains the values that can be used. For normal IP communications on networks, the AddressFamily.InterNetwork value should always be used for the AddressFamily. With the InterNetwork AddressFamily, the SocketType parameter must match a particular ProtocolType parameter. You are not allowed to mix and match SocketTypes and ProtocolTypes.


 


Socket Properties:
Several properties of the Socket class can be used to retrieve information from a created Socket object














































Property



Description



AddressFamily



Gets the address family of the Socket



Blocking



Gets or sets whether the Socket is in blocking mode



LocalEndPoint



Gets the local EndPoint object for the Socket



Connected



Gets a value that indicates if the Socket is connected to a remote device



RemoteEndPoint



Gets the remote EndPoint information for the Socket



SocketType



Gets the type of the Socket



Handle



Gets the operating system handle for the Socket



ProtocolType



Gets the protocol type of the Socket



Available



Gets the amount of data that is ready to be read



Introduction to Socket Exceptions:


One feature of socket programming included in .NET Framework is neither used by Unix nor the Winsock API is socket exceptions. All of the Socket class methods use the SocketException exception. Any socket programming you do should always check for SocketException exceptions and then attempt to recover from the error, or at least warn the user of the problem.


Example: Demonstrate Socket Properties and Socket Exception Handling


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;


using System.Net.Sockets;


 


 


namespace _CSharpApplication


{


  public partial class Form5 : Form


    {


    public Form5()


        {


    InitializeComponent();


        }


 


    private void button1_Click( object sender, EventArgs e)


        { // Demonstrate Socket Properties


            IPAddress _ipAddress = IPAddress .Parse( "127.0.0.1" );


            IPEndPoint _ipEndPoint = new IPEndPoint (_ipAddress, 8000);


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


            lstSocketProperties.Items.Add( "AddressFamily :: " + _socketTest.AddressFamily);


            lstSocketProperties.Items.Add( "SocketType :: " + _socketTest.SocketType);


            lstSocketProperties.Items.Add( "ProtocolType :: " + _socketTest.ProtocolType);


            lstSocketProperties.Items.Add( "Blocking :: " +_socketTest.Blocking);


            _socketTest.Blocking = false ;


            lstSocketProperties.Items.Add( "new Blocking :: " + _socketTest.Blocking);


            lstSocketProperties.Items.Add( "Connected :: " + _socketTest.Connected);


            _socketTest.Bind(_ipEndPoint);


            IPEndPoint _newIpEndPoint = ( IPEndPoint )_socketTest.LocalEndPoint;


            lstSocketProperties.Items.Add( "Local EndPoint :: " + _newIpEndPoint.ToString());


            _socketTest.Close();


        }


 


    private void button2_Click( object sender, EventArgs e)


        { //Socket Exceptions


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


            IPEndPoint _hostIPEndPoint = new IPEndPoint (_hostIPAddress, 8000);


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


      try


            {


                _socket.Connect(_hostIPEndPoint);


            }


      catch ( SocketException e1)


            {


                lstSocketExceptions.Items.Add( "Problem Connecting To Host ........" );


                lstSocketExceptions.Items.Add(e1.ToString());


                _socket.Close();


        return ;


            }


      try


            {


                _socket.Send( Encoding .ASCII.GetBytes( "testing" ));


            }


      catch ( SocketException ex)


            {


                lstSocketExceptions.Items.Add( "Problem Sending Data" );


                lstSocketExceptions.Items.Add(ex.ToString());


                _socket.Close();


        return ;


            }


            _socket.Close();


        }


    }


 }


Output:



 


Clicking On both the Buttons will display the respective outputs in the listbox



  << Prev: Creating your own Permission Set
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
Exchange Hosting
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

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