PC SOFT

ONLINE HELP
FOR WINDEV, WEBDEV AND WINDEV MOBILE

Home | Sign in | English US
  • Overview
  • Principle of communicating by sockets
  • Step 1: Creating the socket
  • Step 2: Waiting for the connection to the socket
  • Example
  • Step 3: Exchanging data
  • Step 4: Ending the communication
  • Transmission mode of information
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadApple WatchUniversal Windows 10 AppWindows Mobile
Others
Stored procedures
Socket: creating a standard socket server
Overview
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.
Versions 23 and later
Universal Windows 10 App This feature is now available in Universal Windows 10 App mode.
New in version 23
Universal Windows 10 App This feature is now available in Universal Windows 10 App mode.
Universal Windows 10 App This feature is now available in Universal Windows 10 App mode.
Principle of communicating by sockets

Step 1: Creating the socket

To create the socket, the server is using SocketCreate. A socket is associated with 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.
We recommend that you 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 the applications, the management of incoming messages is often performed by a specific thread (see Managing the threads for more details.)
  • 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 is using 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 initialization code of project. The synchronization between threads must be done manually (see the help about threads for more details).
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))
ELSE
ThreadExecute("Thread1", threadNormal, WaitProcedure)
END
PROCEDURE WaitProcedure()
LOOP
IF SocketWaitForConnection("Server") THEN
Channel is string
Channel = SocketAccept("Server")
ThreadExecute(Channel, threadNormal, ProcedureManagement, Channel)
END
END

Step 3: Exchanging data

When two computers use the same socket, a communication channel is established between these two computers. These two computers can read and write character strings on the socket.
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 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.
SocketChangeTransmissionMode allows you to modify the transmission mode used.
Minimum required version
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment