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

175
src/cli.c
View File

@ -23,135 +23,78 @@
#include <sys/socket.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_PORT 90190
#define SERVER_PORT 9000
// -------------------------------------------------------------------------- //
// Socket init function //
// -------------------------------------------------------------------------- //
static inline int createSocket(void)
void connectedCommunication(int sockfd)
{
int socketDescriptor;
//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 buff[BUFFERSIZE];
char *exitCommand = "exit";
int n;
while(1) {
for (;;) {
// Zeroing buffer
bzero(buff, sizeof(buff));
printf("\ngem-graph console> ");
fgets(clientMessage, 255, stdin);
clientMessage[strcspn(clientMessage, "\n")] = '\0';
n = 0;
if (strcmp(exitCommand,clientMessage) == 0)
// Read command from terminal
while ((buff[n++] = getchar()) != '\n')
;
// Quit if asked
if (strcmp(exitCommand, buff) == 0) {
return;
}
// Otherwise send command to server
write(sockfd, buff, sizeof(buff));
// Zeroing buffer
bzero(buff, sizeof(buff));
// Reading server answer
read(sockfd, buff, sizeof(buff));
printf("%s\n", buff);
}
}
int main()
{
close(socketDescriptor);
shutdown(socketDescriptor, 0);
shutdown(socketDescriptor, 1);
shutdown(socketDescriptor, 2);
return 0;
int sockfd;
struct sockaddr_in servaddr;
// Socket creation
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printLog("Socket creation failed!\n");
return 1;
}
//Create socket
socketDescriptor = createSocket();
bzero(&servaddr, sizeof(servaddr));
if(socketDescriptor == -1) {
printLog("Could not create socket\n");
continue;
// 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;
}
//Connect to remote server
if (connectSocket(socketDescriptor) < 0) {
printLog("Connection failed\n");
continue;
}
//Send data to the server
sendSocket(socketDescriptor, clientMessage, strlen(clientMessage));
//Received the data from the server
receiveSocket(socketDescriptor, serverReply, 200);
printf("%s\n", serverReply);
memset(serverReply, 0, 255);
close(socketDescriptor);
shutdown(socketDescriptor, 0);
shutdown(socketDescriptor, 1);
shutdown(socketDescriptor, 2);
}
return 0;
printLog("Connected!\n");
connectedCommunication(sockfd);
// close the socket
close(sockfd);
}

View File

@ -22,8 +22,9 @@
#include "../include/base.h"
#include "../include/cmds.h"
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
static void *serverMain(void *server);
@ -38,131 +39,117 @@ void ServerInit(Server_t *server)
pthread_create(server->id, NULL, serverMain, server);
}
// -------------------------------------------------------------------------- //
// Socket init function //
// -------------------------------------------------------------------------- //
static inline int createSocket(void)
#define BUFFER_SIZE 80
static void serverCommunicationInstance(Server_t *args, int sockfd)
{
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 *commandReturn;
char *buff, *commandReturn;
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
while(!args->pleaseStop) {
// Zeroing buffer
bzero(buff, BUFFER_SIZE);
printLog("Waiting for incoming connections...\n");
clientRequestLength = sizeof(struct sockaddr_in);
//accept connection from an incoming client
effectiveSocket = accept(socketDescriptor,(struct sockaddr *)&client,(socklen_t*)&clientRequestLength);
// Read the message from client and copy it in buffer
read(sockfd, buff, sizeof(buff));
if (effectiveSocket < 0) {
printLog("Acceptation failed\n");
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);
// Print buffer which contains the client request
printLog("Client request : %s\n", buff);
// get args in an array
tokenIndex = 0;
argv = (char**) realloc(argv, 1 * sizeof(char*));
argv[0] = strtok(clientRequest, " ");
argv[0] = strtok(buff, " ");
while (argv[tokenIndex]) {
tokenIndex++;
argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*));
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++) {
if (strcmp(cmdList[i].name, argv[0]) == 0) {
commandReturn = cmdList[i].execute(argv, args);
strcpy(serverAnswer, commandReturn);
// Copy server message in buffer
strcpy(buff, commandReturn);
free(commandReturn);
}
}
// REPLY to client
if (send(effectiveSocket, serverAnswer, strlen(serverAnswer), 0) < 0) {
printLog("Send failed\n");
return NULL;
}
close(effectiveSocket);
// and send that buffer to client
write(sockfd, buff, sizeof(buff));
}
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);
return NULL;