Class
ServerSocket
Description
Used to support multiple connections on the same port.
Properties
Name |
Type |
Read-Only |
Shared |
---|---|---|---|
✓ |
|||
✓ |
|||
✓ |
|||
Methods
Name |
Parameters |
Returns |
Shared |
---|---|---|---|
Events
Name |
Parameters |
Returns |
---|---|---|
errorCode As Integer, err As RuntimeException |
Property descriptions
ServerSocket.Handle
Handle As Integer
This is the ServerSocket's internal descriptor and it can be used with Declare statements.
This property is read-only.
The descriptor is platform-specific. If Handle is less than zero, the descriptor is not available.
ServerSocket.IsListening
IsListening As Boolean
True when the ServerSocket is listening for incoming connections.
This property is read-only.
This example checks to see that the ServerSocket is listening.
If mServerSocket.IsListening Then
MessageBox(mServerSocket.NetworkInterface.IPAddress)
Else
MessageBox("Not listening")
End If
ServerSocket.LocalAddress
LocalAddress As String
The local address that the ServerSocket is using.
This property is read-only.
This example gets the local address of the computer that is running the ServerSocket.
TextField1.Text = mServerSocket.LocalAddress
ServerSocket.MaximumSocketsConnected
MaximumSocketsConnected As Integer
The maximum number of connections the ServerSocket will allow to connect.
When a socket disconnects from the ServerSocket, the server will allow one more connection.
Default value:
When instantiated in code, this defaults to 1. Starting with 2017r2, this defaults to 10.
When a ServerSocket is dragged onto the Window, this defaults to 10.
This example sets the MaximumSocketsConnected and MinimumSocketsAvailable properties.
mServerSocket.MaximumSocketsConnected = 25
mServerSocket.MinimumSocketsAvailable = 2
ServerSocket.MinimumSocketsAvailable
MinimumSocketsAvailable As Integer
The smallest number of sockets available in the server's pool of socket connections.
If the ServerSocket falls below this number, it will call the AddSocket event to replenish its supply of sockets.
Default value:
When instantiated in code, this defaults to 1. Starting with 2017r2, this defaults to 2.
When a ServerSocket is dragged onto the Window, this defaults to 2.
This example sets the MaximumSocketsConnected and MinimumSocketsAvailable properties.
mServerSocket.MaximumSocketsConnected = 25
mServerSocket.MinimumSocketsAvailable = 2
ServerSocket.NetworkInterface
NetworkInterface As NetworkInterface
The NetworkInterface that the ServerSocket is bound to.
See the examples for the NetworkInterface. This example gets the IPAddress that is being used.
If mServerSocket.IsListening Then
MessageBox(mServerSocket.NetworkInterface.IPAddress)
Else
MessageBox("Not listening")
End If
ServerSocket.Port
Port As Integer
The port to bind to for listening.
This example gets the Port that is being used by the ServerSocket.
TextField1.Text = mServerSocket.Port.ToString
Method descriptions
ServerSocket.ActiveConnections
ActiveConnections As TCPSocket()
Gets the array of active sockets managed by the ServerSocket. Returns an array of TCPSockets. These are the TCPSockets that were added via the AddSocket event and are now connected.
This example gets the array of connections. The array was declared as a property of the window.
Var ms() As TCPSocket
ms = mServerSocket.ActiveConnections
ServerSocket.Listen
Listen
Begins the listening process for the ServerSocket with the properties you specified.
Start listening on a previously created ServerSocket:
mServerSocket.Listen
ServerSocket.StopListening
StopListening
Terminates listening on the ServerSocket. It does not terminate any established connections.
This example terminates listening.
mServerSocket.StopListening
Event descriptions
ServerSocket.AddSocket
AddSocket As TCPSocket
The ServerSocket is requesting that you add a socket to its internal pool.
This is called when the ServerSocket first begins listening. This socket must stay resident (it cannot be a local variable) and must be non-Nil. It returns a TCPSocket. Since UDP is a connectionless protocol, it does not make sense for a ServerSocket to deal with UDPSockets.
ServerSocket.Error
Error(errorCode As Integer, err As RuntimeException)
Occurs when the socket has an error.
Standard operating system error codes or Xojo-specific errors are returned.
Notes
A ServerSocket is a permanent socket that listens on a single port for multiple connections. When a connection attempt is made on that port, the ServerSocket hands the connection off to another socket, and continues listening on the same port. Without the ServerSocket, it is difficult to implement this functionality due to the latency between a connection coming in, being handed off, creating a new listening socket, and restarting the listening process. If you had two connections coming in at about the same time, one of the connections may be dropped because there was no listening socket available on that port.
You can change the MinimumSocketsAvailable and MaximumSocketsConnected properties after establishing the listening socket. If you change the MaximumSocketsConnected property, it will not kill any existing connections (it just may not allow more connections until the existing connections have been released). If you change the MinimumSocketsAvailable property, it may fire the AddSocket event to replenish its internal buffer.
Binding a ServerSocket to a port below 1024 requires the proper privileges on all operating systems.
Sample code
The following example listens for connections from a client application. The interface has a DesktopListBox and a DesktopButton. A ServerSocket, mServerSocket, has been added to the window that has the following code in its AddSocket event handler:
ListBox1.AddRow("Added Socket")
Var ret As TCPSocket = New ClientSocket(ListBox1, curSocket)
curSocket = curSocket + 1
Return ret
ClientSocket is a subclass of TCPSocket and its constructor takes a DesktopListBox and an Integer.
ClientSocket's Connected event is:
ListBox1.AddRow("Socket " + mNum.ToString + " connected.")
Me.Write("From the server socket, I connected to you.")
Its DataAvailable event handler is:
ListBox1.AddRow("Socket " + mNum.ToString + " got: " + Me.ReadAll)
The SendComplete event handler is:
ListBox1.AddRow("Socket " + mNum.ToString + " send complete.")
The property mNum is the passed value of curSocket in its Constructor.
The "Listen" button calls the Listen method.
mServerSocket.Listen
This triggers the AddSocket event that creates the initial pool of sockets.
The client application has a "Connect" button to connect to the ServerSocket application, a ListBox to display status information, and an array of TCPSockets to make connections.
The Connect button's Pressed event handler is:
TCPSocket1(curSocket).Port = 2002
TCPSocket1(curSocket).Address = "127.0.0.1"
TCPSocket1(curSocket).Connect
curSocket = curSocket + 1
CurSocket is an Integer property of the window and is initialized to 0.
The TCPSocket has the following event handlers. The Connected event handler sends a message to the server application:
ListBox1.AddRow("Socket " + Index.ToString + " connected.")
Me.Write("Socket " + Index.ToString + "connected!")
The DataAvailable event handler reads the message and sends back a reply.
Listbox1.AddRow("Socket " + Index.ToString + ": " + Me.ReadAll)
Me.Write(" I got something from you.")
The Error event handler displays the error in the ListBox:
Listbox1.AddRow("Socket " + Index.ToString + ": Error = " + e.Message))
The SendComplete event handler reports that send is complete:
ListBox1.AddRow("Socket " + Index.ToString + " send complete.")
Compatibility
All project types on all supported operating systems.
See also
Object parent class; SocketCore, SSLSocket, TCPSocket classes.