WIP: [BUG] new network code

This commit is contained in:
Adrien Bourmault 2021-07-08 00:10:36 +02:00
parent 636758324f
commit 4e9b368a72
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
2 changed files with 140 additions and 210 deletions

169
src/cli.c
View File

@ -23,135 +23,78 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#define BUFFERSIZE 80
#define SERVER_IP_ADDR "127.0.0.1" #define SERVER_IP_ADDR "127.0.0.1"
#define SERVER_PORT 90190 #define SERVER_PORT 9000
// -------------------------------------------------------------------------- // void connectedCommunication(int sockfd)
// Socket init function //
// -------------------------------------------------------------------------- //
static inline int createSocket(void)
{ {
int socketDescriptor; char buff[BUFFERSIZE];
//printLog("Create the socket\n");
socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
return socketDescriptor;
}
static inline int connectSocket(int newSocket)
{
int effectiveSocket = -1;
int serverPort = SERVER_PORT;
struct sockaddr_in remote = {0};
remote.sin_addr.s_addr = inet_addr(SERVER_IP_ADDR);
remote.sin_family = AF_INET;
remote.sin_port = htons(serverPort);
effectiveSocket =
connect(newSocket,
(struct sockaddr*)&remote,
sizeof(struct sockaddr_in)
);
return effectiveSocket;
}
static inline int sendSocket(int effectiveSocket, char *request,
short requestSize)
{
int returnSocket = -1;
struct timeval tv;
tv.tv_sec = 20; /* 20 Secs Timeout */
tv.tv_usec = 0;
if (setsockopt(effectiveSocket,
SOL_SOCKET,SO_SNDTIMEO,
(char *)&tv,sizeof(tv)) < 0) {
printLog("Time Out\n");
return -1;
}
returnSocket = send(effectiveSocket, request, requestSize, 0);
return returnSocket;
}
static inline int receiveSocket(int effectiveSocket, char *serverAnswer,
short receiveSize)
{
int returnSocket = -1;
struct timeval tv;
tv.tv_sec = 20; /* 20 Secs Timeout */
tv.tv_usec = 0;
if (setsockopt(effectiveSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
sizeof(tv)) < 0) {
printLog("Time Out\n");
return -1;
}
returnSocket = recv(effectiveSocket, serverAnswer, receiveSize, 0);
//printLog("Anwer %s\n", serverAnswer);
return returnSocket;
}
int main(int argc, char **argv)
{
int socketDescriptor;
char clientMessage[255] = {0};
char serverReply[255] = {0};
char *exitCommand = "exit"; char *exitCommand = "exit";
int n;
while(1) { for (;;) {
// Zeroing buffer
bzero(buff, sizeof(buff));
printf("\ngem-graph console> "); printf("\ngem-graph console> ");
fgets(clientMessage, 255, stdin); n = 0;
clientMessage[strcspn(clientMessage, "\n")] = '\0';
if (strcmp(exitCommand,clientMessage) == 0) // Read command from terminal
{ while ((buff[n++] = getchar()) != '\n')
close(socketDescriptor); ;
shutdown(socketDescriptor, 0);
shutdown(socketDescriptor, 1); // Quit if asked
shutdown(socketDescriptor, 2); if (strcmp(exitCommand, buff) == 0) {
return 0; return;
} }
//Create socket // Otherwise send command to server
socketDescriptor = createSocket(); write(sockfd, buff, sizeof(buff));
if(socketDescriptor == -1) { // Zeroing buffer
printLog("Could not create socket\n"); bzero(buff, sizeof(buff));
continue;
}
//Connect to remote server // Reading server answer
if (connectSocket(socketDescriptor) < 0) { read(sockfd, buff, sizeof(buff));
printLog("Connection failed\n");
continue;
}
//Send data to the server printf("%s\n", buff);
sendSocket(socketDescriptor, clientMessage, strlen(clientMessage)); }
}
//Received the data from the server int main()
receiveSocket(socketDescriptor, serverReply, 200); {
printf("%s\n", serverReply); int sockfd;
struct sockaddr_in servaddr;
memset(serverReply, 0, 255); // Socket creation
sockfd = socket(AF_INET, SOCK_STREAM, 0);
close(socketDescriptor); if (sockfd == -1) {
shutdown(socketDescriptor, 0); printLog("Socket creation failed!\n");
shutdown(socketDescriptor, 1); return 1;
shutdown(socketDescriptor, 2);
} }
return 0; bzero(&servaddr, sizeof(servaddr));
// Assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(SERVER_IP_ADDR);
servaddr.sin_port = htons(SERVER_PORT);
// Connect to the server
if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) {
printLog("Connection failed!\n");
return 2;
}
printLog("Connected!\n");
connectedCommunication(sockfd);
// close the socket
close(sockfd);
} }

View File

@ -22,8 +22,9 @@
#include "../include/base.h" #include "../include/base.h"
#include "../include/cmds.h" #include "../include/cmds.h"
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h>
static void *serverMain(void *server); static void *serverMain(void *server);
@ -38,131 +39,117 @@ void ServerInit(Server_t *server)
pthread_create(server->id, NULL, serverMain, server); pthread_create(server->id, NULL, serverMain, server);
} }
// -------------------------------------------------------------------------- // #define BUFFER_SIZE 80
// Socket init function // static void serverCommunicationInstance(Server_t *args, int sockfd)
// -------------------------------------------------------------------------- //
static inline int createSocket(void)
{ {
int newSocket;
printLog("Create the socket\n");
newSocket = socket(AF_INET, SOCK_STREAM, 0);
return newSocket;
}
// -------------------------------------------------------------------------- //
// Soecket binding function //
// -------------------------------------------------------------------------- //
static inline int bindSocket(int newSocket)
{
int effectiveSocket = -1;
int clientPort = 90190;
struct sockaddr_in remote = {0};
// Internet address family
remote.sin_family = AF_INET;
// Any incoming interface
remote.sin_addr.s_addr = htonl(INADDR_ANY);
remote.sin_port = htons(clientPort); // Local port
effectiveSocket = bind(newSocket,(struct sockaddr*)&remote,sizeof(remote));
return effectiveSocket;
}
// -------------------------------------------------------------------------- //
// Server main function //
// -------------------------------------------------------------------------- //
static void *serverMain(void *server)
{
Server_t *args;
int socketDescriptor, effectiveSocket, clientRequestLength;
struct sockaddr_in client;
char clientRequest[255]= {0};
char serverAnswer[255] = {0};
char **argv = NULL; char **argv = NULL;
char *commandReturn; char *buff, *commandReturn;
int tokenIndex; int tokenIndex;
// Get args
args = (Server_t*) server;
printLog("Server #%lu online\n", *args->id);
//Create socket
socketDescriptor = createSocket();
if (socketDescriptor < 0) {
printLog("Could not create socket\n");
return NULL;
}
printLog("Socket created\n");
//Bind
if( bindSocket(socketDescriptor) < 0) {
//print the error serverAnswer
printLog("Socket bind failed\n");
return NULL;
}
printLog("Socket bind succeeded\n");
//Listen
listen(socketDescriptor, 3);
//Accept and incoming connection //Accept and incoming connection
while(!args->pleaseStop) { while(!args->pleaseStop) {
// Zeroing buffer
bzero(buff, BUFFER_SIZE);
printLog("Waiting for incoming connections...\n"); printLog("Waiting for incoming connections...\n");
clientRequestLength = sizeof(struct sockaddr_in);
//accept connection from an incoming client // Read the message from client and copy it in buffer
effectiveSocket = accept(socketDescriptor,(struct sockaddr *)&client,(socklen_t*)&clientRequestLength); read(sockfd, buff, sizeof(buff));
if (effectiveSocket < 0) { // Print buffer which contains the client request
printLog("Acceptation failed\n"); printLog("Client request : %s\n", buff);
return NULL;
}
printLog("Connection accepted\n");
memset(clientRequest, '\0', sizeof(clientRequest));
strcpy(serverAnswer,"Invalid command\0");
//Receive REQUEST from client
if (recv(effectiveSocket, clientRequest, 200, 0) < 0) {
printLog("Reception failed");
break;
}
printLog("Client request : %s\n",clientRequest);
// get args in an array // get args in an array
tokenIndex = 0; tokenIndex = 0;
argv = (char**) realloc(argv, 1 * sizeof(char*)); argv = (char**) realloc(argv, 1 * sizeof(char*));
argv[0] = strtok(clientRequest, " "); argv[0] = strtok(buff, " ");
while (argv[tokenIndex]) { while (argv[tokenIndex]) {
tokenIndex++; tokenIndex++;
argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*)); argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*));
argv[tokenIndex] = strtok(NULL, " "); argv[tokenIndex] = strtok(NULL, " ");
} }
// test first arg to find command in cmdList // Zeroing buffer
bzero(buff, BUFFER_SIZE);
// Execute command by first arg in cmdList
for (int i = 0; i < LEN(cmdList); i++) { for (int i = 0; i < LEN(cmdList); i++) {
if (strcmp(cmdList[i].name, argv[0]) == 0) { if (strcmp(cmdList[i].name, argv[0]) == 0) {
commandReturn = cmdList[i].execute(argv, args); commandReturn = cmdList[i].execute(argv, args);
strcpy(serverAnswer, commandReturn);
// Copy server message in buffer
strcpy(buff, commandReturn);
free(commandReturn); free(commandReturn);
} }
} }
// REPLY to client // and send that buffer to client
if (send(effectiveSocket, serverAnswer, strlen(serverAnswer), 0) < 0) { write(sockfd, buff, sizeof(buff));
printLog("Send failed\n");
return NULL;
}
close(effectiveSocket);
} }
free(argv); free(argv);
}
// -------------------------------------------------------------------------- //
// Server main function //
// -------------------------------------------------------------------------- //
#define PORT 9000
static void *serverMain(void *server)
{
Server_t *args;
int sockfd, connfd;
uint len;
struct sockaddr_in servaddr, cli;
// Get args
args = (Server_t*) server;
printLog("Server #%lu online\n", *args->id);
// Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printLog("Socket creation failed!\n");
return NULL;
}
// Prepare binding structure
bzero(&servaddr, sizeof(servaddr));
// Assign IP and PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
// Binding newly created socket
if ((bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) != 0) {
printLog("Socket bind failed!\n");
return NULL;
}
// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0) {
printLog("Socket listening failed!\n");
return NULL;
}
printLog("Server listening...\n");
len = sizeof(cli);
// Accept the data packet from client
connfd = accept(sockfd, (struct sockaddr*)&cli, &len);
if (connfd < 0) {
printLog("Server acccept failed!\n");
return NULL;
}
printLog("Client accepted\n");
// Function for chatting between client and server
serverCommunicationInstance(args, connfd);
// After communication ended close the socket
close(sockfd);
printLog("Server #%lu offline\n", *args->id); printLog("Server #%lu offline\n", *args->id);
return NULL; return NULL;