Multiclient with IP:port detection
This commit is contained in:
parent
742b05de74
commit
5ec6e86f6e
|
@ -28,6 +28,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#define BASE_H
|
||||
|
||||
|
@ -130,6 +131,8 @@ struct {
|
|||
bool pleaseStop;
|
||||
Server_t *parent;
|
||||
int sockfd;
|
||||
struct sockaddr_in clientAddr;
|
||||
socklen_t socklen;
|
||||
} typedef ServerCommunication_t;
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
|
|
@ -209,7 +209,7 @@ void connectedCommunication(int sockfd)
|
|||
|
||||
// Detect null-string returned
|
||||
if (receiveBuff[0] == 0) {
|
||||
printLog("An error occured or invalid command!\n");
|
||||
printf("An error occured or invalid command!\n");
|
||||
}
|
||||
|
||||
printf("%s\n", receiveBuff);
|
||||
|
|
47
src/server.c
47
src/server.c
|
@ -23,7 +23,6 @@
|
|||
#include "../include/cmds.h"
|
||||
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
|
@ -47,12 +46,10 @@ void *serverCommunicationInstance(void *server)
|
|||
{
|
||||
ServerCommunication_t *args;
|
||||
char **argv = NULL;
|
||||
char clientIP[16];
|
||||
char receiveBuff[RECEIVE_BUFFER_SIZE];
|
||||
char sendBuff[SEND_BUFFER_SIZE];
|
||||
int tokenIndex, bytesReceived;
|
||||
struct sockaddr_in clientAddr;
|
||||
socklen_t clientAddrSize = sizeof(clientAddr);
|
||||
char clientIP[16];
|
||||
|
||||
args = (ServerCommunication_t*) server;
|
||||
|
||||
|
@ -64,9 +61,7 @@ void *serverCommunicationInstance(void *server)
|
|||
printLog("Waiting for commands...\n");
|
||||
|
||||
// Read the message from client and copy it in buffer
|
||||
bytesReceived = recvfrom(args->sockfd, receiveBuff, sizeof(receiveBuff),
|
||||
0, (struct sockaddr *)&clientAddr,
|
||||
&clientAddrSize);
|
||||
bytesReceived = recv(args->sockfd, receiveBuff, RECEIVE_BUFFER_SIZE, 0);
|
||||
if (bytesReceived == -1) {
|
||||
printLog("Could not receive data!\n");
|
||||
break;
|
||||
|
@ -76,12 +71,15 @@ void *serverCommunicationInstance(void *server)
|
|||
if (bytesReceived == 0)
|
||||
break;
|
||||
|
||||
// get IP addr from client
|
||||
inet_ntop(AF_INET, &(clientAddr.sin_addr), clientIP, clientAddrSize);
|
||||
inet_ntop(AF_INET,
|
||||
&args->clientAddr.sin_addr,
|
||||
clientIP,
|
||||
args->socklen);
|
||||
|
||||
// Print buffer which contains the client request
|
||||
printLog("Client %s:%d request : '%s'\n", clientIP,
|
||||
ntohs(clientAddr.sin_port),
|
||||
printLog("Client %s:%d request : '%s'\n",
|
||||
clientIP,
|
||||
ntohs(args->clientAddr.sin_port),
|
||||
receiveBuff);
|
||||
|
||||
// get args in an array
|
||||
|
@ -105,13 +103,13 @@ void *serverCommunicationInstance(void *server)
|
|||
}
|
||||
|
||||
// and send that buffer to client
|
||||
sendto(args->sockfd, sendBuff, sizeof(sendBuff), 0,
|
||||
(struct sockaddr *)&clientAddr, clientAddrSize);
|
||||
send(args->sockfd, sendBuff, sizeof(sendBuff), 0);
|
||||
}
|
||||
|
||||
close(args->sockfd);
|
||||
printLog("End of communications \n");
|
||||
if (argv)
|
||||
free(argv);
|
||||
printLog("End of communications \n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -127,7 +125,7 @@ static void *serverMain(void *server)
|
|||
ServerCommunication_t serverSlots[MAX_CONNECTION] = {0};
|
||||
int connfd, flags, threadStatus, serverSlotIndex = 0;
|
||||
uint socklen;
|
||||
struct sockaddr_in servaddr, clientAddr;
|
||||
struct sockaddr_in servaddr;
|
||||
char clientIP[16];
|
||||
|
||||
// Get args
|
||||
|
@ -141,12 +139,14 @@ static void *serverMain(void *server)
|
|||
goto serverExiting;
|
||||
}
|
||||
|
||||
// Get socket flags
|
||||
flags = fcntl(args->sockfd, F_GETFL);
|
||||
if (flags == -1) {
|
||||
printLog("Socket parameters getting failed!\n");
|
||||
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");
|
||||
goto serverExiting;
|
||||
|
@ -172,7 +172,7 @@ static void *serverMain(void *server)
|
|||
goto serverExiting;
|
||||
}
|
||||
|
||||
socklen = sizeof(clientAddr);
|
||||
socklen = sizeof(struct sockaddr_in);
|
||||
|
||||
if (getsockname(args->sockfd, (struct sockaddr *) &servaddr, &socklen)
|
||||
== -1) {
|
||||
|
@ -185,10 +185,12 @@ static void *serverMain(void *server)
|
|||
while (!args->pleaseStop) {
|
||||
|
||||
// Accept the data packet from client
|
||||
connfd = accept(args->sockfd, (struct sockaddr*)&clientAddr, &socklen);
|
||||
connfd = accept(args->sockfd,
|
||||
(struct sockaddr*) &serverSlots[serverSlotIndex].clientAddr,
|
||||
&socklen);
|
||||
if (connfd < 0) {
|
||||
// If error is not due to lack of clients connecting, this is error
|
||||
if (errno != EWOULDBLOCK) {
|
||||
if (errno != EWOULDBLOCK && errno != EAGAIN) {
|
||||
printLog("Server acccept failed!\n");
|
||||
goto serverExiting;
|
||||
}
|
||||
|
@ -196,10 +198,15 @@ static void *serverMain(void *server)
|
|||
} else {
|
||||
// Client connected
|
||||
// get IP addr from client
|
||||
inet_ntop(AF_INET, &(clientAddr.sin_addr), clientIP, socklen);
|
||||
printLog("Client accepted from %s\n", clientIP); //TODO pass that thing to communicator ?
|
||||
inet_ntop(AF_INET,
|
||||
&(serverSlots[serverSlotIndex].clientAddr.sin_addr),
|
||||
clientIP, socklen);
|
||||
printLog("Client accepted from %s:%d\n",
|
||||
clientIP,
|
||||
ntohs(serverSlots[serverSlotIndex].clientAddr.sin_port));
|
||||
|
||||
// Populate communicator slot
|
||||
serverSlots[serverSlotIndex].socklen = socklen;
|
||||
serverSlots[serverSlotIndex].sockfd = connfd;
|
||||
serverSlots[serverSlotIndex].parent = args;
|
||||
|
||||
|
|
Loading…
Reference in New Issue