Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / Win32
Article

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 122.9K   3.7K   111   34
Classes with many useful properties and methods to communicate using TCP/IP

Introduction

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.

Background

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.

CClientSocket

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.

CServerSocket

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.

Example

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 127.0.0.1 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()).

Greetings!

License

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


Written By
Software Developer
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionServer report error when client disconnect Pin
equelna10-Aug-16 23:21
equelna10-Aug-16 23:21 
GeneralThanks Pin
Jaime Stuardo - Chile14-May-15 14:48
Jaime Stuardo - Chile14-May-15 14: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.

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