User Tools

Site Tools


Sidebar

Network and communication

k24:k24.1:k24.1.2:start

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:

  • Synonymous with Net.Local, Net.Unix is used.
  • A socket timeout limits the amount of time allowed for communication between connections. This can be the establishment of a connection or the sending of data. It is used to prevent interfering connections from consuming resources and to increase security by closing broken connections. This property can be changed whenever necessary or useful. One case where you may want to close new connections is when you need to perform a large data transfer and want to give it priority. This way you can prevent new connections from making the same type of data transfer request and overloading your server.
 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:

  • The MaxConn parameter can be 0 - no limit on the number of connections or greater than 0, specifying the maximum number of connections active at any one time.
  • If you do not use the optional parameter MaxConn, then there is no limit to the number of connections because the MaxConn property is then automatically set to 0 internally.
  • After calling the Close() method, the server status is set to Net.Inactive. All socket clients that were previously created to manage the individual connections between client and server and through which the communication between client and server runs are set to the status 'inactive'.
  • You can also use the Pause() method if the socket is not currently waiting for connections. However, when it then starts waiting for connections, it will deny all incoming connections until you call the Resume() method.

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:

  • Connection(…) - To accept and work with a connection, use the Accept() method. To reject it, simply do nothing - leave the event unhandled. The connection with this client is closed. The 'RemoteHostIP' parameter is the IP address of the client that tried to connect to the server.
  • Error() - The value of the error constant can help you identify the reason for an error: Net.CannotCreateSocket (-2), Net.CannotBindSocket (-10) or Net.CannotListen (-14).
The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k24/k24.1/k24.1.2/start.txt · Last modified: 18.06.2022 (external edit)

Page Tools