简体   繁体   中英

socket programming , ipv6 client program not working

Below is my code for IPv6 client program on local machine. When I run this program it just pauses like Reading Mode , not even printing "Start:". When I comment the line of socket() system call , then only it proceeds .

#include<stdio.h> 
#include<string.h>    
#include<sys/socket.h>
#include<arpa/inet.h> 
#include<unistd.h>
#include<errno.h>

int main( int argc, char *argv[])
{
    printf("Start:");
    int sock;
    char msg[20];
    struct sockaddr_in6 server;

    server.sin6_family=AF_INET6;
    server.sin6_port=htons(8888);
    inet_pton(AF_INET6, "127.0.0.1", &(server.sin6_addr) );


    sock=socket(AF_INET6, SOCK_STREAM , 0);
    if( sock == -1)
    {
     perror("Socket Creation Failed");
     return 1;
    }

    printf("Connecting");

   if( connect(sock, (struct sockaddr *)&server , sizeof(server)) < 0)
   {
    perror("Connection Failed");
    return 1;
   }

   if( read(sock, msg , sizeof(msg)) < 0)
   {
    perror("Reading Failed");
    return 1;
   }

   puts(msg);
   }

The problem was the inet_pton function . After passing the loopback address in IPv6 formate (0:0:0:0:0:0:0:1) , the program runs fine.

Your program is stuck at the blocking read(sock...). You need to add every where you have printf \\n to flush output to STDOUT to see output with printf!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM