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 4.2


Networking 301

Using REALbasic classes for easy networking

Issue: 4.2 (November/December 2005)
Author: Aaron Ballman
Author Bio: Aaron is the networking goto guy at REAL Software and has just demonstrated the only valid use of the goto statement. In his spare time, Aaron likes to kidnap and ransom off garden gnomes to finance his video game habits.
Article Description: No description available.
Article Length (in bytes): 20,003
Starting Page Number: 27
Article Number: 4212
Related Link(s): None

Excerpt of article text...

Previously, we learned all about what the TCP and UDP protocols are as well as how to use them from within REALbasic. Now we are going to dig deeper into various classes that make use of our newfound knowledge. This article will cover how the ServerSocket works as well as delve into the Easy Networking classes that RB has to offer.

Making your own serverAll Aaron's Subheads were level 2, so I changed them to level 1.

Have you ever wanted to have a socket always listening on a specific port so that you can hand connections off to helper sockets? For example, have a socket listening on port 80 (for HTTP connections) that allows you to serve up web pages. The ServerSocket allows you to do just this! It is a special type of socket that will listen on the port specified (it does not connect out to anything) and hand incoming connections off to other sockets.

Under the hood, the ServerSocket is just a regular TCP socket with a few extra features. When the socket first starts listening, we fire the AddSocket event so that we can fill up our initial socket buffer. This is the buffer that we use to hand the connections off to. So the socket starts listening on the port you specify, and as connections come in, it will grab a socket from the internal buffer and hand the connection off to it. It will only hand the connection off if it does not violate the maximum number of sockets connected property. The beauty of this sort of a scheme is in the fact that at no point in time is the server socket not listening for incoming connections. Because it actually accepts the connection then immediately hands the connection off to another socket, you will not miss any incoming connections. Due to the nature of the ServerSocket, it does not have the capabilities to send and receive data. If you think about it, this makes perfect sense. The ServerSocket never actually accepts a connection itself, so it will never have the chance to send any data out. Something else that should make sense to you is the reasoning behind why you have to return a TCPSocket from the AddSocket event. Since the ServerSocket serves up connections to other sockets, then it can only work with a protocol that relies on connections. Due to the fact that UDP is a connectionless protocol, it wouldn't make sense to return a UDPSocket.

Before we get into how to actually use the ServerSocket, let's take a moment to talk about its very misunderstood AddSocket event.

AddSocket explained

...End of Excerpt. Please purchase the magazine to read the full article.