Skip to content

Commit

Permalink
new i missing exit abruply
Browse files Browse the repository at this point in the history
Co-authored-by: João Maria Janeiro <[email protected]>
  • Loading branch information
Guilherme-Viegas and Joao-Maria-Janeiro committed Mar 12, 2020
1 parent 6b42171 commit 8b14374
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 19 deletions.
Binary file modified main
Binary file not shown.
16 changes: 7 additions & 9 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,25 @@ int checkIpPortValid(int, char **);
int valid_digit(char *);
int checkIp(char *);

char aux[100];


int main(int argc, char *argv[]) {
if(!checkIpPortValid(argc, argv)) exit(0); // Check if the dkt program was summoned with 3 arguments
char ip[15];
strcpy(ip, "127.0.0.1");
char* port = argv[2];
Server * myServer = (Server*)malloc(sizeof(Server));

strcpy(myServer->myIp, "127.0.0.1");
strcpy(myServer->myPort, argv[2]);


while(1) { // The code will run until the "exit" command is summoned
char buffer[100];
char lixo[100];
// Show the user interface
printf("Available commands:\n\n new i - create a new ring with the i server only \n exit\n");
fgets(buffer, 100 , stdin);
const char delim[2] = " ";
if(strcmp(strtok(strdup(buffer), delim), "new") == 0) { // Check if the command is the new command
int server;
sscanf(buffer, "%s %d", lixo, &server); // Get the server port
printf("Entrou\n");
createServer(port);
sscanf(buffer, "%s %d", aux, &(myServer->key)); // Get the server key
createServer(myServer);
} else if(strcmp(buffer, "exit\n") == 0) {
exit(0);
}
Expand Down
8 changes: 4 additions & 4 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ fd_set rfds;
socklen_t addrlen;
struct addrinfo hints,*res;
struct sockaddr_in addr;
char buffer[128];
char buffer[128] = "";



void createServer(char*port) {
void createServer(Server* server) {
fd=socket(AF_INET,SOCK_STREAM,0);
if (fd==-1) {
printf("SOCKET ERROR\n");
Expand All @@ -42,7 +42,7 @@ void createServer(char*port) {
hints.ai_socktype=SOCK_STREAM;
hints.ai_flags=AI_PASSIVE;

errcode=getaddrinfo(NULL,port,&hints,&res);
errcode=getaddrinfo(NULL,server->myPort,&hints,&res);
if((errcode)!=0) {
printf("GETADDRINFO ERROR\n");
/*error*/exit(1);
Expand Down Expand Up @@ -112,7 +112,7 @@ void createServer(char*port) {
} else {
buffer[n] = '\0';
write(1,"received: ",11); write(1,buffer,n); // Print incoming message
n = write(newfd,"Server Response\n",n);
n = write(newfd,"Server Response\n",17);
if(n==-1)/*error*/exit(1);
}
}
Expand Down
6 changes: 3 additions & 3 deletions server.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#define _SERVER_H

typedef struct server {
char* myIp;
char* myPort;
char myIp[30];
char myPort[10];
int key;

char* nextIp;
Expand All @@ -12,7 +12,7 @@ typedef struct server {
}Server;


void createServer(char*);
void createServer(Server*);


#endif
Binary file modified test/tester
Binary file not shown.
6 changes: 3 additions & 3 deletions test/tester.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ void main(int argc, char *argv[]) {
n=connect(fd,res->ai_addr,res->ai_addrlen);
if(n==-1)/*error*/exit(1);

while(1) {
// while(1) {
char user_input[128];
fgets(user_input, 100 , stdin);
n=write(fd,user_input,128);
n=write(fd,user_input, strlen(user_input));
if(n==-1)/*error*/exit(1);

n=read(fd,buffer,128);
if(n==-1)/*error*/exit(1);

write(1,"Teste: ",8); write(1,buffer,n); write(1, "\n", 3);
}
// }

freeaddrinfo(res);
close(fd);
Expand Down

0 comments on commit 8b14374

Please sign in to comment.