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 <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_PORT 9000
void connectedCommunication(int sockfd)
{
char buff[BUFFERSIZE], curChar;
char sendBuff[SEND_BUFFER_SIZE], receiveBuff[RECEIVE_BUFFER_SIZE], curChar;
char *exitCommand = "exit";
int i;
for (;;) {
// Zeroing buffer
bzero(buff, sizeof(buff));
bzero(sendBuff, sizeof(buff));
printf("\ngem-graph console> ");
i = 0;
// Read command from terminal
while (( curChar= getchar()) != '\n')
buff[i++] = curChar;
sendBuff[i++] = curChar;
// Check null-sized string
if (buff[0] == 0) {
if (sendBuff[0] == 0) {
continue;
}
// Quit if asked
if (strcmp(exitCommand, buff) == 0) {
if (strcmp(exitCommand, sendBuff) == 0) {
return;
}
// Otherwise send command to server
write(sockfd, buff, sizeof(buff));
write(sockfd, sendBuff, sizeof(sendBuff));
// Zeroing buffer
bzero(buff, sizeof(buff));
bzero(receiveBuff, sizeof(receiveBuff));
// Reading server answer
read(sockfd, buff, sizeof(buff));
read(sockfd, receiveBuff, sizeof(receiveBuff));
printf("%s\n", buff);
printf("%s\n", receiveBuff);
}
}