Antarus
Antarus

Reputation: 1621

Unable to connect 2 machines using ipv6 (TCP server client )

I was trying to do a simple tcp server client using ipv6. It works on the same machine for ipv6 and ipv4 but when on different machines ipv6 fails to connect.

Server Code

int main(int argc, char* argv[])
{
    int sockfd,new_fd,rv,yes=1; 
    struct addrinfo hints,*servinfo,*p; 
    struct sockaddr_storage their_addr;
    socklen_t addr_size;

    SOCKET listenSocket,clientSocket;
    WSADATA w;

    if (WSAStartup(0x0101, &w) != 0)
    {
        fprintf(stderr, "Could not open Windows connection.\n");
        exit(0);
    }

    //ip=argv[1];
    //port=argv[2];

    memset(&hints,0,sizeof(hints));

    hints.ai_family=AF_INET6;
    hints.ai_socktype=SOCK_STREAM;  
    hints.ai_flags=AI_NUMERICHOST;

    if((rv = getaddrinfo("fe80::c0a8:0160","5002",&hints,&servinfo)) != 0)
    {
        perror("\nGetaddrinfo failed\n");
        return 1;
    }   

    //Creating socket   
    listenSocket = socket(servinfo->ai_family,servinfo->ai_socktype,servinfo->ai_protocol);

    if(listenSocket == INVALID_SOCKET)
    {
        printf("\nSocket failed with error \n");
        WSACleanup();
    }

    //setting non blocking mode
    u_long iMode = 1;
    rv = ioctlsocket(listenSocket,FIONBIO,&iMode);

    if(rv == SOCKET_ERROR)
    {
        printf("\nioctl failed\n");
        WSACleanup();
    }

    rv = bind(listenSocket,servinfo->ai_addr,(int)servinfo->ai_addrlen);

    if(rv == SOCKET_ERROR)
    {
        perror("\nBind: \n");
    }

    freeaddrinfo(servinfo);

    rv = listen(listenSocket,SOMAXCONN);

    if(rv == SOCKET_ERROR)
    {
        perror("listen");
        return 1;
    }

    // now accept an incoming connection:


    char recvbuf[DEFAULT_BUFLEN];
    int buflen = DEFAULT_BUFLEN;
    SOCKET AcceptSocket;

    while (1)
    {
        AcceptSocket = SOCKET_ERROR;

        while (AcceptSocket == SOCKET_ERROR)
        {
            AcceptSocket = accept(listenSocket, NULL, NULL);
        }

        printf("Server: Client Connected!\n");
        listenSocket = AcceptSocket;

        rv = recv(listenSocket,recvbuf,buflen,0);
        break;
    }


    printf("Received %d bytes from client \n",rv);  

    closesocket(listenSocket);
    closesocket(AcceptSocket);

    return 0;

}

Client Code

int main(int argc,char* argv[])
{
    struct addrinfo hints,*servinfo,*p;
    int rv;
    SOCKET connectSocket;
    WSADATA w;

    if (WSAStartup(0x0101, &w) != 0)
    {
        fprintf(stderr, "Could not open Windows connection.\n");
        exit(0);
    }

    //resetting memory
    memset(&hints,0,sizeof(hints));

    hints.ai_family = AF_INET6;
    hints.ai_socktype = SOCK_STREAM;    
    hints.ai_flags = AI_NUMERICHOST;

    //getting values

    if((rv = getaddrinfo("fe80::c0a8:160","5002",&hints,&servinfo)) != 0)
    {
        perror("Getaddrinfo failed");
        return 1;
    }

    //Creating socket
    connectSocket = socket(servinfo->ai_family,servinfo->ai_socktype,servinfo->ai_protocol);

    if(connectSocket == INVALID_SOCKET)
    {
        perror("Socket create : ");
    }

    rv = connect(connectSocket,servinfo->ai_addr,(int)servinfo->ai_addrlen);
    if(rv == SOCKET_ERROR)
    {
        perror("Socket Connect : ");
    }


    //free memory
    freeaddrinfo(servinfo);


    // Send and receive data.
    int bytesSent;

    char sendbuf[200] = "Client: Sending some test string to server...";
    char recvbuf[200] = "";

    bytesSent = send(connectSocket, sendbuf, strlen(sendbuf), 0);
    printf("Client: send() - Bytes Sent: %ld\n", bytesSent);

    closesocket(connectSocket);
    return 0;
}

The aim is just to print how many bytes transferred.

Upvotes: 1

Views: 1696

Answers (1)

shinkou
shinkou

Reputation: 5154

It appears that you're using a link local address. Are you sure for that? Also, I'd suggest you check your firewall settings first.

EDIT: Try to include the zone ID. When you issue the ipconfig in command prompt, you should be able to get addresses like fe80::c0a8:0160%21 where %21 is the zone ID. It's important when you use link local addresses according to this answer.

Upvotes: 3

Related Questions