Click here to Skip to main content
12,072,067 members (61,005 online)
Click here to Skip to main content
Add your own
alternative version

Stats

81.3K views
2.7K downloads
109 bookmarked
Posted

Class for Communication using Windows Sockets and TCP/IP

, 7 Dec 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

JonataCB
Software Developer
Brazil Brazil
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralThanks Pin
Jaime Stuardo - Chile14-May-15 15:48
memberJaime Stuardo - Chile14-May-15 15:48 
GeneralMy vote of 5 Pin
tommy Burns21-Mar-12 21:58
membertommy Burns21-Mar-12 21:58 
Questionhow to send and receive file Pin
happyw200427-Nov-10 4:53
memberhappyw200427-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
kolja2003@yandex.ru5-Feb-09 3:19
memberkolja2003@yandex.ru5-Feb-09 3:19 
QuestionObjects? Pin
nick_journals16-Jun-08 3:25
membernick_journals16-Jun-08 3:25 
GeneralSome features for the new article... Pin
nick_journals6-May-08 6:00
membernick_journals6-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
kolja2003@yandex.ru5-Feb-09 2:22
memberkolja2003@yandex.ru5-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 
GeneralRe: Could you please translate these projects to English... Pin
JonataCB27-Jun-07 5:02
memberJonataCB27-Jun-07 5:02 
QuestionExample Pin
Mahdi Afsar15-Jun-07 3:46
memberMahdi Afsar15-Jun-07 3:46 
AnswerRe: Example Pin
JonataCB20-Jun-07 9:45
memberJonataCB20-Jun-07 9:45 
GeneralBroken archives Pin
Yury Goltsman12-Jun-07 23:43
memberYury Goltsman12-Jun-07 23:43 
QuestionHow to send message to a special client? Pin
Mad20069-Jun-07 0:30
memberMad20069-Jun-07 0:30 
AnswerRe: How to send message to a special client? Pin
JonataCB12-Jun-07 9:53
memberJonataCB12-Jun-07 9:53 
GeneralGreat Pin
merlin9818-Jun-07 9:53
membermerlin9818-Jun-07 9:53 
GeneralJust for me... Pin
ahfauth29-May-07 8:02
memberahfauth29-May-07 8:02 
GeneralRe: Just for me... Pin
ahfauth30-May-07 7:56
memberahfauth30-May-07 7:56 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160208.1 | Last Updated 7 Dec 2007
Article Copyright 2007 by JonataCB
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid