Monday, August 22, 2011

Writing a TCP/IP Server sample c code


Here I will present you a simple TCP/IP server program. This server will replace the daytime service that was used in the previous post. Listing below shows the source code for the program. 

The basic steps for the program can be listed as
  1. Decide on the server network address for the server. The default is the local. loopback address of 127.0.0.1.
  2.  Decide on a server port number. Note that the default is port 9099.
  3.  Create a socket.
  4.  Create the server socket address.
  5.  Bind the socket address.
  6.   Mark the socket as a listening socket.
  7.  Start the client service loop.
  8.   Accept a client connection.
  9.  Generate a date and time string
  10. Write the date and time string back to the client.
  11.  Close the client connection.

Some steps require a bit of further explanation. In step 2, the program accepts a port number as an optional command-line argument two. This is necessary, because you don't want to run your version of the server on the standard port 13 (remember from the last chapter, your daytime server listens on that port). Instead, you'll use port 9099 by default, which does not require root privileges to run, and it won't disrupt your standard services. The command-line parameter permits you to use different port numbers if you prefer.



NOTE
TCP/IP ports 1 to 1023 are reserved for privileged programs (running as root). Nonprivileged programs use ports 1024 or greater.
Note also that a port value of zero is a wild port number. An unassigned port number will be assigned when bind (2) is called, if the port number is specified as zero.

Step 4 is different from previous programs. Line 81 has a call to strcmp(3) to see if the value "*" was given for the server address. If so, then a wild address is provided in lines 90 and 91. Now this might raise your eyebrow, because it was mentioned earlier that server addresses couldn't be completely wild. The important word to notice is "completely." The port and IP number cannot both be wild. The port number is not wild in this example, but it is permitted for the IP number to be wild in the server address. This allows the server to accept connections on any IP interface. This becomes important for systems that have several network cards.


TIP
A server address cannot completely wild. However, with a specified port number, the IP number portion of a TCP/IP address can be wild (INADDR_ANY). This allows your server to accept connections from any valid IP interface on that host.

Step 8 is the point where the server program calls the function accept(2). This is where the server control "blocks." The server will not execute any further instructions unless a client connects. After a connection has taken place, the accept(2) call will return in line 122. Note that variable c holds the client socket that has been returned by the accept(2) function call. Client request processing continues in steps 9 to 11. Then the server repeats step 8 to await the next client connection. The following shows how to make and invoke the server program:



Output
[sgupta@rhel54x64 server]$ gcc -c -Wall -Wreturn-type server.c
[sgupta@rhel54x64 server]$ gcc server.o -o server
[sgupta@rhel54x64 server]$ ./server &T
[1] 1049
[sgupta@rhel54x64 server]$ telnet 127.0.0.1 9099
Trying 127.0.0.1 . . .
Connected to 127.0.0.1.
Escape character is '^]'.
Sunday Aug 22 21:18:24 2011
Connection closed by foreign host.
[sgupta@rhel54x64 server]$

The output shows the server being run in the background, without any command-line parameters.
The telnet command connects to it to test it out. Notice that port 9099 is specified on the telnet command line.
The returned output from the server is purposely different from the standard daytime server. The standard daytime server abbreviates the weekday name. Because the session output shows the full weekday name, you can be confident that you contacted your server instead of the standard server.

Output
[sgupta@rhel54x64 server]$ ./server &
Address already in use: bind(2)
[2] 1057
[2] Exit 1 ./server
[sgupta@rhel54x64 server]$

TIP
Sometimes you will get the ''Address already in use" error, even though the server is not running. The Linux kernel will make the server address unavailable for a short period after the server has terminated. This behavior is controlled by the socket option SO_LINGER,




No comments:

Post a Comment