New network code
This commit is contained in:
parent
4e9b368a72
commit
9933c41a1e
15
src/cli.c
15
src/cli.c
|
@ -33,20 +33,25 @@
|
||||||
|
|
||||||
void connectedCommunication(int sockfd)
|
void connectedCommunication(int sockfd)
|
||||||
{
|
{
|
||||||
char buff[BUFFERSIZE];
|
char buff[BUFFERSIZE], curChar;
|
||||||
char *exitCommand = "exit";
|
char *exitCommand = "exit";
|
||||||
int n;
|
int i;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// Zeroing buffer
|
// Zeroing buffer
|
||||||
bzero(buff, sizeof(buff));
|
bzero(buff, sizeof(buff));
|
||||||
printf("\ngem-graph console> ");
|
printf("\ngem-graph console> ");
|
||||||
|
|
||||||
n = 0;
|
i = 0;
|
||||||
|
|
||||||
// Read command from terminal
|
// Read command from terminal
|
||||||
while ((buff[n++] = getchar()) != '\n')
|
while (( curChar= getchar()) != '\n')
|
||||||
;
|
buff[i++] = curChar;
|
||||||
|
|
||||||
|
// Check null-sized string
|
||||||
|
if (buff[0] == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Quit if asked
|
// Quit if asked
|
||||||
if (strcmp(exitCommand, buff) == 0) {
|
if (strcmp(exitCommand, buff) == 0) {
|
||||||
|
|
21
src/server.c
21
src/server.c
|
@ -39,30 +39,33 @@ void ServerInit(Server_t *server)
|
||||||
pthread_create(server->id, NULL, serverMain, server);
|
pthread_create(server->id, NULL, serverMain, server);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define BUFFER_SIZE 80
|
#define SEND_BUFFER_SIZE 80
|
||||||
|
#define RECEIVE_BUFFER_SIZE 80
|
||||||
static void serverCommunicationInstance(Server_t *args, int sockfd)
|
static void serverCommunicationInstance(Server_t *args, int sockfd)
|
||||||
{
|
{
|
||||||
char **argv = NULL;
|
char **argv = NULL;
|
||||||
char *buff, *commandReturn;
|
char receiveBuff[RECEIVE_BUFFER_SIZE];
|
||||||
|
char sendBuff[SEND_BUFFER_SIZE];
|
||||||
|
char *commandReturn;
|
||||||
int tokenIndex;
|
int tokenIndex;
|
||||||
|
|
||||||
//Accept and incoming connection
|
//Accept and incoming connection
|
||||||
while(!args->pleaseStop) {
|
while(!args->pleaseStop) {
|
||||||
|
|
||||||
// Zeroing buffer
|
// Zeroing buffer
|
||||||
bzero(buff, BUFFER_SIZE);
|
bzero(receiveBuff, RECEIVE_BUFFER_SIZE);
|
||||||
printLog("Waiting for incoming connections...\n");
|
printLog("Waiting for incoming connections...\n");
|
||||||
|
|
||||||
// Read the message from client and copy it in buffer
|
// Read the message from client and copy it in buffer
|
||||||
read(sockfd, buff, sizeof(buff));
|
read(sockfd, receiveBuff, sizeof(receiveBuff));
|
||||||
|
|
||||||
// Print buffer which contains the client request
|
// Print buffer which contains the client request
|
||||||
printLog("Client request : %s\n", buff);
|
printLog("Client request : '%s'\n", receiveBuff);
|
||||||
|
|
||||||
// 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(buff, " ");
|
argv[0] = strtok(receiveBuff, " ");
|
||||||
while (argv[tokenIndex]) {
|
while (argv[tokenIndex]) {
|
||||||
tokenIndex++;
|
tokenIndex++;
|
||||||
argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*));
|
argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*));
|
||||||
|
@ -70,7 +73,7 @@ static void serverCommunicationInstance(Server_t *args, int sockfd)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Zeroing buffer
|
// Zeroing buffer
|
||||||
bzero(buff, BUFFER_SIZE);
|
snprintf(sendBuff, SEND_BUFFER_SIZE, "%s", "Invalid command!");
|
||||||
|
|
||||||
// Execute command by first arg in cmdList
|
// Execute command by first arg in cmdList
|
||||||
for (int i = 0; i < LEN(cmdList); i++) {
|
for (int i = 0; i < LEN(cmdList); i++) {
|
||||||
|
@ -78,13 +81,13 @@ static void serverCommunicationInstance(Server_t *args, int sockfd)
|
||||||
commandReturn = cmdList[i].execute(argv, args);
|
commandReturn = cmdList[i].execute(argv, args);
|
||||||
|
|
||||||
// Copy server message in buffer
|
// Copy server message in buffer
|
||||||
strcpy(buff, commandReturn);
|
strcpy(sendBuff, commandReturn);
|
||||||
free(commandReturn);
|
free(commandReturn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// and send that buffer to client
|
// and send that buffer to client
|
||||||
write(sockfd, buff, sizeof(buff));
|
write(sockfd, sendBuff, sizeof(sendBuff));
|
||||||
}
|
}
|
||||||
|
|
||||||
free(argv);
|
free(argv);
|
||||||
|
|
Loading…
Reference in New Issue