Click here to Skip to main content
15,169,244 members
Articles / Desktop Programming / Win32
Posted 29 May 2007


111 bookmarked

Class for Communication using Windows Sockets and TCP/IP

Rate me:
Please Sign up or sign in to vote.
4.31/5 (24 votes)
7 Dec 2007CPOL3 min read
Classes with many useful properties and methods to communicate using TCP/IP


I have observed that many developers are lost when they have to work for the first time with Sockets, mainly those developers who were accustomed to Delphi's components to communicate using TCP/IP (TServerSocket and TClientSocket). Therefore, I felt motivated to create the libraries in the mould of the components of Delphi.

In addition to the two classes, I have developed an example of how to use the classes. Moreover, this is also a good example of use of delegates and events.


Change the names of the classes for CServerSocket and CClientSocket. This way (the name of classes starting with the letter C) both can be associated with C#, which was used to develop the classes, but it is also a reminder to use the "CallBack". Despite examples of the use of Sockets with the method Thread.Start() did not see many reasons to use it. I did some tests and realized that there are two advantages to use "Callback" instead of threads.

  1. The development is easier and flexible
  2. The application responds faster

One question that is often given is: "How many connections does the Socket support?" Well, what happens is that when a Client is connected to the server, that Client is redirected to another port so that the server continues waiting for a call in the initial port. This means that as there are 65535 ports, the socket could receive up to 65535 calls. But we always consider the computer's processing capacity and the ports used by other applications.

As the purpose of this article is to show how to use the methods and properties of Sockets to create an efficient class, I will stick to explaining the methods and properties of these classes. Possibly in the future, I will complete this article with a more detailed explanation of Delegates and Callback.


All methods and properties are fairly intuitive. Therefore, I would like to comment only on some details.

The properties ReceivedBytes and ReceivedText act as buffers. So when one of them is read, its content is erased. They should be used in conjunction with the event OnRead.

The same applies to the property WriteText that returns a string that was passed by the connection. It should be used in conjunction with the event OnWrite.

The builder of the class requires that the parameters to establish the connection are informed – CClientSocket(string IP, int port). However, the connection is established only with the method Connect().

The method SendFile() has the option to send or not send messages before and after being sent the file. This can be used to create a manager for the transfer of files, creating a suitable protocol for communication.


The properties ReceivedBytes, ReceivedText and WriteText have the same function of the other class.

In instantiation of a class, it is necessary to inform the door in which the class will be waiting for a call. With the method Active(), the class begins to wait for a call and the event OnListen is fired.

With the method Deactive(), the class fails to listen to the port and loses all connections. However, with the method CloseConnection(int SocketIndex), it is possible to finish only a specific connection.

The other methods are very intuitive.


The example is also very intuitive. It is a server that shows some properties of the connected clients and sends messages to specific clients or to all customers. It also has two clients. So that they can connect, you must use IP and the door of your choice.

You can also see how the events of the classes are implemented.

You are not required to use the clients of the application example. You can use the HyperTerminal, for example.

How could a server manage the contents of customers? An ArrayList solves the problem, but I use the ComboBox.

A method that has not been implemented in the example, but is very intuitive is the sending and receipt of the file (SendFile()).



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Brazil Brazil
No Biography provided

Comments and Discussions

QuestionServer report error when client disconnect Pin
equelna11-Aug-16 0:21
Memberequelna11-Aug-16 0:21 
GeneralThanks Pin
Jaime Stuardo - Chile14-May-15 15:48
MemberJaime Stuardo - Chile14-May-15 15:48 
I am developing two .NET 4.5 applications needing to communicate between them by mean of Tcp/Ip... this class works perfectly and avoided me to program communication routines by myself.

GeneralMy vote of 5 Pin
Tommy Burns21-Mar-12 21:58
MemberTommy Burns21-Mar-12 21:58 
Questionhow to send and receive file Pin
dongwang_fl27-Nov-10 4:53
Memberdongwang_fl27-Nov-10 4:53 
GeneralMy vote of 4 Pin
Toli Cuturicu19-Aug-10 5:46
MemberToli Cuturicu19-Aug-10 5:46 
GeneralCompact Framework Memory Leak Pin
arcticbrew15-Apr-10 12:08
Memberarcticbrew15-Apr-10 12:08 
GeneralSimple and neat. Thanks. Pin
thompsons7-Aug-09 13:35
Memberthompsons7-Aug-09 13:35 
GeneralHave one small remark about your events.. Pin
yqt15-Feb-09 3:19
Memberyqt15-Feb-09 3:19 
QuestionObjects? Pin
Nick__J16-Jun-08 3:25
MemberNick__J16-Jun-08 3:25 
GeneralSome features for the new article... Pin
Nick__J6-May-08 6:00
MemberNick__J6-May-08 6:00 
Questionsendin large files Pin
Tika1-May-08 5:37
MemberTika1-May-08 5:37 
AnswerRe: sendin large files Pin
JonataCB3-May-08 5:10
MemberJonataCB3-May-08 5:10 
QuestionClass for Communication using Windows Sockets and TCP/IP Pin
RShitole13-Apr-08 22:51
MemberRShitole13-Apr-08 22:51 
AnswerRe: Class for Communication using Windows Sockets and TCP/IP Pin
JonataCB3-May-08 5:11
MemberJonataCB3-May-08 5:11 
GeneralGet windows error 10054 when client disconnects Pin
Uwe Leinweber8-Nov-07 22:17
MemberUwe Leinweber8-Nov-07 22:17 
GeneralRe: Get windows error 10054 when client disconnects Pin
yqt15-Feb-09 2:22
Memberyqt15-Feb-09 2:22 
SuggestionRe: Get windows error 10054 when client disconnects Pin
Jacques Pelletier13-Dec-12 20:13
MemberJacques Pelletier13-Dec-12 20:13 
GeneralCannot block a call on this socket while an earlier asynchronous call is in progress. Pin
poortl91097-Sep-07 7:20
Memberpoortl91097-Sep-07 7:20 
GeneralRe: Cannot block a call on this socket while an earlier asynchronous call is in progress. Pin
JonataCB10-Sep-07 2:45
MemberJonataCB10-Sep-07 2:45 
GeneralGreat job Pin
j7tech23-Jul-07 5:19
Memberj7tech23-Jul-07 5:19 
GeneralActivate /deactivate the server Pin
MrZigZag27-Jun-07 4:02
MemberMrZigZag27-Jun-07 4:02 
GeneralRe: Activate /deactivate the server Pin
JonataCB27-Jun-07 5:00
MemberJonataCB27-Jun-07 5:00 
AnswerRe: Activate /deactivate the server Pin
MrZigZag2-Jul-07 23:56
MemberMrZigZag2-Jul-07 23:56 
GeneralRe: Activate /deactivate the server Pin
JonataCB23-Jul-07 4:20
MemberJonataCB23-Jul-07 4:20 
GeneralCould you please translate these projects to English... Pin
Delicious26-Jun-07 20:11
MemberDelicious26-Jun-07 20:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.