Fix coding style in client code
This commit is contained in:
parent
227a9f7b47
commit
0d4b9c21b6
@ -1,3 +1,2 @@
|
|||||||
all:
|
all:
|
||||||
gcc -g -Wall -o client client.c
|
gcc -g -Wall -o client client.c
|
||||||
|
|
||||||
|
356
client/client.c
356
client/client.c
@ -15,208 +15,208 @@
|
|||||||
int
|
int
|
||||||
main(void)
|
main(void)
|
||||||
{
|
{
|
||||||
int sockfd;
|
int sockfd;
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
struct addrinfo *servinfo;
|
struct addrinfo *servinfo;
|
||||||
struct addrinfo *p;
|
struct addrinfo *p;
|
||||||
int rv;
|
int rv;
|
||||||
int connected;
|
int connected;
|
||||||
|
|
||||||
// Set the hints
|
// Set the hints
|
||||||
memset(&hints, 0, sizeof hints);
|
memset(&hints, 0, sizeof hints);
|
||||||
hints.ai_family = AF_UNSPEC;
|
hints.ai_family = AF_UNSPEC;
|
||||||
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.
|
||||||
connected = 0;
|
connected = 0;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If p is NULL, we didn't succeed to create a socket. This should never happen
|
break;
|
||||||
if (p == NULL)
|
}
|
||||||
{
|
|
||||||
fprintf(stderr, "client: failed to connect\n");
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the socket to non-blocking so we can control the timeout value
|
// If p is NULL, we didn't succeed to create a socket. This
|
||||||
fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
// should never happen
|
||||||
|
if (p == NULL) {
|
||||||
|
fprintf(stderr, "client: failed to connect\n");
|
||||||
|
|
||||||
// Start the connection (it usually won't succeed yet)
|
return 2;
|
||||||
con = connect(sockfd, p->ai_addr, p->ai_addrlen);
|
}
|
||||||
|
|
||||||
// If connect() returned an error saying the connection is in progress
|
// Set the socket to non-blocking so we can control the timeout value
|
||||||
if ((con < 0) && (errno == EINPROGRESS))
|
fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
||||||
{
|
|
||||||
// Cycle INNER1
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
struct timeval tv;
|
|
||||||
int t;
|
|
||||||
fd_set master;
|
|
||||||
|
|
||||||
// Create an empty descriptor set, and add our socket to it
|
// Start the connection (it usually won't succeed yet)
|
||||||
FD_ZERO(&master);
|
con = connect(sockfd, p->ai_addr, p->ai_addrlen);
|
||||||
FD_SET(sockfd, &master);
|
|
||||||
|
|
||||||
// Set the timeout value to SENDING_FREQ seconds
|
// If connect() returned an error saying the connection is in progress
|
||||||
tv.tv_sec = SENDING_FREQ;
|
if ((con < 0) && (errno == EINPROGRESS)) {
|
||||||
tv.tv_usec = 0;
|
// Cycle INNER1
|
||||||
|
while (1) {
|
||||||
// Run the select()
|
struct timeval tv;
|
||||||
t = select(sockfd + 1, NULL, &master, NULL, &tv);
|
int t;
|
||||||
|
fd_set master;
|
||||||
|
|
||||||
if ((t < 0) && (errno != EINTR))
|
// Create an empty descriptor set, and add our socket to it
|
||||||
{
|
FD_ZERO(&master);
|
||||||
// Some serious error happened, let's exit
|
FD_SET(sockfd, &master);
|
||||||
perror("select");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
else if (t < 0)
|
|
||||||
{
|
|
||||||
// select() was interrupted, lets restart the INNER1 cycle
|
|
||||||
printf("select() interrupted, continue\n");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else if (t > 0)
|
|
||||||
{
|
|
||||||
size_t lon;
|
|
||||||
int valopt;
|
|
||||||
|
|
||||||
lon = sizeof(int);
|
// Set the timeout value to SENDING_FREQ seconds
|
||||||
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0)
|
tv.tv_sec = SENDING_FREQ;
|
||||||
{
|
tv.tv_usec = 0;
|
||||||
fprintf(stderr, "Error in getsockopt() %d - %s\n", errno, strerror(errno));
|
|
||||||
connected = 0;
|
|
||||||
sleep(SENDING_FREQ);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// Check the value returned...
|
|
||||||
if (valopt)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error in delayed connection() %d - %s\n", valopt, strerror(valopt));
|
|
||||||
connected = 0;
|
|
||||||
sleep(SENDING_FREQ);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
printf("Connected with select().\n");
|
|
||||||
connected = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (t == 0)
|
|
||||||
{
|
|
||||||
// Connection timed out, let's break out from INNER1
|
|
||||||
printf("connect() timed out.\n");
|
|
||||||
connected = 0;
|
|
||||||
close(sockfd);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (con < 0)
|
|
||||||
{
|
|
||||||
// connect() error. Wait SENDING_FREQ seconds and try again (restarting the OUTER cycle)
|
|
||||||
// XXX error handling?
|
|
||||||
printf("connect() error, retry\n");
|
|
||||||
sleep(SENDING_FREQ);
|
|
||||||
connected = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else if (con == 0)
|
|
||||||
{
|
|
||||||
// We are now connected. Usually we won't get here, but in the select() loop above instead
|
|
||||||
printf("Connected without select().\n");
|
|
||||||
connected = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (connected)
|
// Run the select()
|
||||||
{
|
t = select(sockfd + 1, NULL, &master, NULL, &tv);
|
||||||
// If we are connected, let's jump in the INNER2 cycle
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
fd_set read_fds;
|
|
||||||
int t;
|
|
||||||
struct timeval tv;
|
|
||||||
char buf[MAXDATASIZE];
|
|
||||||
|
|
||||||
// Create and empty descriptor set and add our socket
|
if ((t < 0) && (errno != EINTR)) {
|
||||||
FD_ZERO(&read_fds);
|
// Some serious error happened, let's exit
|
||||||
FD_SET(sockfd, &read_fds);
|
perror("select");
|
||||||
|
|
||||||
// Set the timeout value to SENDING_FREQ seconds
|
exit(1);
|
||||||
tv.tv_sec = SENDING_FREQ;
|
} else if (t < 0) {
|
||||||
tv.tv_usec = 0;
|
// select() was interrupted, lets restart the INNER1 cycle
|
||||||
|
printf("select() interrupted, continue\n");
|
||||||
// Let's run the select()
|
|
||||||
t = select(sockfd + 1, &read_fds, NULL, NULL, &tv);
|
|
||||||
|
|
||||||
if ((t < 0) && (errno != EINTR))
|
continue;
|
||||||
{
|
} else if (t > 0) {
|
||||||
// select() ran into an error, this is bad. Let's exit
|
size_t lon;
|
||||||
perror("select");
|
int valopt;
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
else if (t < 0)
|
|
||||||
{
|
|
||||||
// select() interrupted, try again by restarting the INNER2 cycle
|
|
||||||
printf("select() interrupted\n");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else if (t > 0)
|
|
||||||
{
|
|
||||||
// We got some data from the server
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
printf("Data from server.\n");
|
lon = sizeof(int);
|
||||||
|
if (getsockopt(sockfd,
|
||||||
|
SOL_SOCKET, SO_ERROR,
|
||||||
|
(void*)(&valopt), &lon) < 0) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"Error in getsockopt() %d - %s\n",
|
||||||
|
errno, strerror(errno));
|
||||||
|
connected = 0;
|
||||||
|
sleep(SENDING_FREQ);
|
||||||
|
|
||||||
len = recv(sockfd, &buf, MAXDATASIZE, 0);
|
break;
|
||||||
|
}
|
||||||
|
// Check the value returned...
|
||||||
|
if (valopt) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"Error in delayed connection() %d - %s\n",
|
||||||
|
valopt, strerror(valopt));
|
||||||
|
connected = 0;
|
||||||
|
sleep(SENDING_FREQ);
|
||||||
|
|
||||||
if (len <= 0)
|
break;
|
||||||
{
|
}
|
||||||
// If the received data length is at most 0, we are disconnected, so break out from INNER2
|
|
||||||
printf("Closing connection.\n");
|
|
||||||
connected = 0;
|
|
||||||
close(sockfd);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we arrive here, select() ran into timeout, so we should send some data to the server.
|
printf("Connected with select().\n");
|
||||||
printf("Sending data to server\n");
|
connected = 1;
|
||||||
send(sockfd, "!\n", 2, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This should never happen, but if so, let's clean up after ourselved
|
break;
|
||||||
freeaddrinfo(servinfo);
|
} else if (t == 0) {
|
||||||
|
// Connection timed out, let's break out from INNER1
|
||||||
|
printf("connect() timed out.\n");
|
||||||
|
connected = 0;
|
||||||
|
close(sockfd);
|
||||||
|
|
||||||
close(sockfd);
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (con < 0) {
|
||||||
|
// connect() error. Wait SENDING_FREQ seconds and try
|
||||||
|
// again (restarting the OUTER cycle)
|
||||||
|
|
||||||
return 0;
|
// XXX error handling?
|
||||||
|
printf("connect() error, retry\n");
|
||||||
|
sleep(SENDING_FREQ);
|
||||||
|
connected = 0;
|
||||||
|
|
||||||
|
continue;
|
||||||
|
} else if (con == 0) {
|
||||||
|
// We are now connected. Usually we won't get here, but in
|
||||||
|
// the select() loop above instead
|
||||||
|
printf("Connected without select().\n");
|
||||||
|
connected = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connected) {
|
||||||
|
// If we are connected, let's jump in the INNER2 cycle
|
||||||
|
while (1) {
|
||||||
|
fd_set read_fds;
|
||||||
|
int t;
|
||||||
|
struct timeval tv;
|
||||||
|
char buf[MAXDATASIZE];
|
||||||
|
|
||||||
|
// Create and empty descriptor set and add our socket
|
||||||
|
FD_ZERO(&read_fds);
|
||||||
|
FD_SET(sockfd, &read_fds);
|
||||||
|
|
||||||
|
// Set the timeout value to SENDING_FREQ seconds
|
||||||
|
tv.tv_sec = SENDING_FREQ;
|
||||||
|
tv.tv_usec = 0;
|
||||||
|
|
||||||
|
// Let's run the select()
|
||||||
|
t = select(sockfd + 1, &read_fds, NULL, NULL, &tv);
|
||||||
|
|
||||||
|
if ((t < 0) && (errno != EINTR)) {
|
||||||
|
// select() ran into an error, this is bad. Let's exit
|
||||||
|
perror("select");
|
||||||
|
|
||||||
|
exit(1);
|
||||||
|
} else if (t < 0) {
|
||||||
|
// select() interrupted, try again by restarting
|
||||||
|
// the INNER2 cycle
|
||||||
|
printf("select() interrupted\n");
|
||||||
|
|
||||||
|
continue;
|
||||||
|
} else if (t > 0) {
|
||||||
|
// We got some data from the server
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
printf("Data from server.\n");
|
||||||
|
|
||||||
|
len = recv(sockfd, &buf, MAXDATASIZE, 0);
|
||||||
|
|
||||||
|
if (len <= 0) {
|
||||||
|
// If the received data length is at most 0,
|
||||||
|
// we are disconnected, so break out from
|
||||||
|
// INNER2
|
||||||
|
printf("Closing connection.\n");
|
||||||
|
connected = 0;
|
||||||
|
close(sockfd);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we arrive here, select() ran into timeout, so we
|
||||||
|
// should send some data to the server.
|
||||||
|
printf("Sending data to server\n");
|
||||||
|
send(sockfd, "!\n", 2, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This should never happen, but if so, let's clean up after ourselved
|
||||||
|
freeaddrinfo(servinfo);
|
||||||
|
|
||||||
|
close(sockfd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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"
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user