patterncMinor
Stupidly simple TCP client/server
Viewed 0 times
simpletcpclientserverstupidly
Problem
I'm trying to validate some results that I see when using NetPipe to test some connectivity between a couple of Linux boxes (over various hardware). So, I concocted this simple client and server to do the same and I cannot seem to get the same numbers as NetPipe - I'm about 30-40% off the rtt times that it sees.
Is there something stupid that I'm doing wrong with my simple example?
Server:
```
#include / for printf() and fprintf() /
#include / for socket(), bind(), and connect() /
#include / for sockaddr_in and inet_ntoa() /
#include
#include / for atoi() and exit() /
#include / for memset() /
#include / for close() /
#include / for perror() /
#include / for exit() /
#define MAXPENDING 1
void die(char *errorMessage)
{
perror(errorMessage);
exit(1);
}
void handle(unsigned short quickAck, int clntSock)
{
long long c_ts; / current read timestamp /
int value = 1;
// Enable quickAck
if (quickAck && setsockopt(clntSock, IPPROTO_TCP, TCP_QUICKACK, (char *)&value, sizeof(int)) \n", argv[0]);
exit(1);
}
echoServPort = atoi(argv[1]); / First arg: local port /
quickAck = atoi(argv[2]); / Whether quick ack is enabled or not /
/ Create socket for incoming connections /
if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
die("socket() failed");
/ Construct local address structure /
memset(&echoServAddr, 0, sizeof(echoServAddr)); / Zero out structure /
echoServAddr.sin_family = AF_INET; / Internet address family /
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); / Any incoming interface /
echoServAddr.sin_port = htons(echoServPort); / Local port /
/ Bind to the local address /
if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
die("bind() failed");
/ Mark the socket so it will listen for incoming connections /
if (listen(servSock, MAXPENDING) < 0)
die("liste
Is there something stupid that I'm doing wrong with my simple example?
Server:
```
#include / for printf() and fprintf() /
#include / for socket(), bind(), and connect() /
#include / for sockaddr_in and inet_ntoa() /
#include
#include / for atoi() and exit() /
#include / for memset() /
#include / for close() /
#include / for perror() /
#include / for exit() /
#define MAXPENDING 1
void die(char *errorMessage)
{
perror(errorMessage);
exit(1);
}
void handle(unsigned short quickAck, int clntSock)
{
long long c_ts; / current read timestamp /
int value = 1;
// Enable quickAck
if (quickAck && setsockopt(clntSock, IPPROTO_TCP, TCP_QUICKACK, (char *)&value, sizeof(int)) \n", argv[0]);
exit(1);
}
echoServPort = atoi(argv[1]); / First arg: local port /
quickAck = atoi(argv[2]); / Whether quick ack is enabled or not /
/ Create socket for incoming connections /
if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
die("socket() failed");
/ Construct local address structure /
memset(&echoServAddr, 0, sizeof(echoServAddr)); / Zero out structure /
echoServAddr.sin_family = AF_INET; / Internet address family /
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); / Any incoming interface /
echoServAddr.sin_port = htons(echoServPort); / Local port /
/ Bind to the local address /
if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
die("bind() failed");
/ Mark the socket so it will listen for incoming connections /
if (listen(servSock, MAXPENDING) < 0)
die("liste
Solution
Not much to comment on, this program is pretty straightforward. A few notes:
-
The user of the client has a lot of information to input.
The more the user has to enter, the steeper the initial learning curve to use the program is. Also, I'm not sure I want the user to control the `
-
The user of the client has a lot of information to input.
fprintf(stderr, "Usage: %s \n", argv[0]);The more the user has to enter, the steeper the initial learning curve to use the program is. Also, I'm not sure I want the user to control the `
and the anyways. A malicious user might abuse this for a DOS of the server. Eliminating those as required input and setting them in your code would be a more secure and a more user-friendly option.
-
You have too many comments.
close(clntSock); /* Close client socket */
For that particular example, it is quite obvious what that statement does. There are a lot of other comparable examples in this code.
-
It is more common to return 0; when main() is finished, rather than to exit(0). Both will call the registered atexit` handlers and will cause program termination though.Code Snippets
fprintf(stderr, "Usage: %s <Server IP> <Server Port> <Iterations> <Gap>\n", argv[0]);close(clntSock); /* Close client socket */Context
StackExchange Code Review Q#13933, answer score: 8
Revisions (0)
No revisions yet.