Skip to content

Commit

Permalink
Added TCP server with 2 connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao-Maria-Janeiro committed Mar 11, 2020
1 parent 995c539 commit cd02221
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
Binary file modified main
Binary file not shown.
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int checkIp(char *);
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, "192.168.1.16");
strcpy(ip, "127.0.0.1");
char* port = argv[2];

while(1) { // The code will run until the "exit" command is summoned
Expand Down
58 changes: 42 additions & 16 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
#include <errno.h>



#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif

int fd,errcode, newfd, afd=0;
int fd,errcode, newfd, afd=0, second_tcp_fd = 0;
int maxfd, counter;
ssize_t n;
fd_set rfds;
Expand All @@ -27,7 +26,8 @@ struct addrinfo hints,*res;
struct sockaddr_in addr;
char buffer[128];

enum {idle, busy} state;
typedef enum {idle, busy} state;


void createServer(char*port) {
fd=socket(AF_INET,SOCK_STREAM,0);
Expand Down Expand Up @@ -56,46 +56,72 @@ void createServer(char*port) {

if(listen(fd,5)==-1)/*error*/exit(1);

state = idle;
state state_first_tcp;
state state_second_tcp;

state_first_tcp = idle;


while(1) {
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
maxfd = fd;

if(state == busy) {
if(state_first_tcp == busy) {
FD_SET(afd, &rfds);
maxfd = max(maxfd, afd);
}

if(state_second_tcp == busy) {
FD_SET(second_tcp_fd, &rfds);
maxfd = max(maxfd, second_tcp_fd);
}

counter = select(maxfd + 1, &rfds, (fd_set*)NULL, (fd_set*)NULL, (struct timeval *)NULL);
if(counter <= 0) exit(1);

if(FD_ISSET(fd, &rfds)) {
addrlen = sizeof(addr);
if((newfd = accept(fd, (struct sockaddr*)&addr, &addrlen)) == -1) exit(1);
switch(state) {
switch(state_first_tcp) {
case idle:
afd = newfd;
state = busy;
break;
afd = newfd;
state_first_tcp = busy;
break;
case busy:
close(newfd);
break;
if(state_second_tcp == idle) {
second_tcp_fd = newfd;
state_second_tcp = busy;
break;
}
close(newfd);
break;
}
}

if(FD_ISSET(afd, &rfds)) {
if((n = read(afd, buffer, 128)) != 0) {
if(n==-1) exit(1);
// Add write function
n=write(newfd,buffer,n);
if(n==-1) exit(1);
write(1,"received: ",10);write(1,buffer,n); // Print incoming message
n=write(afd,"Server Response\n",n);
} else {
printf("\n\n\n\nCLLLOOSSEEE\n\n\n\n");
close(afd);
state = idle;
state_first_tcp = idle;
}
}

if(FD_ISSET(second_tcp_fd, &rfds)) {
if((n = read(second_tcp_fd, buffer, 128)) != 0) {
if(n==-1) exit(1);
write(1,"received: ",10);write(1,buffer,n); // Print incoming message
n=write(second_tcp_fd,buffer,n);
} else {
close(second_tcp_fd);
state_second_tcp = idle;
}
}

}
close(fd);
}
}
Binary file modified tester
Binary file not shown.
18 changes: 11 additions & 7 deletions tester.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <string.h>
#include <math.h>
#include <sys/select.h>
#define PORT "58"
#define PORT "58006"

#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
Expand All @@ -31,19 +31,23 @@ void main() {
hints.ai_family=AF_INET; //IPv4
hints.ai_socktype=SOCK_STREAM; //TCP socket

errcode=getaddrinfo("192.168.1.7",PORT,&hints,&res);
errcode=getaddrinfo("127.0.0.1",PORT,&hints,&res);
if(n!=0)/*error*/exit(1);

n=connect(fd,res->ai_addr,res->ai_addrlen);
if(n==-1)/*error*/exit(1);

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

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

write(1,"Teste: ",6); write(1,buffer,n);
write(1,"Teste: ",6); write(1,buffer,n);
}

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

0 comments on commit cd02221

Please sign in to comment.