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