Errno is useful

This commit is contained in:
Adrien Bourmault 2021-07-12 15:12:29 +02:00
parent dd59e34110
commit 442c76e96c
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
1 changed files with 23 additions and 16 deletions

View File

@ -23,6 +23,7 @@
#include "../include/cmds.h"
#include <netdb.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
@ -48,7 +49,7 @@ void *serverCommunicationInstance(void *server)
char **argv = NULL;
char receiveBuff[RECEIVE_BUFFER_SIZE];
char sendBuff[SEND_BUFFER_SIZE];
int tokenIndex, bytesReceived;
int tokenIndex, bytesReceived, clientPort;
char clientIP[16];
args = (ServerCommunication_t*) server;
@ -58,12 +59,12 @@ void *serverCommunicationInstance(void *server)
// Zeroing buffer
bzero(receiveBuff, RECEIVE_BUFFER_SIZE);
printLog("Waiting for commands...\n");
//printLog("Waiting for commands...\n");
// Read the message from client and copy it in buffer
bytesReceived = recv(args->sockfd, receiveBuff, RECEIVE_BUFFER_SIZE, 0);
if (bytesReceived == -1) {
printLog("Could not receive data!\n");
printLog("Could not receive data! (%s)\n", strerror(errno));
break;
};
@ -77,10 +78,13 @@ void *serverCommunicationInstance(void *server)
clientIP,
args->socklen);
// Get port number from client
clientPort = ntohs(args->clientAddr.sin_port);
// Print buffer which contains the client request
printLog("Client %s:%d request : '%s'\n",
clientIP,
ntohs(args->clientAddr.sin_port),
clientPort,
receiveBuff);
// get args in an array
@ -110,7 +114,7 @@ void *serverCommunicationInstance(void *server)
close(args->sockfd);
if (argv)
free(argv);
printLog("Disconnected from\n");
printLog("Disconnected from %s:%d\n", clientIP, clientPort);
return NULL;
}
@ -137,7 +141,7 @@ static void *serverMain(void *server)
// Create socket
args->sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (args->sockfd == -1) {
printLog("Socket creation failed!\n");
printLog("Socket creation failed! (%s)\n", strerror(errno));
args->returnValue = 1;
goto serverExiting;
}
@ -145,14 +149,14 @@ static void *serverMain(void *server)
// Get socket flags
flags = fcntl(args->sockfd, F_GETFL);
if (flags == -1) {
printLog("Socket parameters getting failed!\n");
printLog("Socket parameters getting failed! (%s)\n", strerror(errno));
args->returnValue = 1;
goto serverExiting;
}
// Change socket flags to non-blocking
if (fcntl(args->sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
printLog("Socket non-blocking setting failed!\n");
printLog("Socket non-blocking setting failed! (%s)\n", strerror(errno));
args->returnValue = 1;
goto serverExiting;
}
@ -166,24 +170,26 @@ static void *serverMain(void *server)
servaddr.sin_port = htons(PORT);
// Binding newly created socket
if ((bind(args->sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == -1) {
printLog("Socket bind failed!\n");
if ((bind(args->sockfd, (struct sockaddr*)&servaddr,
sizeof(servaddr))) == -1) {
printLog("Socket bind failed! (%s)\n", strerror(errno));
args->returnValue = 1;
goto serverExiting;
}
// Now server is ready to listen and verification
if (listen(args->sockfd, MAX_CONNECTION) == -1) {
printLog("Socket listening failed!\n");
printLog("Socket listening failed! (%s)\n", strerror(errno));
args->returnValue = 1;
goto serverExiting;
}
socklen = sizeof(struct sockaddr_in);
// Get server socket address structure
if (getsockname(args->sockfd, (struct sockaddr *)&servaddr, &socklen)
== -1) {
printLog("Could not get sock name!\n");
printLog("Could not get socket structure! (%s)\n", strerror(errno));
args->returnValue = 1;
goto serverExiting;
}
@ -199,7 +205,7 @@ static void *serverMain(void *server)
if (connfd < 0) {
// If error is not due to lack of clients connecting, this is error
if (errno != EWOULDBLOCK && errno != EAGAIN) {
printLog("Server acccept failed!\n");
printLog("Server acccept failed! (%s)\n", strerror(errno));
goto serverExiting;
}
sleep(1);
@ -224,13 +230,14 @@ static void *serverMain(void *server)
serverCommunicationInstance,
(void*)&serverSlots[serverSlotIndex]);
if(threadStatus != 0) {
printLog("Error from pthread: %d\n", threadStatus);
printLog("Error from pthread: %d (%s)\n",
threadStatus, strerror(errno));
args->returnValue = 1;
goto serverExiting;
}
serverSlotIndex++;
printLog("Accepted connection. Server will now listen...\n");
//printLog("Accepted connection. Server will now listen...\n");
}
}