Socket C communication between two servers

问题内容:

I try to communicate two servers using C sockets, my servers use the UDP protocol. I first created two programs (server1 and server2), server1 has the argument ipv6 address and the port and it receives the address and port of the second server. The second server has its address, its port, the address of the server 1 and the port of the server 1 in argument, it must send its address and its port to the server 1 then receive any chain (here, what it has already sent to the server1). To send the address and the port of the server 2 -> server 1 it is good, but the server 1 can not send anything to the server two, it remains blocked to recvfrom. I do not know how to do. Here are the two programs:

server 1 :

int main(int argc, char **argv) {
int sockfd, sockfd2, i = 0;
char buf[1024];
char ip_port[1024];
char ip[1024];
char* port;
socklen_t addrlen;
memset(ip_port, '\0', 1024);
memset(ip, '\0', 1024);

struct sockaddr_in6 my_addr = {0};
struct sockaddr_in6 client = {0}; // useful later
struct sockaddr_in6 server = {0};

// check the number of args on command line
if(argc != 3)
{
    printf("Usage: %s ip port\n", argv[0]);
    exit(-1);
}

// socket factory
if((sockfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
    perror("socket");
    exit(EXIT_FAILURE);
}

// init local addr structure and other params
my_addr.sin6_family      = AF_INET6;
my_addr.sin6_port        = htons(atoi(argv[2]));
my_addr.sin6_addr            = in6addr_any;
addrlen                  = sizeof(struct sockaddr_in6);
memset(buf,'\0',1024);

// bind addr structure with socket
if(bind(sockfd, (struct sockaddr *) &my_addr, addrlen) == -1) {
    perror("bind");
    close(sockfd);
    exit(EXIT_FAILURE);
}

if(recvfrom(sockfd, ip_port, 1024, 0
            , (struct sockaddr *) &serveur, &addrlen) == -1)
{
    perror("recvfrom");
    close(sockfd);
    exit(EXIT_FAILURE);
}

printf("%s\n", ip_port);

while(ip_port != '\0' && ip_port[i] != ' ') {
    ip[i] = ip_port[i];
    i++;
}
port = strchr(ip_port, ' ');
for(i = 0; port[i]; i++) port[i] = port[i+1];

// socket factory
if((sockfd2 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
    perror("socket");
    exit(EXIT_FAILURE);
}

server.sin6_family = AF_INET6;
server.sin6_port = htons(atoi(port));

if(inet_pton(AF_INET6, ip, &(server.sin6_addr)) != 1) {
perror("inet_pton");
    close(sockfd);
    exit(EXIT_FAILURE); }

if (sendto(sockfd2, ip_port, strlen(ip_port), 0, (struct sockaddr *) &server, addrlen) == -1) {
    perror("sendto");
    close(sockfd);
    exit(EXIT_FAILURE);
}
// close the socket
close(sockfd);

return 0;
}

server 2 :

int main(int argc, char **argv) {
int sockfd, sockfd2;
char buf[1024];
char ip_port[1024];
socklen_t addrlen;
memset(ip_port, '\0', 1024);

struct sockaddr_in6 my_addr = {0};
struct sockaddr_in6 server = {0};
struct sockaddr_in6 client = {0};

// check the number of args on command line
if(argc != 5)
{
    printf("Usage: %s ip port ip_server2 port_server2 \n", argv[0]);
    exit(-1);
}

// socket factory
if((sockfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
    perror("socket");
    exit(EXIT_FAILURE);
}

if((sockfd2 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
    perror("socket");
    exit(EXIT_FAILURE);
}

// init local addr structure and other params
my_addr.sin6_family      = AF_INET6;
my_addr.sin6_port        = htons(atoi(argv[2]));
my_addr.sin6_addr        = in6addr_any;
server.sin6_family       = AF_INET6;
server.sin6_port         = htons(atoi(argv[4]));
addrlen                  = sizeof(struct sockaddr_in6);
memset(buf,'\0',1024);

if(inet_pton(AF_INET6, argv[3], &(server.sin6_addr)) != 1) {
perror("inet_pton");
    close(sockfd);
    exit(EXIT_FAILURE);
}

strcat(ip_port, argv[1]);
strcat(ip_port, " ");
strcat(ip_port, argv[2]);

if (sendto(sockfd2, ip_port, strlen(ip_port), 0, (struct sockaddr *) &serveur, addrlen) == -1) {
    perror("sendto");
    close(sockfd);
    exit(EXIT_FAILURE);
}

// bind addr structure with socket
if(bind(sockfd, (struct sockaddr *) &my_addr, addrlen) == -1) {
    perror("bind");
    close(sockfd);
    exit(EXIT_FAILURE);
}

if(recvfrom(sockfd, buf, 1024, 0
            , (struct sockaddr *) &client, &addrlen) == -1)
{
    perror("recvfrom");
    close(sockfd);
    exit(EXIT_FAILURE);
}

printf("IP PORT : %s\n", buf);

// close the socket
close(sockfd);

return 0;
}

Have a good day and thanks.

问题评论:

    
Sounds like you could benefit from a Sockets tutorial. Search for “sockets beej” in Google.
    
In Server2: It would be prudent to bind sockfd before you sendto server1.
    
remove at least all compilation error

原文地址:

https://stackoverflow.com/questions/47754797/socket-c-communication-between-two-servers

添加评论