Using REALbasic classes for easy networking
Issue: 4.2 (November/December 2005)
Author: Aaron Ballman
Article Description: No description available.
Article Length (in bytes): 18,414
Starting Page Number: 27
RBD Number: 4212
Resource File(s): None
Related Link(s): None
Known Limitations: 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.
...End of Excerpt. Please purchase the magazine to read the full article.
Article copyrighted by REALbasic Developer magazine. All rights reserved.