Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Programming > C#

Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More
 
Bookmarks Article Tools Search this Article Display Modes

Winsock with C#


On 8th March, 2007
Smile Winsock with C#

Show Printable Version Email this Page Subscription Add to Favorites Copy Winsock with C# link

Author

Febian ( Light Poster )

Yet to provide details about himself


All articles By Febian

Recent Articles

Similar Articles

Introduction



We use Winsock to establish connection between client and server . This is the beginning of everything in the computer of today's !!! The net is the most power tools of the programmers and you must learning to use it. In this article we will see how to create a simple chat program. The chat program will be just a server and a client, that you can connect from the internet ( or LAN ) and simply exchange text messages.

What is Winsock



The Winsock we are going to use is an COM (References) that we can add in our C# program so we can use it's features. When using the internet ( like from a web browser ) a lot of things happen behind the scenes. Packets are constructed by the softwares that are then being send through routers and others are being received by your Operating System and analyzed by the application that send it. In order to do such complicated things a lot of in formation like headers, packet size ,hashes ,packet order and many more are required to create the packets. You WILL NOT have to deal with that stuff using the Winsock control from C#(or any other language) Continue reading to see just how easy it is to effectively use Winsock.

What do you need



For this tutorial you will only need a computer running Windows ,Microsoft Visual Studio 2005 , and the will to learn!

What are servers and clients



To connect any 2 programs, you need at least one server and one client.

The server will be the program that opens the ports on the hosting machine and receive the connections while the client is called the program that connects to the remote host. For example, when you connect to Google with your firefox ( or internet explorer ), your browser plays the role of the client that connects to the hosts that are running at Google. Most common is for the servers to be able to receive more than one connections from different clients These is called multithreaded socket servers , but I am not going to show you how they are made in this tutorial, just to keep things simple. What i am going to show you is simply 2 applications connecting together, and sending/receiving text strings.

Writing the Client



First create your client form..



Now we have the form but it has no code inside, it's only the components.

Remember that you need to add Winsock COM component control to your program, to do this right click on the toolbar that is left to the form ( where buttons ,labels... are) Then click on the "Choose Items ... " item from the menu bar and find "Microsoft Winsock Control 6.0" , Check it and click ok.



And now the CODE!!! of the forms components

Code: CSharp
Boolean isConnect= false; // we use this Boolean to know if we are connected
private void Connect_Click(object sender,EventArgs e)
{
    try
    {
        w1.Close(); // close the winsock in case it was open

        // now is time to connect ;-))
        // ipText and portText is textbox
        // in ipText the user is write the host ip
        // in portText the user is write the host port
        // (BUT client and host MUST use the same port )
        // if the server accept the connection the w1_connect event is raised
        w1.Connect(IPText.Text, PortText.Text);

    }
    // with try - catch we take all the exception that maybe come and write it to the screen
    catch (System.Windows.Forms.AxHost.InvalidActiveXStateException g)
    {
        DataInput.Text += "\n" + g.ToString();
    }
}

private void Send_Click(object sender, EventArgs e)
{
    try
    {
        if (isConnect)
        {
            //this will send the data that we wrote in SendText textbox
            w1.SendData(SendText.Text);

            //we add what we said to screen
            DataInput.Text += "\nClent(You ;-) : " + SendText.Text;

            //after we send the data we clear the textbox
            SendText.Text = "";
        }
        else
            MessageBox.Show("You are not connect to any host ");
    }
    // with try - catch we take all the exception that maybe come and write it to the screen
    catch (AxMSWinsockLib.AxWinsock.InvalidActiveXStateException g)
    {
        DataInput.Text += "\n" + g.ToString();
    }
    catch (Exception ex)
    {
        DataInput.Text +="\n" + ex.Message;
    }
}
This is the part that you must control the event that happen to the Wnsock . To do that you must say to the system what event you want to control and handle .

We need 3 specific event to use : (Error,ConnectEvent,DataArrival)
Code: CSharp
this.w1.Error += new AxMSWinsockLib.DMSWinsockControlEvents_ErrorEventHandler(this.w1_Error);
this.w1.ConnectEvent +=new System.EventHandler(this.w1_ConnectEvent);
this.w1.DataArrival += new AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEventHandler(this.w1_DataArrival);
This code is written in InitializeComponent() (The method that design the form ) you can find this method to <Form Name>.Designer.cs

One trick : When you type"this.w1.ConnectEvent +=" you can press TAB and then Visual Studio complete the sentence "new System.EventHandler(this.w1_ConnectEvent);"

And if you press TAB again the Studio insert automatically the method w1_ConnectEvent that handle the event .

The Handle method CODE:
Code: CSharp
// in case of any errors ( like when the host don't accept your connection request)
// this event raises and the e.number and e.description hold the errors info
void w1_Error(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent e)
{
    DataInput.Text += "\n- Error : " + e.description;
    isConnect = false;
}

// when the server sends any data this event raises
private void w1_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
{
    // data is the variable that store the incoming msgbox
    String data ="";

    // dat is variable type object
    // the method getData of Winsock take only general type object
    // and we input data in the variable to use it next time by ref
    Object dat = (object)data;


    // with this command the data are
    // being stored in the dat variable so
    // we can use them.
    w1.GetData(ref dat);


    // change back the object dat to string
    data = (String)dat;


    // write data to the screen
    // (adding a host ip in front of the string to know where this came from :o)
    DataInput.Text += "\nServer - " + w1.RemoteHostIP + " : " + data ;

}


private void w1_ConnectEvent(object sender, EventArgs e)
{
    //there we write what we what our program to do
    //when the connection with the host is established
    //w1.remotehostip is a function that will return as the hosts ip
    DataInput.Text += "\n - Connect Event : " + w1.RemoteHostIP;


    isConnect = true;
}

Writing the Server



Basically the server is pretty much the same as the client. I will only mention the differences. The working server code is included in the example source code that you can download

Here is the Server Form...



The first difference is in the Connection. While the client set a remote ip and a remote port and tried to connect on them, the server only needs to set a local port and listen to it.

Listening on the port means that the program is monitoring for any connection request made by the clients on that specific port. This is what you must do for the 'Start Listening' Button :
Code: CSharp
private void Start_Click(object sender, EventArgs e)
{
    if (portText.Text != "")
    {
        //to create a connection we need a server(host).
        //server we call the program that keeps a port open
        //so the someone ( client ) can connect
        //do open the port we must write the following code


        //with this we set the port we want to use for .the connection
        w1.LocalPort = Int32.Parse(portText.Text);

        //this command opens the port and waits (listens) for a connection
        w1.Listen();
    }
}

private void Disconnect_Click(object sender, EventArgs e)
{
    w1.Close();//this will close the connection with the client
    w1.LocalPort = Int32.Parse(portText.Text);
    w1.Listen();//open the port to wait for another connection
    DataInput.Text += "\n - Disconnected";
}
Now the next difference from the client is in the connection handling.

The client had the Connect event that was triggered when the connection was established

With the server we need to accept the request from the client before the connection is completed

To do that we use the ConnectionRequest that is triggered when a client tries to connect on our host. the connection will be completed only if we accept the request.

this.w1.ConnectionRequest +=new AxMSWinsockLib.DMSWinsockControlEvents_ConnectionR equestEventHandler(this.w1_ConnectionRequest);

Bellow is the code that handles the connection request
Code: CSharp
//this event is called when a client request a connection with the server
private void w1_ConnectionRequest(object sender,......AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent e)
{
    if (isConnected==true )
    {
        w1.Close ();
    }

    //with this commands we accept the connection request to the server..
    w1.Accept (e.requestID );
    isConnected =true;
    //now we are connected with the client
    DataInput.Text +="\n - Client Connected :" + w1.RemoteHostIP ;
}
Old 12-08-2007, 04:14 AM   #2
Newbie Member
 
Join Date: Dec 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
jtehrhart is on a distinguished road

Re: Winsock with C#


I cannot find the link to download the example program. Does it exist?
jtehrhart is offline   Reply With Quote
Old 02-19-2008, 08:49 AM   #3
Newbie Member
 
Join Date: Feb 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
cwl1945 is on a distinguished road

Re: Winsock with C#


As previously stated by a previous post, some download code would really be nice. There is one main problem with this article. The Client Form is labeled "Server". I think the author just cut and pasted the wrong image. The Client form should have a text box for inputting the server's IP address -- going through the included code there is a IP TextBox which gives credence to the wrong form was cut and pasted in this article. A downloadable "working" version would be of great benefit to someone trying to learn from this example.
cwl1945 is offline   Reply With Quote
Old 04-19-2008, 01:23 AM   #4
Newbie Member
 
Join Date: Apr 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
morg is on a distinguished road

Re: Winsock with C#


Hi, did you see xf.server component that helps to create high performance servers in .net? I don't think that activex or com can provide better performance because there is more overhead with extra wrapper layer. Also there are memory and marshaling issues.
morg is offline   Reply With Quote
Old 05-10-2008, 04:52 AM   #5
Light Poster
 
Join Date: May 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
CyCloneNL is on a distinguished road

Re: Winsock with C#


Quote:
We use Winsock to establish connection between client and server.
Well it isnt the only way, and i believe not the easiest way either.

Quote:
This is the beginning of everything in the computer of today's !!!
Incorrect, a computer starts with a Motherboard. When you replace Computer with programming, you are incorrect again. You dont start with one of the hardest things, you start off easy.

Quote:
The net is the most power tools of the programmers and you must learning to use it.
That whole sentence just Doesn't not make any sense, at all. Did you write it in your own language, and let some sort of online translator translate it for you?

Quote:
In this article we will see how to create a simple chat program. The chat program will be just a server and a client, that you can connect from the internet ( or LAN ) and simply exchange text messages.
That must be the best sentence in the whole story!
What if two clients connect?
CyCloneNL is offline   Reply With Quote
Old 09-09-2009, 05:36 PM   #6
Newbie Member
 
Join Date: Sep 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
lespero is on a distinguished road

Re: Winsock with C#


I have try this example to my application.
I have a problem to w1.Accept(e.requestId)...when the program goes there the client disconnect...does anybody have the same problem???
lespero is offline   Reply With Quote
Old 11-06-2009, 02:25 PM   #7
Newbie Member
 
Join Date: Nov 2009
Location: Alkmaar, The Netherlands
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
Hrqls is on a distinguished road
Thumbs up

Re: Winsock with C#


thanks for the project, it works like a charm (after 1 fix)

i just had to change 1 piece to make it work :
in w1_ConnectionRequest you should not check if you are connected, but always close the connection before you accept the incoming connection.
Hrqls is offline   Reply With Quote
Old 05-28-2010, 09:11 PM   #8
Newbie Member
 
Join Date: May 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
Mostapha is on a distinguished road
Exclamation

Re: Winsock with C#


Please Hrqls,

I can't get make it work. could you send me your source code at mostapha#hotmail.com.

Anticipated thanks.

Mos.
Mostapha is offline   Reply With Quote
Old 08-26-2010, 04:04 PM   #9
Light Poster
 
Join Date: Aug 2010
Location: US
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
iamscottj is on a distinguished road

Re: Winsock with C#


I think I agree somewhat to CyCloneNL because the fact that if it is a chat program, why does it only interacts with only one computer as a client? It must support multiple computers too. Its if it can interact with multiple clients and the incoming message should contain an indication of who is sending the message else everything will be confusing. Very confusing.
iamscottj is offline   Reply With Quote
Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
100 Multiple choice questions in C coderzone C-C++ 115 09-02-2010 03:29 AM
C# interview questions Sanskruti C# 11 08-18-2010 04:58 PM
Huffman Encoding in C (Minimum Variance Encoding) rai_gandalf C-C++ 11 05-10-2010 12:07 AM
ANSI C Standard Amit Ray C-C++ 1 03-06-2008 12:38 PM
How to use COM Interop in C# Nataraj1978 C# 1 02-20-2006 08:49 PM

 

All times are GMT +5.5. The time now is 05:08 AM.