Fix coding style in client code

This commit is contained in:
Gergely Polonkai 2016-05-31 14:57:21 +02:00
parent 227a9f7b47
commit 0d4b9c21b6
3 changed files with 178 additions and 180 deletions

View File

@ -1,3 +1,2 @@
all: all:
gcc -g -Wall -o client client.c gcc -g -Wall -o client client.c

View File

@ -28,15 +28,13 @@ main(void)
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
// Get address info (AKA resolv hostname) // Get address info (AKA resolv hostname)
if ((rv = getaddrinfo(SERVER_ADDRESS, PORT, &hints, &servinfo)) != 0) if ((rv = getaddrinfo(SERVER_ADDRESS, PORT, &hints, &servinfo)) != 0) {
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1; return 1;
} }
// Cycle OUTER // Cycle OUTER
while (1) while (1) {
{
int con; int con;
// Here we assume that we are not connected. // Here we assume that we are not connected.
@ -45,21 +43,23 @@ main(void)
printf("Connecting...\n"); printf("Connecting...\n");
// Create a socket // Create a socket
for (p = servinfo; p != NULL; p = p->ai_next) for (p = servinfo; p != NULL; p = p->ai_next) {
{ if ((sockfd = socket(p->ai_family,
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) p->ai_socktype,
{ p->ai_protocol)) == -1) {
perror("client: socket"); perror("client: socket");
continue; continue;
} }
break; break;
} }
// If p is NULL, we didn't succeed to create a socket. This should never happen // If p is NULL, we didn't succeed to create a socket. This
if (p == NULL) // should never happen
{ if (p == NULL) {
fprintf(stderr, "client: failed to connect\n"); fprintf(stderr, "client: failed to connect\n");
return 2; return 2;
} }
@ -70,11 +70,9 @@ main(void)
con = connect(sockfd, p->ai_addr, p->ai_addrlen); con = connect(sockfd, p->ai_addr, p->ai_addrlen);
// If connect() returned an error saying the connection is in progress // If connect() returned an error saying the connection is in progress
if ((con < 0) && (errno == EINPROGRESS)) if ((con < 0) && (errno == EINPROGRESS)) {
{
// Cycle INNER1 // Cycle INNER1
while (1) while (1) {
{
struct timeval tv; struct timeval tv;
int t; int t;
fd_set master; fd_set master;
@ -90,74 +88,76 @@ main(void)
// Run the select() // Run the select()
t = select(sockfd + 1, NULL, &master, NULL, &tv); t = select(sockfd + 1, NULL, &master, NULL, &tv);
if ((t < 0) && (errno != EINTR)) if ((t < 0) && (errno != EINTR)) {
{
// Some serious error happened, let's exit // Some serious error happened, let's exit
perror("select"); perror("select");
exit(1); exit(1);
} } else if (t < 0) {
else if (t < 0)
{
// select() was interrupted, lets restart the INNER1 cycle // select() was interrupted, lets restart the INNER1 cycle
printf("select() interrupted, continue\n"); printf("select() interrupted, continue\n");
continue; continue;
} } else if (t > 0) {
else if (t > 0)
{
size_t lon; size_t lon;
int valopt; int valopt;
lon = sizeof(int); lon = sizeof(int);
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0) if (getsockopt(sockfd,
{ SOL_SOCKET, SO_ERROR,
fprintf(stderr, "Error in getsockopt() %d - %s\n", errno, strerror(errno)); (void*)(&valopt), &lon) < 0) {
fprintf(stderr,
"Error in getsockopt() %d - %s\n",
errno, strerror(errno));
connected = 0; connected = 0;
sleep(SENDING_FREQ); sleep(SENDING_FREQ);
break; break;
} }
// Check the value returned... // Check the value returned...
if (valopt) if (valopt) {
{ fprintf(stderr,
fprintf(stderr, "Error in delayed connection() %d - %s\n", valopt, strerror(valopt)); "Error in delayed connection() %d - %s\n",
valopt, strerror(valopt));
connected = 0; connected = 0;
sleep(SENDING_FREQ); sleep(SENDING_FREQ);
break; break;
} }
printf("Connected with select().\n"); printf("Connected with select().\n");
connected = 1; connected = 1;
break; break;
} } else if (t == 0) {
else if (t == 0)
{
// Connection timed out, let's break out from INNER1 // Connection timed out, let's break out from INNER1
printf("connect() timed out.\n"); printf("connect() timed out.\n");
connected = 0; connected = 0;
close(sockfd); close(sockfd);
break; break;
} }
} }
} } else if (con < 0) {
else if (con < 0) // connect() error. Wait SENDING_FREQ seconds and try
{ // again (restarting the OUTER cycle)
// connect() error. Wait SENDING_FREQ seconds and try again (restarting the OUTER cycle)
// XXX error handling? // XXX error handling?
printf("connect() error, retry\n"); printf("connect() error, retry\n");
sleep(SENDING_FREQ); sleep(SENDING_FREQ);
connected = 0; connected = 0;
continue; continue;
} } else if (con == 0) {
else if (con == 0) // We are now connected. Usually we won't get here, but in
{ // the select() loop above instead
// We are now connected. Usually we won't get here, but in the select() loop above instead
printf("Connected without select().\n"); printf("Connected without select().\n");
connected = 1; connected = 1;
} }
if (connected) if (connected) {
{
// If we are connected, let's jump in the INNER2 cycle // If we are connected, let's jump in the INNER2 cycle
while (1) while (1) {
{
fd_set read_fds; fd_set read_fds;
int t; int t;
struct timeval tv; struct timeval tv;
@ -174,20 +174,18 @@ main(void)
// Let's run the select() // Let's run the select()
t = select(sockfd + 1, &read_fds, NULL, NULL, &tv); t = select(sockfd + 1, &read_fds, NULL, NULL, &tv);
if ((t < 0) && (errno != EINTR)) if ((t < 0) && (errno != EINTR)) {
{
// select() ran into an error, this is bad. Let's exit // select() ran into an error, this is bad. Let's exit
perror("select"); perror("select");
exit(1); exit(1);
} } else if (t < 0) {
else if (t < 0) // select() interrupted, try again by restarting
{ // the INNER2 cycle
// select() interrupted, try again by restarting the INNER2 cycle
printf("select() interrupted\n"); printf("select() interrupted\n");
continue; continue;
} } else if (t > 0) {
else if (t > 0)
{
// We got some data from the server // We got some data from the server
size_t len; size_t len;
@ -195,17 +193,20 @@ main(void)
len = recv(sockfd, &buf, MAXDATASIZE, 0); len = recv(sockfd, &buf, MAXDATASIZE, 0);
if (len <= 0) if (len <= 0) {
{ // If the received data length is at most 0,
// If the received data length is at most 0, we are disconnected, so break out from INNER2 // we are disconnected, so break out from
// INNER2
printf("Closing connection.\n"); printf("Closing connection.\n");
connected = 0; connected = 0;
close(sockfd); close(sockfd);
break; break;
} }
} }
// If we arrive here, select() ran into timeout, so we should send some data to the server. // If we arrive here, select() ran into timeout, so we
// should send some data to the server.
printf("Sending data to server\n"); printf("Sending data to server\n");
send(sockfd, "!\n", 2, 0); send(sockfd, "!\n", 2, 0);
} }
@ -219,4 +220,3 @@ main(void)
return 0; return 0;
} }

View File

@ -2,4 +2,3 @@
#define MAXDATASIZE 128 #define MAXDATASIZE 128
#define SENDING_FREQ 10 #define SENDING_FREQ 10
#define SERVER_ADDRESS "127.0.0.1" #define SERVER_ADDRESS "127.0.0.1"