MULTIPLE CLIENTS WORKING!
This commit is contained in:
parent
2aa5c2814f
commit
742b05de74
8
Makefile
8
Makefile
|
@ -43,21 +43,21 @@ $(BINDIR)/tests/centers: $(SRCDIR)/tests/centers.c
|
||||||
|
|
||||||
# ---- General recipes ------------------------------------------------------- #
|
# ---- General recipes ------------------------------------------------------- #
|
||||||
|
|
||||||
$(BINDIR)/%.o: $(SRCDIR)/%.c $(INCDIR)/%.h $(INCDIR)/base.h
|
$(BINDIR)/%.o: $(SRCDIR)/%.c $(INCDIR)/%.h | $(INCDIR)/base.h
|
||||||
@echo "Compiling $<"
|
@echo "Compiling $<"
|
||||||
@$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $<
|
@$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
# ---- Main recipe ----------------------------------------------------------- #
|
# ---- Main recipe ----------------------------------------------------------- #
|
||||||
$(BINDIR)/main.o: $(SRCDIR)/main.c $(INCDIR)/base.h
|
$(BINDIR)/main.o: $(SRCDIR)/main.c | $(INCDIR)/base.h
|
||||||
@echo "Compiling $<"
|
@echo "Compiling $<"
|
||||||
@$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $<
|
@$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
$(BINDIR)/gem-graph-server: $(SERVEROBJ) $(INCDIR)/base.h
|
$(BINDIR)/gem-graph-server: $(SERVEROBJ)
|
||||||
@echo "Building program to $@"
|
@echo "Building program to $@"
|
||||||
@$(CC) -o $@ $(SERVEROBJ) $(LDFLAGS)
|
@$(CC) -o $@ $(SERVEROBJ) $(LDFLAGS)
|
||||||
@echo "Success!"
|
@echo "Success!"
|
||||||
|
|
||||||
$(BINDIR)/gem-graph-ctl: $(CLIOBJ) $(INCDIR)/base.h
|
$(BINDIR)/gem-graph-ctl: $(CLIOBJ)
|
||||||
@echo "Building program to $@"
|
@echo "Building program to $@"
|
||||||
@$(CC) -o $@ $(CLIOBJ) $(LDFLAGS)
|
@$(CC) -o $@ $(CLIOBJ) $(LDFLAGS)
|
||||||
@echo "Success!"
|
@echo "Success!"
|
||||||
|
|
|
@ -222,7 +222,7 @@ int main()
|
||||||
struct sockaddr_in servaddr;
|
struct sockaddr_in servaddr;
|
||||||
|
|
||||||
// Socket creation
|
// Socket creation
|
||||||
sockfd = socket(PF_INET, SOCK_STREAM, 0);
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (sockfd == -1) {
|
if (sockfd == -1) {
|
||||||
printLog("Socket creation failed!\n");
|
printLog("Socket creation failed!\n");
|
||||||
return 1;
|
return 1;
|
||||||
|
|
70
src/server.c
70
src/server.c
|
@ -25,6 +25,7 @@
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
@ -46,9 +47,12 @@ 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;
|
int tokenIndex, bytesReceived;
|
||||||
|
struct sockaddr_in clientAddr;
|
||||||
|
socklen_t clientAddrSize = sizeof(clientAddr);
|
||||||
|
|
||||||
args = (ServerCommunication_t*) server;
|
args = (ServerCommunication_t*) server;
|
||||||
|
|
||||||
|
@ -60,14 +64,25 @@ 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
|
||||||
read(args->sockfd, receiveBuff, sizeof(receiveBuff));
|
bytesReceived = recvfrom(args->sockfd, receiveBuff, sizeof(receiveBuff),
|
||||||
|
0, (struct sockaddr *)&clientAddr,
|
||||||
|
&clientAddrSize);
|
||||||
|
if (bytesReceived == -1) {
|
||||||
|
printLog("Could not receive data!\n");
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
// Ignore null-sized request
|
// Ignore null-sized request
|
||||||
if (receiveBuff[0] == 0)
|
if (bytesReceived == 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// get IP addr from client
|
||||||
|
inet_ntop(AF_INET, &(clientAddr.sin_addr), clientIP, clientAddrSize);
|
||||||
|
|
||||||
// Print buffer which contains the client request
|
// Print buffer which contains the client request
|
||||||
printLog("Client request : '%s'\n", receiveBuff);
|
printLog("Client %s:%d request : '%s'\n", clientIP,
|
||||||
|
ntohs(clientAddr.sin_port),
|
||||||
|
receiveBuff);
|
||||||
|
|
||||||
// get args in an array
|
// get args in an array
|
||||||
tokenIndex = 0;
|
tokenIndex = 0;
|
||||||
|
@ -90,7 +105,8 @@ void *serverCommunicationInstance(void *server)
|
||||||
}
|
}
|
||||||
|
|
||||||
// and send that buffer to client
|
// and send that buffer to client
|
||||||
write(args->sockfd, sendBuff, sizeof(sendBuff));
|
sendto(args->sockfd, sendBuff, sizeof(sendBuff), 0,
|
||||||
|
(struct sockaddr *)&clientAddr, clientAddrSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
close(args->sockfd);
|
close(args->sockfd);
|
||||||
|
@ -103,14 +119,16 @@ void *serverCommunicationInstance(void *server)
|
||||||
// Server main function //
|
// Server main function //
|
||||||
// -------------------------------------------------------------------------- //
|
// -------------------------------------------------------------------------- //
|
||||||
#define PORT 9000
|
#define PORT 9000
|
||||||
|
#define MAX_CONNECTION 100
|
||||||
|
|
||||||
static void *serverMain(void *server)
|
static void *serverMain(void *server)
|
||||||
{
|
{
|
||||||
Server_t *args;
|
Server_t *args;
|
||||||
ServerCommunication_t serverSlots[50] = {0};
|
ServerCommunication_t serverSlots[MAX_CONNECTION] = {0};
|
||||||
int connfd, flags, serverSlotIndex = 0;
|
int connfd, flags, threadStatus, serverSlotIndex = 0;
|
||||||
uint len;
|
uint socklen;
|
||||||
struct sockaddr_in servaddr, cli;
|
struct sockaddr_in servaddr, clientAddr;
|
||||||
|
char clientIP[16];
|
||||||
|
|
||||||
// Get args
|
// Get args
|
||||||
args = (Server_t*) server;
|
args = (Server_t*) server;
|
||||||
|
@ -143,46 +161,60 @@ static void *serverMain(void *server)
|
||||||
servaddr.sin_port = htons(PORT);
|
servaddr.sin_port = htons(PORT);
|
||||||
|
|
||||||
// Binding newly created socket
|
// Binding newly created socket
|
||||||
if ((bind(args->sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) != 0) {
|
if ((bind(args->sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == -1) {
|
||||||
printLog("Socket bind failed!\n");
|
printLog("Socket bind failed!\n");
|
||||||
close(args->sockfd);
|
|
||||||
goto serverExiting;
|
goto serverExiting;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now server is ready to listen and verification
|
// Now server is ready to listen and verification
|
||||||
if ((listen(args->sockfd, 5)) != 0) {
|
if (listen(args->sockfd, MAX_CONNECTION) == -1) {
|
||||||
printLog("Socket listening failed!\n");
|
printLog("Socket listening failed!\n");
|
||||||
close(args->sockfd);
|
|
||||||
goto serverExiting;
|
goto serverExiting;
|
||||||
}
|
}
|
||||||
|
|
||||||
len = sizeof(cli);
|
socklen = sizeof(clientAddr);
|
||||||
|
|
||||||
|
if (getsockname(args->sockfd, (struct sockaddr *) &servaddr, &socklen)
|
||||||
|
== -1) {
|
||||||
|
printLog("Could not get sock name!\n");
|
||||||
|
goto serverExiting;
|
||||||
|
}
|
||||||
|
|
||||||
printLog("Server listening...\n");
|
printLog("Server listening...\n");
|
||||||
|
|
||||||
while (!args->pleaseStop) {
|
while (!args->pleaseStop) {
|
||||||
|
|
||||||
// Accept the data packet from client
|
// Accept the data packet from client
|
||||||
connfd = accept(args->sockfd, (struct sockaddr*)&cli, &len);
|
connfd = accept(args->sockfd, (struct sockaddr*)&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) {
|
||||||
printLog("Server acccept failed!\n");
|
printLog("Server acccept failed!\n");
|
||||||
goto serverExiting;
|
goto serverExiting;
|
||||||
}
|
}
|
||||||
|
sleep(1);
|
||||||
} else {
|
} else {
|
||||||
// Client connected, creating a thread
|
// Client connected
|
||||||
printLog("Client accepted\n");
|
// 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 ?
|
||||||
|
|
||||||
|
// Populate communicator slot
|
||||||
serverSlots[serverSlotIndex].sockfd = connfd;
|
serverSlots[serverSlotIndex].sockfd = connfd;
|
||||||
serverSlots[serverSlotIndex].parent = args;
|
serverSlots[serverSlotIndex].parent = args;
|
||||||
|
|
||||||
pthread_create(&serverSlots[serverSlotIndex].id,
|
// Create thread
|
||||||
|
threadStatus = pthread_create(&serverSlots[serverSlotIndex].id,
|
||||||
NULL,
|
NULL,
|
||||||
serverCommunicationInstance,
|
serverCommunicationInstance,
|
||||||
(void*)&serverSlots[serverSlotIndex]);
|
(void*)&serverSlots[serverSlotIndex]);
|
||||||
|
if(threadStatus != 0) {
|
||||||
|
printLog("Error from pthread: %d\n", threadStatus);
|
||||||
|
goto serverExiting;
|
||||||
|
}
|
||||||
|
|
||||||
printLog("Server listening...\n");
|
serverSlotIndex++;
|
||||||
|
printLog("Accepted connection. Server will now listen...\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue