socket connect: invalid argument /TCPv6 client - problem

0

Witam.

Mam taki kod:

 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>

void error(char *msg) {
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[]) {
    int sockfd, portno, n, rc;
    struct sockaddr_in6 serv_addr;
    struct hostent *server;
    struct addrinfo hints, *res;
    struct in6_addr serveraddr;
    char buffer[256] = "This is a string from client!";

    if (argc < 3) {
        fprintf(stderr, "Usage: %s  \n", argv[0]);
        exit(0);
    } 

    printf("\nIPv6 TCP Client Started...\n");
    
    memset(&hints, 0, sizeof(hints));
    hints.ai_flags    = AI_NUMERICSERV;
    hints.ai_family   = AF_INET6;
    hints.ai_socktype = SOCK_STREAM;
    
    
    rc = getaddrinfo(argv[2],argv[1], &hints, &res);
    if (rc != 0)
    {
	printf("\nHosta nie znaleziono --> %s\n", gai_strerror(rc));
	if (rc == EAI_SYSTEM)
	  perror("\nNiepowodzenie getaddrinfo()"); 
	
    } else {
	printf("\n getaddrinfo [OK]");
    }
    
    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (sockfd < 0)
    {
      perror("Niepowodzenie socket()");
    } else {
      printf("socket created [OK]"); 
    }
    
	
    //Sockets Layer Call: connect()
    if (connect(sockfd, res->ai_addr,res->ai_addrlen) < 0) {
        error("ERROR connecting");
    } else {
	printf("Connect OK");
    }

    close(sockfd);
        
    return 0;
}

funkcja connect zwraca mi "invalid argument". Wie ktoś dlaczego ?

dzięki z góry.

0

Nie dla kazdego interfejsu bedziesz mogl utworzyc socket.
http://linux.die.net/man/3/getaddrinfo
Na dole masz swietny przyklad jak to powinno wygladac, i ze w przypadku nieprawidlowego wywolania socket() bierzemy po prostu nastepny zwrocony rekord. Chyba, ze zwraca Ci 1 interfejs, wtedy podajesz bledne argumentu dla programu.

0

Dlaczego nie dla każdego ?

Poprawiłem na coś takiego:

 

int main(int argc, char *argv[]) {
    int sockfd, portno, n, rc;
    struct sockaddr_in6 serv_addr;
    struct hostent *server;
    struct addrinfo hints, *res, *rp;
    struct in6_addr serveraddr;
    char buffer[256] = "This is a string from client!";

    if (argc < 3) {
        fprintf(stderr, "Usage: %s  \n", argv[0]);
        exit(0);
    } 

    printf("\nIPv6 TCP Client Started...\n");
    
    memset(&hints, 0, sizeof(hints));
    hints.ai_flags    = AI_NUMERICSERV;
    hints.ai_family   = AF_INET6;
    hints.ai_socktype = SOCK_STREAM;
    
    
    rc = getaddrinfo(argv[2],argv[1], &hints, &res);
    if (rc != 0)
    {
	printf("\nHosta nie znaleziono --> %s\n", gai_strerror(rc));
	if (rc == EAI_SYSTEM)
	  perror("\nNiepowodzenie getaddrinfo()"); 
	
    } else {
	printf("\n getaddrinfo [OK]\n ");
    }
    
    
   for (rp = res; rp != NULL; rp = rp->ai_next) {
        sockfd = socket(rp->ai_family, rp->ai_socktype,
                     rp->ai_protocol);
        if (sockfd == -1) {
            continue;
	} else {
	  printf("SOCKET CREATED");
	}
       if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1){
	    printf("Connected");
            break;                  /* Success */
       } else {
	  error("ERROR connecting");
       }
       close(sockfd);
    }
  
    close(sockfd);
        
    return 0;
}

Tworzy się jeden socket ale w dalszym ciągu mam ten błąd. Adres do programu podaję taki: fe80:27ffe6fb port 8000. Inne pomysły ?

1 użytkowników online, w tym zalogowanych: 0, gości: 1