Special

Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 21 in printed book and digital formats -- plus a one-year subscription (beginning with 22.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview


Buy Now

Issue 12.4 ('Game Center')
Instant purchase and download via GumRoad!

FEATURE

Inside ServerSocket

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
Resource File(s):

Download Icon 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 MyServerSocket for the example project. In this class, you add events AddSocket and Error. In the Error event, you can put code to log errors like InvalidPortError or AddessInUseError (two constants defined in SocketCore class).

In the AddSocket event, 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 MySocket and 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.