![]() |
Winsock with C#
On 8th March, 2007
|
Recent Articles
Similar Articles
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
We need 3 specific event to use : (Error,ConnectEvent,DataArrival) Code: CSharp
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
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
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
|
|
|
#2 |
|
Newbie Member
Join Date: Dec 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ![]() |
Re: Winsock with C#
I cannot find the link to download the example program. Does it exist?
|
|
|
|
|
|
#3 |
|
Newbie Member
Join Date: Feb 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ![]() |
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.
|
|
|
|
|
|
#4 |
|
Newbie Member
Join Date: Apr 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ![]() |
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.
|
|
|
|
|
|
#5 | ||||
|
Light Poster
Join Date: May 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ![]() |
Re: Winsock with C#Quote:
Quote:
Quote:
Quote:
What if two clients connect? |
||||
|
|
|
|
|
#6 |
|
Newbie Member
Join Date: Sep 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ![]() |
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??? |
|
|
|
|
|
#7 |
|
Newbie Member
Join Date: Nov 2009
Location: Alkmaar, The Netherlands
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ![]() |
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. |
|
|
|
![]() |
|
| Currently Active Users Reading This Article: 1 (0 members and 1 guests) | |
| Article Tools | Search this Article |
| Display Modes | |
| Bookmarks | |
|
|
|
|||||||||||||||||||||||||||||||||||||