Table of Contents

24.1.2.0 Class ServerSocket (gb.net)

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

24.1.2.0.1 Accept() method

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

24.1.2.0.2 Properties

The ServerSocket class has these properties:

PropertyData typeDescription
CountIntegerReturns the number of connection sockets created with the Accept() method.
TypeIntegerReturns or sets the socket type to use. The socket type can be one of the following values: Net.Internet (1) or Net.Local (0).
PortIntegerReturns or sets the server socket port for sockets of server type Net.Internet.
PathStringReturns or sets the server socket path for Unix sockets of server type Net.Local.
StatusIntegerReturns the status of the server socket as a constant of the Net class.
TimeoutIntegerReturns 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

24.1.2.0.3 Methods

The ServerSocket class has the following methods:

MethodReturn typeDescription
Accept ( ) SocketUse 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:

24.1.2.0.4 Events

The ServerSocket class has only these two events:

EventDescription
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: