PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
  • This lesson will teach you the following concepts
  • Overview
  • Server application: for a 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
  • Studying the code used
Lesson 7.7. The sockets
This lesson will teach you the following concepts
  • Overview.
  • Server application.
  • Client application.

Estimated time: 20 mn
Previous LessonTable of contentsNext Lesson
Overview
WINDEV proposes several functions used to perform an advanced management of sockets.
A socket is a communication resource used by the applications to communicate between computers regardless of the network type.
This communication mode is used to establish a communication between two applications found on different computers (connected by Internet or on the same local network) or on the same computer.
A WINDEV application can manage the sockets according to different modes:
  • Client WINDEV application: the application connects to any server and it exchanges data via a socket.
  • WINDEV, WEBDEV or WINDEV Mobile "Server" application: the WINDEV, WEBDEV or WINDEV Mobile application is a server, exchanging information by sockets with several client computers. The threads must be used to manage several simultaneous connections.

Example

WINDEV is supplied with a training example allowing you to understand the use of sockets: "WD Using sockets". This example is accessible from the WINDEV home page (Ctrl + <).
Server application: for a simplified server
WINDEV gives you the ability to create a simplified socket server. This server allows you to communicate 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:
  1. Creating the socket.
  2. Exchanging data.
  3. Closing the socket.

Creating the socket

To create the socket, the server is using SocketCreate. A socket is associated with a specific port. For a simplified use of socket by programming on the server, specify the socket name.
The client computer will connect to this socket in order to exchange data. The connection between the two computers will be actually established during the first exchange of data between the two computers (which means when the server reads information for the first time).
The connection is established during the first successful attempt of SocketRead on the server.

Exchanging data

When two computers are using the same socket, a communication stream is established between these two computers. These two computers can read and write character strings on the socket.
Remark: To avoid locking the applications, the management of incoming messages is often performed by a specific thread.
To read and write on the socket, the WINDEV server application must use SocketRead and SocketWrite.
Caution: To perform a read operation, a write operation must have been done beforehand. For example:
  1. The client computer writes into the socket: it sends a request to the server.
  2. The server performs a read operation on the socket in a thread. As soon as a message is received, the message is processed by the server.
  3. If a response to the message is required, the server identifies the client computer (SocketClientInfo) and sends a response to it.

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 computer.
Client application
A client application of a socket server connects to a standard server in order to exchange information via 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:
  1. Connecting to the server.
  2. Exchanging data.
  3. Ending the communication.

Connecting to the server

To connect to a server socket, all you have to do is use SocketConnect. This function is used to perform a request for connecting to the server.
The socket is identified by its port and by its address.

Exchanging data

When two computers are using the same socket, a communication stream is established between these two computers. These two computers can read and write character strings on the socket.
Remark: To avoid locking the applications, the management of incoming messages is often performed by a specific thread.
To read and write on the socket, the WINDEV client application must use SocketRead and SocketWrite.

Ending the communication

To end the communication, all you have to do is close the socket from the client computer with SocketClose.
Remark: you also have the ability to end the communication from the server.
Practical example
The programming of sockets will be presented via the unit example named "Using sockets".

Example test

  • Open the unit example named "Using sockets".
  • Run the test of "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.
    1. On computer A, click the "Create" button to create the socket.
    2. Computer B can connect to the socket created by computer A. All you have to do is click the "Connect" button (in Computer B section).
    3. Computer B sends a message to computer A:
      • Type message to send in the "Sentence to send to computer A" area.
      • Click the "Send" button found in the "Computer B" area.
    4. To retrieve the message on computer A, click the "Get" button found in the "Computer A" area.
    5. Click the "Disonnect" buttons to disconnect the two computers.
  • Stop the window test to go back to the editor.

Studying the code used

  • Let's study the code of the different buttons that have been used.
  • First of all, we are going to study the processes performed by the socket server (computer A).
    1. Display the code of "Create" button found in the "Computer A" area:
      • Select the "Create" button.
      • Press F2 to display the events.
    2. In the "Click" event, you will see SocketCreate, which is used to create the socket. Close the code editor.
    3. Display the code of "Get" button found in the "Computer A" area:
      • Select the "Get" button.
      • Press F2 to display the events.
    4. The following code is used in the "Click" event:
      EDT_SentenceReceivedFromComputerB = SocketRead("ComputerA", False, 2000)
      IF EDT_SentenceReceivedFromComputerB <> "" THEN
      Info("Message received from IP address # "+...
      SocketClientInfo("ComputerA", SocketAddress))
      END
      You will notice the presence of SocketRead that is used to read the socket that was created beforehand. The message read is immediately displayed in the "EDT_SentenceReceivedFromComputerB" control. Close the code editor.
  • Let's study the processes performed by the client (computer B).
    1. Display the code of "Connect" button found in the "Computer B" area:
      • Select the "Connect" button.
      • Press F2 to display the events.
    2. In the "Click" process, you will see SocketConnect, which is used to connect to the socket created by computer A. Close the code editor.
    3. Display the code of "Send" button found in the "Computer B" area:
      • Select the "Send" button.
      • Press F2 to display the events.
    4. The following code is used in the "Click" event:
      IF NOT SocketWrite("ForComputerA", ...
      EDT_SentenceToSendToComputerA) THEN
      Error(ErrorInfo(errMessage))
      RETURN
      END
      You will notice the presence of SocketWrite that is used to send a message onto the socket to which computer B is connected. Close the code editor.
Note: In this lesson, we have seen a "simple" communication between a server and a client computer: the client sends messages and the server processes the messages. You have the ability to create more complex applications.
You have the ability to establish a communication between two applications, both client and server. In this case, the management of threads becomes very important to manage the send operations and the responses.
Previous LessonTable of contentsNext Lesson
Minimum version required
  • Version 24
Comments
Click [Add] to post a comment