I spent some time on TCP tuning on linux, see link TCP performance tuning, then I thought that people may not know how to verify how much window size their application is picking. So, I wanted to write a C program to verify the configuration. Then, googlling tells me that there are lots of people have done it already. Here is a one I think it looks easy and good. http://en.wikibooks.org/wiki/C_Programming/Networking_in_UNIX In case it disappears, I past the whole article to the bottom of this page, thanks to wikibooks.org One thing need to mention is that the default window size is from net.core.rmem_default, which defined for all protocols. Default is 87380 bytes. But for TCP, it is overwritten by the parameter tcp_rmem. Here is the full explain of the parameter from TCP man page. tcp_rmem (since Linux 2.4) This is a vector of 3 integers: [min, default, max]. These parameters are used by TCP to regulate receive buffer sizes. TCP dynamically adjusts the size of the receive buffer from the defaults listed below, in the range of these values, depending on memory available in the system. min minimum size of the receive buffer used by each TCP socket. The default value is the system page size. (On Linux 2.4, the default value is 4K, lowered to PAGE_SIZE bytes in low-memory systems.) This value is used to ensure that in memory pressure mode, allocations below this size will still succeed. This is not used to bound the size of the receive buffer declared using SO_RCVBUF on a socket. default the default size of the receive buffer for a TCP socket. This value overwrites the initial default buffer size from the generic global net.core.rmem_default defined for all protocols. The default value is 87380 bytes. (On Linux 2.4, this will be lowered to 43689 in low-memory systems.) If larger receive buffer sizes are desired, this value should be increased (to affect all sockets). To employ large TCP windows, the net.ipv4.tcp_window_scaling must be enabled (default). max the maximum size of the receive buffer used by each TCP socket. This value does not override the global net.core.rmem_max. This is not used to limit the size of the receive buffer declared using SO_RCVBUF on a socket. The default value is calculated using the formula max(87380, min(4MB, tcp_mem[1]*PAGE_SIZE/128)) (On Linux 2.4, the default is 87380*2 bytes, lowered to 87380 in low-memory systems). A simple clientTo start with, we'll look at one of the simplest things you can do: initialize a stream connection and receive a message from a remote server.
This is the very bare bones of a client; in practice, we would check every function that we call for failure, however, error checking has been left out for clarity. As you can see, the code mainly revolves around mysocket = socket(AF_INET, SOCK_STREAM, 0); The
Now we get on to the interesting part: The first line uses The second line sets the address family. This should be the same value that was passed as the first parameter to The third line is where we set the IP of the machine we need to connect to. The variable The fourth line sets the destination port number. The Now that all of the preliminary work is done, we can actually make the connection and use it: connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr)); This tells our OS to use the socket len = recv(mysocket, buffer, MAXRCVLEN, 0); Now this receives up to And that's about it! The next step after learning how to receive data is learning how to
send it. If you've understood the previous section then this is quite
easy. All you have to do is use the A simple server
Superficially, this is very similar to the client. The first important difference is that rather than creating a The Once we have "session" socket we can handle it in the same way as with the client, using Note that this server can only accept one connection at a time; if
you want to simultaneously handle multiple clients then you'll need to Useful network functionsint gethostname(char *hostname, size_t size); The parameters are a pointer to an array of chars and the size of that array. If possible, it finds the hostname and stores it in the array. On failure it returns -1. struct hostent *gethostbyname(const char *name); This function obtains information about a domain name and stores it in a FAQsWhat about stateless connections?If you don't want to exploit the properties of TCP in your program
and would rather just use a UDP connection, then you can just replace If you want to exploit the properties of UDP, then you can use How do I check for errors?The functions |
Scripting > C examples >