IM concepts and Jabber protocols
3.5 Testing the server
One of the simplest ways (if not the most tedious) to test the server is to use tel- net.16 First start the server. I have included a simple Windows batch file named server.bat with the online source code to start the server on Windows. I’ve also included a simple Unix shell script that will do the same on Unix17 and MacOS X.
15 You can stop the server by terminating the Java Virtual Machine (JVM): usually this can be done by
pressing CTRL+C.
16Telnet is a simple networking tool that should be included as part of any complete TCP/IP stack. It is
Testing the server 99
The server should start and print its banner “Jabber Server -- ####” where #### is the port number it is using. By convention, the port number for Jabber is 5222.
To test the server, open a console/terminal window and type: “telnet host- name port” where “hostname” is the Internet domain name or network address of the computer the server is running on and “port” is the port number that the Jab- ber server reported in its startup banner. If you’re running the server and telnet on a computer without a network you can use 127.0.0.1 as the host name (e.g., “telnet 127.0.0.1 5222” without the quotes).
Telnet will connect with the Jabber server and wait for you to type something. Type in the opening stream tag:18
<stream:stream from='user@server/resource' to='server'>
The server will respond with its stream tag, including a stream ID. Now close the stream by typing:
</stream:stream>
The server will respond with its closing stream then close the connection. Telnet will exit and you’ll be back at the prompt. A typical session should look something like the following:
Telnet testing the server
% telnet 127.0.0.1 5222 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. Send: <stream:stream Send: from='[email protected]/work' Send: to='shigeoka.com' Send: xmlns='jabber:client' Send: xmlns:stream='http://etherx.jabber.org/streams'> Recv: <stream:stream Recv: from='shigeoka.com' Recv: to='[email protected]/work' Recv: xmlns='jabber:client' Recv: xmlns:stream='http://etherx.jabber.org/streams' Recv: id='0'> Send: </stream:stream> Recv: </stream:stream>
Connection closed by foreign host.
17 I include Linux and the BSDs like FreeBSD in the Unix category.
18Our server does not check for the stream namespace attribute: xmlns='http://etherx.jabber.org/
streams' although it should. It is required by the Jabber protocols. The open source reference Jabber server rejects streams without this namespace.
100 CHAPTER 3
IM concepts and Jabber protocols
Now try the same thing with two telnet sessions in two console/terminal windows at the same time with different “from” Jabber ID’s to make sure the server can handle more than one connection at a time. Experiment by sending message packets addressed to the other telnet session to see how the server routes mes- sages. A message packet is typically of the form shown here:19
Sample Message Packet
<message from='[email protected]/work' to='[email protected]/home'> <body>
This is the message </body>
</message>
The server provides a good starting point for future explorations into the Jab- ber protocols. Now that we have a basic Jabber server, we need a basic Jabber cli- ent. Having both a client and server will allow us to test them against each other rather than debugging them using telnet.20 We’ll develop a Jabber client in the next chapter.
3.6
Conclusion
The code for a basic Jabber server introduced in this chapter can handle Jabber packets and valid Jabber XML streams. Throughout the rest of the book, we will add to this server to support other parts of the Jabber protocols.
The code demonstrates how simple—and powerful—the Jabber platform is. It allows us to quickly build Jabber software and embed it into all sorts of devices and applications. Using Jabber IM we can concentrate on building software sys- tems to do what we want without wasting time and resources on basic messaging infrastructure.
To show just how simple and fun Jabber can be, the next chapter introduces the most important and useful Jabber core protocol: message.
19 We’ll look at the message packet in detail in the next chapter.
20 Testing with telnet is a bit crude but shows the advantage of using XML rather than binary data formats.
We sacrifice network bandwidth efficiency but gain the ability to directly play with the protocols without any tools beyond telnet. Rather than type XML packets directly into telnet, though, I find it much eas- ier to type into a text editor and then cut and paste into the telnet window. That way I can edit the packets and reuse them.