Skip to content

Commit

Permalink
updating commands and server
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcisio-marinho committed Dec 6, 2017
1 parent 2943c84 commit 0c59d26
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 29 deletions.
Binary file modified C/bin/client
Binary file not shown.
Binary file modified C/bin/server
Binary file not shown.
2 changes: 1 addition & 1 deletion C/lib/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ char * execute(char * command){
memset(output, 0, MAX_TERMINAL_OUTPUT);

if (!(fpipe = (FILE*)popen(command,"r"))){
error("Pipe error");
return NULL;
}

while ( fgets( line, 256, fpipe)){
Expand Down
21 changes: 5 additions & 16 deletions C/lib/communication.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,16 @@ int listen_forever(){
int addrlen = sizeof(address);
char buffer[size] = {0};
while(1){
int rest = 0;

// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
perror("socket failed");
exit(EXIT_FAILURE);
error("socket failed");
}

// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt))){
perror("setsockopt");
exit(EXIT_FAILURE);
error("setsockopt");
}

address.sin_family = AF_INET;
Expand All @@ -115,26 +112,18 @@ int listen_forever(){
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0){
perror("bind failed");
exit(EXIT_FAILURE);
error("bind failed");
}

if (listen(server_fd, 3) < 0){
perror("listen");
exit(EXIT_FAILURE);
error("listen");
}

if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0){
perror("accept");
exit(EXIT_FAILURE);
error("accept");
}

if(rest == 1){
printf("Dormindo por 5 segundos");
sleep(5);
continue;
}
return new_socket;
}
}
Expand Down
29 changes: 17 additions & 12 deletions C/server.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
#include "lib/include.h"


int main(){
int main(int argc, char *argv[]){

int new_socket, bytes_read, bytes_sent;
char buffer[size] = {0};
char output[MAX_TERMINAL_OUTPUT] = {0};
new_socket = listen_forever();

while(1){
printf(">>> ");
fgets(buffer, 100, stdin);
bytes_sent = send(new_socket, buffer, size, 0);
bytes_read = recv( new_socket , output, MAX_TERMINAL_OUTPUT, 0);
if(bytes_read == 0 || bytes_read == -1){
printf("Client Disconnected\n");
exit(-1);
}else{
printf("%s\n", output );
memset(output , 0, sizeof(output));
printf("Trying to connect\n");
new_socket = listen_forever();
printf("Connected!\n");

while(1){
printf(">>> ");
fgets(buffer, 100, stdin);
bytes_sent = send(new_socket, buffer, size, 0);
bytes_read = recv( new_socket , output, MAX_TERMINAL_OUTPUT, 0);
if(bytes_read == 0 || bytes_read == -1){
printf("Client Disconnected\n");
break;
}else{
printf("%s\n", output );
memset(output , 0, sizeof(output));
}
}
}
return 0;
Expand Down

0 comments on commit 0c59d26

Please sign in to comment.