The ServerSocket (gb.net) class is used to enable server-side waiting for incoming connections and their establishment. It is not intended for the actual communication. The class can be used for both the TCP and Unix protocols. Note: Unix sockets have their own chapter in '24.1.4 UnixSocket'.
The class performs its work asynchronously so that your programme is not stopped by the internal work of the server object.
This is how you create a new Internet server socket:
Dim hServerSocket As ServerSocket hServerSocket = New ServerSocket ( [ Port As Integer, MaxConn As Integer ] ) As "EventName"
Alternative:
Dim hServerSocket As ServerSocket hServerSocket = New ServerSocket As "EventName" hServerSocket.Type = Net.Internet hServerSocket.Port = iPort hServerSocket.Listen(10) ' Der Server baut maximal 10 TCP/IP-Verbindungen auf
To create a new Unix server socket:
Dim hServerSocket As ServerSocket hServerSocket = New ServerSocket ( [ Path As String, MaxConn As Integer ] ) As "EventName"
Alternative:
Dim hServerSocket As ServerSocket hServerSocket = New ServerSocket As "EventName" hServerSocket.Type = Net.Unix hServerSocket.Path = sPath hServerSocket.Listen(10) ' Der Server baut maximal 10 Verbindungen auf
This class is designed to present a server that accepts or rejects requested connections. All the rest of the work, such as sending data or receiving data, is done by a socket that is created and started by the server, one per connection server ↔ client.
Use the following control structure to inspect all sockets created with the Accept() method:
Dim hServerSocket As ServerSocket Dim hConnetionSocket As Socket For Each hConnetionSocket In hServerSocket ... Next
The ServerSocket class has these properties:
Property | Data type | Description |
---|---|---|
Count | Integer | Returns the number of connection sockets created with the Accept() method. |
Type | Integer | Returns or sets the socket type to use. The socket type can be one of the following values: Net.Internet (1) or Net.Local (0). |
Port | Integer | Returns or sets the server socket port for sockets of server type Net.Internet. |
Path | String | Returns or sets the server socket path for Unix sockets of server type Net.Local. |
Status | Integer | Returns the status of the server socket as a constant of the Net class. |
Timeout | Integer | Returns or sets the server socket timeout in milliseconds. |
Table 24.1.2.0.1 : Properties of the ServerSocket class.
Notes:
If sBuf = "start-massive-transfer" Then btnPause.Enabled = False Print "Doing massive transfer." MyServerSocket.Pause() '-- Prevent this priority connection from keeping the others waiting too long '-- Verhindern Sie, dass diese Prioritätsverbindung die anderen zu lange warten lässt MyServerSocket.Timeout = 10 * 1000 Else If sBuf = "end-massive-transfer" Then Print "Massive transfer done." MyServerSocket.Timeout = 0 '-- Retain btnPause control of Pause/Resume state '-- btnPause-Steuerung des Pause/Fortsetzungs-Status beibehalten If Not Waiting Then MyServerSocket.Resume() btnPause.Enabled = True Endif
The ServerSocket class has the following methods:
Method | Return type | Description |
---|---|---|
Accept ( ) | Socket | Use this method to accept a connection request from a client. You must always use this method in the Connection event. The function returns a Socket object connected to the client, which you can use to manage this connection and perform communication. The new Socket object is automatically attached to the event observer “Socket”. |
Close () | - | Use this method to close all connections set up by the server and end the eavesdropping process. |
Listen ( [ MaxConn As Integer ] | - | Starts listening at the selected TCP port or local path. Optionally you can pass the parameter 'MaxConn'. |
Pause () | - | Use this method to keep all existing connections open. But they will not accept any more - until you use the Resume()-method. |
Resume () | - | Use this method to restart listening for new connection requests if you previously stopped this process with the Pause() method. |
Table 24.1.2.0.2 : Methods of the ServerSocket class.
Notes:
The ServerSocket class has only these two events:
Event | Description |
---|---|
Connection ( RemoteHostIP As String ) | The event triggered when a client tries to connect to the server (request). This is the only place in the source code where you can accept or reject this connection. |
Error ( ) | This event is triggered when errors occur within the Listen() method. The status property takes a value less than zero. |
Table 24.1.2.0.3 : Events of the ServerSocket class.
Notes: