>
Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline
Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro


Cure Your Waba Woes with a Serial Socket (Continued)

The only Socket method I haven't implemented is setReadTimeout(). I considered it unnecessary for the application I was crafting. For the sake of completeness, it could be implemented as a do-nothing method on the client side. (Or it could be implemented on the host side with minimal work. It would amount to adding a new command packet—I'll describe those in a moment—and adding code on the host to pass the request to the Socket object.)

 
Figure 2. | Packet Structure

Client and server communicate using the simple, variable-length packet scheme shown in Figure 2. Each packet begins with a five-byte header. The first four bytes of the header encode the packet length. (The packet length counts itself. So, the smallest possible packet would have a length count of five, because a header is the smallest possible packet.) Following the length is a single command byte. The protocol recognizes four commands, each corresponding to one of the methods implemented by spSocket.

ADVERTISEMENT

After the packet header comes an optional payload, whose length and content varies depending on the command. For an open operation, the payload carries the target port number in the first four bytes, followed by the string of the URL to be opened (encoding into a byte array). A read bytes packet has only a four-byte payload: the byte-encoded integer value representing the number of bytes to be read from the Socket. A write bytes packet's payload consists of the stream of bytes to be written to the Socket. (The number of bytes to be written is, of course, encoded in the packet's header.) Finally, the close operation packet has no payload; it consists of only a four-byte count and a single command byte.

The host proxy responds to requests with a similar packet format. The response to an open packet is simply a five-byte packet header. The command field is used to indicate whether the open was successful. The response to a request to read bytes can vary, depending on whether the read was successful. If unsuccessful, the read operation will return only a five-byte response (only a byte count, an error value in the command field, and no payload). If successful, the read operation will return a packet with a payload containing the bytes read from the Socket. (The number of bytes read might be less than the number of bytes requested to be read. But, the client can recognize that simply by looking at the packet length.)

The packet structure of a response to a request to write bytes is simply a packet header: a byte count, followed by a code indicating success or failure. The same structure is used in the response packet to a close request.

Back to top

Printer-Friendly Version












Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home