From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

Class (inherits from Object)

Used to support multiple connections on the same port.

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


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.


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 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.


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 = ""
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 = " + Str(Me.lastErrorCode))

The SendComplete event handler reports that send is complete:

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

See Also

SocketCore, SSLSocket, TCPSocket classes.