|
|
|
|
|
- Overview
- Principle of communicating by sockets
- Step 1: Socket creation
- Step 2: Waiting for socket connection
- Example
- Step 3: Exchanging data
- Step 4: End communication
- Transmission mode of information
Socket: create 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: Socket creation 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 through programming) and a port number are associated with each socket. Step 2: Waiting for socket connection All workstations wishing to communicate with the server can connect to the socket: these are client workstations.. 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 (function 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 Thread management).
- refuse the connection (SocketDeny).
Example 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 executed by different threads simultaneously, you need to change the thread synchronization mode: use the ThreadMode function with the constant ThreadSectionCritique 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("Serveur", 8000) THEN
Error("erreur création " + ErrorInfo(errMessage))
ELSE
ThreadExecute("Thread1", threadNormal, ProcédureAttente)
END
PROCEDURE ProcédureAttente()
LOOP
IF SocketWaitForConnection("Serveur") THEN
Canal is string
Canal = SocketAccept("Serveur")
ThreadExecute(Canal, threadNormal, ProcédureGestion, Canal)
END
END
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 perform a read, a write must have been performed.. For example: - The client computer writes to the socket: it sends a request to the server.
- The server reads from 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: End 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 of the message is indicated at the beginning of the message.. This transmission mode is recommended when the sockets are used to communicate between two WINDEV applications.
The message is of the form: "11\r\nHelloWord". - Method 2: standard mode: end of message is signaled by a specific, pre-determined character. 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 protocols most commonly used on the Internet.
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|