Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

Class for Communication using Windows Sockets and TCP/IP

4.31/5 (24 votes)
7 Dec 2007CPOL3 min read 2   3.7K  
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)