Click here to Skip to main content
Click here to Skip to main content

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

Comments and Discussions

 
GeneralMy vote of 5 Pinmembertommy Burns21-Mar-12 21:58 
Questionhow to send and receive file Pinmemberhappyw200427-Nov-10 4:53 
GeneralMy vote of 4 PinmemberToli Cuturicu19-Aug-10 5:46 
GeneralCompact Framework Memory Leak Pinmemberarcticbrew15-Apr-10 12:08 
GeneralSimple and neat. Thanks. Pinmemberthompsons7-Aug-09 13:35 
GeneralHave one small remark about your events.. Pinmemberkolja2003@yandex.ru5-Feb-09 3:19 
QuestionObjects? Pinmembernick_journals16-Jun-08 3:25 
GeneralSome features for the new article... Pinmembernick_journals6-May-08 6:00 
Questionsendin large files PinmemberTika1-May-08 5:37 
AnswerRe: sendin large files PinmemberJonataCB3-May-08 5:10 
QuestionClass for Communication using Windows Sockets and TCP/IP PinmemberRShitole13-Apr-08 22:51 
AnswerRe: Class for Communication using Windows Sockets and TCP/IP PinmemberJonataCB3-May-08 5:11 
GeneralGet windows error 10054 when client disconnects PinmemberUwe Leinweber8-Nov-07 22:17 
GeneralRe: Get windows error 10054 when client disconnects Pinmemberkolja2003@yandex.ru5-Feb-09 2:22 
SuggestionRe: Get windows error 10054 when client disconnects PinmemberJacques Pelletier13-Dec-12 20:13 
GeneralCannot block a call on this socket while an earlier asynchronous call is in progress. Pinmemberpoortl91097-Sep-07 7:20 
GeneralRe: Cannot block a call on this socket while an earlier asynchronous call is in progress. PinmemberJonataCB10-Sep-07 2:45 
GeneralGreat job Pinmemberj7tech23-Jul-07 5:19 
GeneralActivate /deactivate the server PinmemberMrZigZag27-Jun-07 4:02 
GeneralRe: Activate /deactivate the server PinmemberJonataCB27-Jun-07 5:00 
AnswerRe: Activate /deactivate the server PinmemberMrZigZag2-Jul-07 23:56 
GeneralRe: Activate /deactivate the server PinmemberJonataCB23-Jul-07 4:20 
GeneralCould you please translate these projects to English... PinmemberDelicious26-Jun-07 20:11 
GeneralRe: Could you please translate these projects to English... PinmemberJonataCB27-Jun-07 5:02 
QuestionExample PinmemberMahdi Afsar15-Jun-07 3:46 
AnswerRe: Example PinmemberJonataCB20-Jun-07 9:45 
GeneralBroken archives PinmemberYury Goltsman12-Jun-07 23:43 
QuestionHow to send message to a special client? PinmemberMad20069-Jun-07 0:30 
AnswerRe: How to send message to a special client? PinmemberJonataCB12-Jun-07 9:53 
GeneralGreat Pinmembermerlin9818-Jun-07 9:53 
GeneralJust for me... Pinmemberahfauth29-May-07 8:02 
GeneralRe: Just for me... Pinmemberahfauth30-May-07 7:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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.1411022.1 | Last Updated 7 Dec 2007
Article Copyright 2007 by JonataCB
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid