简体   繁体   English

套接字编程,ipv6客户端程序不起作用

[英]socket programming , ipv6 client program not working

Below is my code for IPv6 client program on local machine. 以下是我在本地计算机上的IPv6客户端程序的代码。 When I run this program it just pauses like Reading Mode , not even printing "Start:". 当我运行该程序时,它像Reading Mode一样暂停,甚至不打印“ Start:”。 When I comment the line of socket() system call , then only it proceeds . 当我注释socket()系统调用的行时,则仅继续进行。

#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 . 问题是inet_pton函数。 After passing the loopback address in IPv6 formate (0:0:0:0:0:0:0:1) , the program runs fine. 在以IPv6格式(0:0:0:0:0:0:0:0:1)传递回送地址后,程序可以正常运行。

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! 您需要添加每个具有printf \\n以将输出刷新到STDOUT以查看带有p​​rintf的输出!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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