ServerSocket

From Xojo Documentation

Class (inherits from Object)

Used to support multiple connections on the same port.

Events
AddSocket Error
Properties
Handle fa-lock-32.png MaximumSocketsConnected Port
IsListening fa-lock-32.png MinimumSocketsAvailable
LocalAddress fa-lock-32.png NetworkInterface
Methods
ActiveConnections Listen StopListening

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:

ListBox1.AddRow("Added Socket")
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:

ListBox1.AddRow("Socket " + Str(mNum) + " connected.")
Me.Write("From the server socket, I connected to you.")

Its DataAvailable event handler is:

ListBox1.AddRow("Socket " + Str(mNum) + " got: " + Me.ReadAll)

The SendComplete event handler is:

ListBox1.AddRow("Socket " + Str(mNum) + " 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 Action 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 " + Str(Index) + " connected.")
Me.Write("Socket " + Str(Index) + "connected!")

The DataAvailable event handler reads the message and sends back a reply.

Listbox1.AddRow("Socket " + Str(Index) + ": " + Me.ReadAll)
Me.Write(" I got something from you.")

The Error event handler displays the error in the ListBox:

Listbox1.AddRow("Socket " + Str(Index) + ": Error = " + Str(Me.lastErrorCode))

The SendComplete event handler reports that send is complete:

ListBox1.AddRow("Socket " + Str(Index) + " send complete.")

See Also

SocketCore, SSLSocket, TCPSocket classes.