ServerSocket
From Xojo Documentation
Used to support multiple connections on the same port.
Events | ||
|
Properties | |||||||
|
Methods | |||
|
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.
Examples
The following example listens for connections from a client application. The interface has a ListBox and a Pushbutton. A ServerSocket, mServerSocket, has been added to the window that has the following code in its AddSocket event handler:
Var ret As TCPSocket = New ClientSocket(ListBox1, curSocket)
curSocket = curSocket + 1
Return ret
ClientSocket is derived from TCPSocket and its constructor takes a ListBox and an Integer.
ClientSocket's Connected event is:
Me.Write("From the server socket, I connected to you.")
Its DataAvailable event handler is:
The SendComplete event handler is:
The property mNum is the passed value of curSocket in its Constructor.
The "Listen" button calls the Listen method.
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 Action event handler is:
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:
Me.Write("Socket " + Str(Index) + "connected!")
The DataAvailable event handler reads the message and sends back a reply.
The Error event handler displays the error in the ListBox:
The SendComplete event handler reports that send is complete:
See Also
SocketCore, SSLSocket, TCPSocket classes.