5.6. Testing server programs

Usually server programs are not interactive programs. There is no screen to output details to, and no user to read those details. So how do we test them, how do we know if they work?

Since we're usually working with plain ASCII, line-oriented data, we can use a telnet client to debug our server code.

With a telnet client, we'll type in the code that we expect the client program to send, and we'll see the exact responses that the server gives us. This is an invaluable tool for debugging sockets programs :)

Unfortunately (at least as of V4R5) the AS/400's TELNET command doesn't work nicely for debugging sockets programs. However, the Microsoft Windows and Unix (Linux, FreeBSD, etc) telnet clients work just fine.

So, let's test the server program:

  1. If you haven't done so already, compile the server program now.

  2. Run the server by typing: CALL SERVEREX1 (or whatever name you compiled it as)

  3. From Microsoft Windows, Click "Start", then "Run" then type: telnet as400 4000

    or, from a Linux/BSD/Unix shell, type: telnet as400 4000

  4. When it says 'Please enter your name now!', type your name.

    Note: Depending on your telnet client, you may not be able to see what you type -- this is normal

  5. It should respond with "hello" and "goodbye" and then disconnect.

this same technique can be used to debug just about any server program. For example, if you wanted to test our my web server, you might type:

  1. telnet www.scottklement.com 80

  2. Once connected, type: GET http://www.scottklement.com/rpg/socktut/ip_servertesting.txt HTTP/1.0

  3. press enter twice.

It's always interesting just how much of the Internet can be experienced with a simple telnet client

This first example server program doesn't know how to end. It'll just keep running forever, locking out port 4000. Sure, you could use system request to end the program, or you could do a WRKACTJOB and then 'End Job', but if you do that, OS/400 won't ever take port 4000 out of listen mode!

Therefore, the easiest way to end this program is by using the NETSTAT command.

  1. Sign on to your AS/400 from a different session and type: NETSTAT *CNN

  2. It will show you a list of TCP/IP ports being listened on, as well as showing anyone who is connected, and which remote and local ports they are connected with.

  3. If you have sufficient authority, you can place a '4' next to a given connection to end that connection.

  4. To end our server program, find the line that says that it is in 'Listen' state on port 4000. Put a '4' next to that line, and end that connection.

  5. The server program will end, it will say that it got an error: 'accept(): The protocol required to support the specified address family is not available at this time.' This is because OS/400 ends the connection by the protocol unavailable to the program for that particular socket.