TCP Client connecting to two TCP Servers

FS Code needs to be like this

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>


int max(int fd, int fd2){
    if(fd > fd2) return fd;
    else return fd2;

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

    int ASfd, errcode, listenfd, User_fd;
    ssize_t n;
    socklen_t addrlen, addrlen2;
    struct addrinfo hints, hints2, *res, *res2;
    struct sockaddr_in addr;
    struct sockaddr_in addr2;
    fd_set readfds;

    int pid;

    char* AS_PORT = argv[3];
    char* AS_IP = argv[2];
    char* FS_PORT = argv[1];
    char buffer[364];
    char check[4],  num_aluno[6],  Fname[64],  Fcontent[128]; 
    long Fsize;

    //TCP SOCKET
    listenfd = socket(AF_INET,SOCK_STREAM,0);
    if(listenfd == -1) exit(1);

    memset(&hints2,0,sizeof hints2);
    hints2.ai_family=AF_INET;
    hints2.ai_socktype=SOCK_STREAM;
    hints2.ai_flags=AI_PASSIVE;

    errcode = getaddrinfo(NULL,FS_PORT,&hints2,&res2);
    if(errcode != 0){
        perror("getaddrinfo");
        exit(1);
    }

    n=bind(listenfd,res2->ai_addr, res2->ai_addrlen);
    if(n==-1) exit(1);

    if(listen(listenfd,10) == -1) exit(1);

    ASfd = socket(AF_INET,SOCK_DGRAM,0);
    if(ASfd==-1) exit(1);

    memset(&hints,0,sizeof hints);
    hints.ai_family=AF_INET;
    hints.ai_socktype=SOCK_DGRAM;
    hints.ai_flags=AI_PASSIVE;

    errcode = getaddrinfo(AS_IP,AS_PORT,&hints,&res);
    if(errcode != 0) exit(1);

    addrlen=sizeof(addr);
    addrlen2=sizeof(addr2);


    while(1){
        FD_CLR(ASfd,&readfds);
        FD_CLR(listenfd,&readfds);
        FD_SET(ASfd,&readfds);
        FD_SET(listenfd,&readfds);
        select(max(ASfd,listenfd)+1,&readfds,NULL,NULL,NULL);
        if(FD_ISSET(ASfd,&readfds)){
            n = recvfrom(ASfd,buffer,128,0,(struct sockaddr*)&addr,&addrlen);
            if(n == -1) exit(1);
            write(1,buffer,strlen(buffer)+1);
        }
        else if(FD_ISSET(listenfd,&readfds)){
            User_fd = accept(listenfd,(struct sockaddr*)&addr2,&addrlen2);
            if(User_fd == -1){
                perror("accept");
                exit(1);
            }            
            if ((pid = fork())==0) {
                close(listenfd);
                while(1){
                    read(User_fd,buffer,364);
                    write(1,buffer,strlen(buffer)+1);
                    sscanf(buffer,"%s",check);
                    if(strcmp(check,"UPL")==0){
                        sscanf(buffer,"%s %s %s %ld %s",check,num_aluno,Fname,&Fsize,Fcontent);
                        write(1,Fcontent,strlen(Fcontent)+1);
                    }
                }
                freeaddrinfo(res);
                close(User_fd);
                exit(0);
            }
        }
    }

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top