New network code (better buffers)
This commit is contained in:
parent
9933c41a1e
commit
fa3efd69e5
21
src/cli.c
21
src/cli.c
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue