New network code (better buffers)

This commit is contained in:
Adrien Bourmault 2021-07-08 00:30:25 +02:00
parent 9933c41a1e
commit fa3efd69e5
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
1 changed files with 11 additions and 10 deletions

View File

@ -26,48 +26,49 @@
#include <netdb.h> #include <netdb.h>
#include <sys/socket.h> #include <sys/socket.h>
#define BUFFERSIZE 80 #define SEND_BUFFER_SIZE 80
#define RECEIVE_BUFFER_SIZE 80
#define SERVER_IP_ADDR "127.0.0.1" #define SERVER_IP_ADDR "127.0.0.1"
#define SERVER_PORT 9000 #define SERVER_PORT 9000
void connectedCommunication(int sockfd) void connectedCommunication(int sockfd)
{ {
char buff[BUFFERSIZE], curChar; char sendBuff[SEND_BUFFER_SIZE], receiveBuff[RECEIVE_BUFFER_SIZE], curChar;
char *exitCommand = "exit"; char *exitCommand = "exit";
int i; int i;
for (;;) { for (;;) {
// Zeroing buffer // Zeroing buffer
bzero(buff, sizeof(buff)); bzero(sendBuff, sizeof(buff));
printf("\ngem-graph console> "); printf("\ngem-graph console> ");
i = 0; i = 0;
// Read command from terminal // Read command from terminal
while (( curChar= getchar()) != '\n') while (( curChar= getchar()) != '\n')
buff[i++] = curChar; sendBuff[i++] = curChar;
// Check null-sized string // Check null-sized string
if (buff[0] == 0) { if (sendBuff[0] == 0) {
continue; continue;
} }
// Quit if asked // Quit if asked
if (strcmp(exitCommand, buff) == 0) { if (strcmp(exitCommand, sendBuff) == 0) {
return; return;
} }
// Otherwise send command to server // Otherwise send command to server
write(sockfd, buff, sizeof(buff)); write(sockfd, sendBuff, sizeof(sendBuff));
// Zeroing buffer // Zeroing buffer
bzero(buff, sizeof(buff)); bzero(receiveBuff, sizeof(receiveBuff));
// Reading server answer // Reading server answer
read(sockfd, buff, sizeof(buff)); read(sockfd, receiveBuff, sizeof(receiveBuff));
printf("%s\n", buff); printf("%s\n", receiveBuff);
} }
} }