What is the effect of the bind() function on a client code?

Asked

Viewed 133 times

2

I was wandering through Google Images behind a state diagram for server and client model and I ended up finding this here:

Diagrama

I aroused curiosity regarding the image above due to the fact that the use of the function is cited bind() on the client’s side. This seemed a little strange, because I learned that the function bind() should only be used by the server and not by the client.

However, I come here with the following doubts:

  • What a role the function bind() plays in a client code?
  • Why in the diagram says that the use of the function bind() is optional?
  • As the function bind() could be applied in the code below?

    client. c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include <netdb.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    #define SRV_ADDR "127.0.0.1"
    #define SRV_PORT "9009"
    
    int make_socket(void){
    
        int ecode, sockfd;
    
        struct addrinfo *results=NULL, hints;
    
        memset(&hints, 0, sizeof(struct addrinfo));
    
        hints.ai_family=AF_INET;
        hints.ai_socktype=SOCK_STREAM;
        hints.ai_protocol=IPPROTO_TCP;
    
        if((ecode=getaddrinfo(SRV_ADDR, SRV_PORT, &hints, &results))!=0){
    
            sockfd=-1;
    
        }else{
    
            struct addrinfo *it=NULL;
    
            for(it=results; it!=NULL; it=it->ai_next){
    
                if((sockfd=socket(it->ai_family, it->ai_socktype, it->ai_protocol))==-1){
    
                    continue;
                }
    
                if(connect(sockfd, it->ai_addr, it->ai_addrlen)==0){
    
                    break;
                }
    
                close(sockfd);
            }
    
            freeaddrinfo(results);
    
            if(it==NULL){
    
                sockfd=-1;
            }
        }
    
        return sockfd;
    }
    
    short get_msg(char *msg, size_t n){
    
        int rv;
    
        if(fgets(msg, n, stdin)!=NULL){
    
            size_t len=strlen(msg);
    
            msg[len-1]='\0'; //elimina o \n
    
            rv=0;
    
        }else{
    
            rv=-1;
        }
    
        return rv;
    }
    
    int main(void){
    
        int sockfd=make_socket();
    
        if(sockfd!=-1){
    
            short rv;
            char msg[101];
    
            printf("\n");
    
            do{
    
                do{
    
                    printf("Você >");
    
                    if((rv=get_msg(msg, 101))!=0){
    
                        printf("\n* Falha na leitura!");
                        printf("\n  |.__Escreva a mensagem novamente.\n\n");
                    }
    
                    send(sockfd, msg, 101, 0);
    
                }while(rv!=0);
    
                if(strcmp(msg, "!exit")!=0){
    
                    recv(sockfd, msg, 101, 0);
    
                    printf("Estranho: %s\n", msg);
                }
    
            }while(strcmp(msg, "!exit")!=0);
    
            close(sockfd);
        }
    
        return 0;
    }
    

1 answer

0


If no bind is made in the socket used in connect then an automatic bind is made on an available port, usually a port called "ephemeral". (This also happens when you bind to port 0).

In general the 'bind' is not done on the local port in a client connection (created by 'connect') because there is no need, but eventually there may be use cases that this can be done. You could create a server application that only accepts connections from a client that uses a specific local port, for example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.