- Principle of communicating by sockets
- Step 1: Creating the socket
- Step 2: Waiting for the connection to the socket
- Step 3: Exchanging data
- Step 4: Ending the communication
- Transmission mode of information
Socket: creating a standard socket server
WINDEV allows you to create a standard socket server. This server is used to manage the connection of several client computers on the same socket.
This principle is used to create a news server for example.
Principle of communicating by sockets
Step 1: Creating the socket
To create the socket, the server uses SocketCreate
. A socket is bound to a specific port. Several sockets can be created, each socket using a specific port number. A name (used to handle the socket by programming) and a port number are associated with each socket.
Step 2: Waiting for the connection to the socket
All the computers that want to communicate with the server can connect to the socket: these are the client computers. The server manages the different connection requests from the client computers via SocketWaitForConnection
. This function allows you to find out whether a connection request is performed on a specific socket.
It is recommended to use SocketWaitForConnection
in a specific thread. Therefore, this function is performed in background task. When a connection request is detected, you can:
- accept the connection (SocketAccept): in this case, a specific communication channel is created. To avoid locking applications, incoming messages are often handled by a specific thread (for more details, see Managing the threads).
- refuse the connection (SocketDeny).
The following example is used to create a socket on the server and to manage the connections of the client computers in a thread. If the connection is accepted, the management of this connection is performed by a specific thread.
A thread is run for each connection. Each thread uses the same service function ("ManagementProcedure"). To allow the procedure to be run by several threads at the same time, the synchronization mode of threads must be changed: use ThreadMode
associated with the threadCriticalSection
constant in the project initialization code. The synchronization between threads must be done manually (for more details, see the help about threads
To differentiate the threads, the name of each thread corresponds to the name of the communication channel (unique name).
IF NOT SocketCreate("Server", 8000) THEN
Error("creation error" + ErrorInfo(errMessage))
ThreadExécute("Thread1", threadNormal, WaitProcedure)
IF SocketWaitForConnection("Server") THEN
Channel is string
Channel = SocketAccept("Server")
ThreadExécute(Channel, threadNormal, ProcedureManagement, Channel)
Step 3: Exchanging data
When two machines use the same socket, a communication channel is established between them. These two machines can read from and write character strings to the socket.
Caution: To start a read operation, a write operation must have been done previously. For example:
- The client machine writes to the socket: it sends a request to the server.
- The server performs a read operation on the socket in a thread. When a message is received, it is processed by the server.
- If a response to the message is required, the server identifies the client computer and sends a response to it.
Step 4: Ending the communication
To end the communication, all you have to do is close the socket with SocketClose
Transmission mode of information
The transmission mode of the message defines the mode used to specify the length of the message.
Several methods can be used to define the length of the message during the communication between sockets.
- Method 1: WINDEV mode: By default, the number of characters in the message is specified at the beginning of message. This transmission mode is recommended when the sockets are used to communicate between two WINDEV applications.
The message has the following format: "11\r\nHelloWord"
- Method 2: standard mode: The end of the message is signaled by a specific character, defined in advance. This transmission mode is recommended when the sockets are used to communicate between a WINDEV application and another application. In this case, a specific character must be included in the message to indicate that the message is over.
The message has the following format: "Hello world<EOF>"
- Method 3: standard mode with buffer: Corresponds to the standard mode optimized for the most frequently used protocols on Internet.
This page is also available for…