Getting Started with the ServerSocket
Issue: 12.4 (July/August 2014)
Author: Christian Schmitz
Author Bio: Christian Schmitz is the creator of the Monkeybread Software Xojo/Real Studio Plugins.
Article Description: No description available.
Article Length (in bytes): 8,343
Starting Page Number: 19
Article Number: 12406
12406project.zip Updated: 2014-07-03 00:14:30
Related Link(s): None
Excerpt of article text...
In this article, I want to show you how to get started with the ServerSocket. In general, the Serversocket allows you to accept connections from outside and pass them to individual sockets which handle the actual transfer. As the ServerSocket keeps a pool of waiting sockets, the ServerSocket can very quickly accept connections. The operating system buffers the data on the socket, so your code can process them one after the other without a problem or performance issues. Only if the pool of available sockets is too small, you will see connections dropped.
Setting up ServerSocket
The first thing to do is to create a new subclass of ServerSocket. You add a class to your project and set the super class to ServerSocket. We name it
MyServerSocketfor the example project. In this class, you add events
Error. In the
Errorevent, you can put code to log errors like
AddessInUseError(two constants defined in
AddSocketevent, you have the job to add a new socket. You probably have a TCPSocket subclass which you use for the ServerSocket's client sockets. In the example, I added a class named
MySocketand added a property ID there. With the ID property, I plan to give each socket its own unique ID. So, in our AddSocket event, I declare a static variable counter. Making the variable static makes sure that all calls to the AddSocket event refer to the same global counter variable. This way we can count it up and assign the current value to the new socket. Finally, we return the new socket to be added to the socket pool. In our example the event looks like this:
Function AddSocket() As TCPSocket
...End of Excerpt. Please purchase the magazine to read the full article.