- In this lesson you will learn the following concepts
- Overview
- Server application: simplified server
- Creating the socket
- Exchanging data
- Closing the socket
- Client application
- Connecting to the server
- Exchanging data
- Ending the communication
- Practical example
- Example test
- Analyzing the code used
In this lesson you will learn the following concepts - Overview.
- Server application.
- Client application.
WINDEV includes advanced socket management functions. A socket is a communication resource used by applications to communicate from one computer to another regardless of the type of network. This communication mode is used to establish communication between two applications on different computers (connected via Internet or on the same local network) or on the same computer. WINDEV applications can manage sockets according to different modes: - WINDEV Client application: the application connects to any server and exchanges data via a socket.
- WINDEV, WEBDEV or WINDEV Mobile "Server" application: the WINDEV, WEBDEV or WINDEV Mobile application is a server that exchanges information via sockets with multiple client machines. To manage multiple simultaneous connections, it is necessary to use threads.
| | |  | Example | WINDEV includes a training example that allows you to understand the use of sockets: "WD Using sockets". This example is accessible from the WINDEV home page (Ctrl + <). |
Server application: simplified server WINDEV gives you the ability to create a simplified socket server. This server communicates with a single client computer at a time. This type of application is very useful when two remote applications must communicate. The steps for creating a simplified server are as follows: - Creating the socket.
- Exchanging data.
- Closing the socket.
Creating the socket To create the socket, the server uses SocketCreate. A socket is bound to a specific port. To easily use sockets programmatically on the server, specify the socket name. The client machine will connect to the socket to exchange data. The connection will be actually established at the first data exchange between the two machines (i.e. when the server reads information for the first time). The connection is established the first time SocketRead reads information on the server successfully. Exchanging data When two machines use the same socket, a communication stream is established between them. These two machines can read from and write character strings to the socket. Remark: To avoid blocking applications, the reception of messages is often handled by a specific thread. 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 reads data 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 machine (SocketClientInfo) and sends a response.
Closing the socket To end the communication, the server can close the socket with SocketClose. Remark: the socket can also be closed by the client machine. A client application of a socket server connects to a standard server to exchange information via a socket. Example: A client WINDEV application can connect to a standard news server on Internet. The steps for creating a client application are as follows: - Connecting to the server.
- Exchanging data.
- Ending the communication.
Connecting to the server To connect to a server socket, all you have to do is use SocketConnect. This function makes a connection request to the server. The socket is identified by its port and address. Exchanging data When two machines use the same socket, a communication stream is established between them. These two machines can read from and write character strings to the socket. Remark: To avoid blocking applications, the reception of messages is often handled by a specific thread. Ending the communication To end the communication, close the socket from the client machine with SocketClose. Remark: you also have the ability to end the communication from the server. The programming of sockets will be presented via the unit example named "Using sockets". Example test - Open the unit example named "Using sockets".
- Test the "WIN_Socket" window. A message will be sent from computer B to computer A. Computer A is the Server application and computer B is the Client application.
- On computer A, click the "Create" button to create the socket.
- Computer B can connect to the socket created by computer A. Simply click the "Connect" button (in the Computer B section).
- Computer B sends a message to computer A:
- Type the message to be sent in the "Sentence to send to computer A" field.
- Click "Send" in the "Computer B" area.
- To retrieve the message on computer A, click the "Get" button in the "Computer A" area.
- Click the "Disonnect" buttons to disconnect the two computers.
- Stop the window test to go back to the editor.
Analyzing the code used - Let's take a look at the code of the different buttons that have been used.
- First, we will analyze the processes performed by the socket server (computer A).
- Open the code of the "Create" button in the "Computer A" area:
- Select the "Create" button.
- Press F2 to open the events.
- In the "Click" event, you will see SocketCreate, which is used to create the socket. Close the code editor.
- Open the code of the "Get" button in the "Computer A" area:
- Select the "Get" button.
- Press F2 to open the events.
- The following code is used in the "Click" event:
EDT_SentenceReceivedFromComputerB = SocketRead("ComputerA", False, 2000) IF EDT_SentenceReceivedFromComputerB <> "" THEN Info("Message received from the IP address # " + SocketClientInfo("ComputerA", SocketAddress)) END
SocketRead reads the socket created previously. The message read is immediately displayed in the "EDT_SentenceReceivedFromComputerB" control. Close the code editor.
- Let's take a look at the processes performed by the client (computer B).
- Open the code of the "Connect" button in the "Computer B" area:
- Select the "Connect" button.
- Press F2 to open the events.
- In the "Click" event, you will see SocketConnect, which is used to connect to the socket created by computer A. Close the code editor.
- Open the code of the "Send" button in the "Computer B" area:
- Select the "Send" button.
- Press F2 to open the events.
- The following code is used in the "Click" event:
IF NOT SocketWrite("ForComputerA", EDT_SentenceToSendToComputerA) THEN Error(ErrorInfo(errMessage)) RETURN END
SocketWrite sends a message to the socket to which computer B is connected. Close the code editor.
Remark: In this lesson, we have seen a "simple" communication between a server and a client computer: the client sends messages and the server processes them. It is also possible to create more complex applications. Communication can also be established between two applications, both client and server. In this case, threads are very useful to manage sent messages and responses.
|
|
|
|