0

我正在尝试为 Solaris 上的 IPv6 服务器提供示例代码。当我提供链接本地地址时,它工作正常。但是当我给出全局地址时,它无法绑定。请告诉我,我们可以在 Solaris 上使用全局 IPv6 地址吗?

这是我的代码....

#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

/* the port users will be connecting to */

#define MYPORT 9000

/* how many pending connections queue will hold */

#define BACKLOG 10

int main(int argc, char *argv[ ])

{

/* listen on sock_fd, new connection on new_fd */
int sockfd, new_fd;

/* my address information */struct sockaddr_in6 their_addr;
socklen_t sin_size;

//struct sigaction sa;
int yes = 1;

if ((sockfd = socket(AF_INET6, SOCK_STREAM, 0)) == -1) 
{
    perror("Server-socket() error lol!");
    return 0;//exit(1);
}
else
printf("Server-socket() sockfd is OK...\n");

if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
{
    perror("Server-setsockopt() error lol!");
    return 0;//exit(1);
}else
    printf("Server-setsockopt is OK...\n");



/* host byte order */
my_addr.sin6_family = AF_INET6;

/* short, network byte order */
my_addr.sin6_port = htons(MYPORT);

/* automatically fill with my IP */
 inet_pton(AF_INET6,"2345:1111:aaaa::500",&my_addr.sin6_addr);
//inet_pton(AF_INET6,"fe80::203:baff:fe50:cbe5",&my_addr.sin6_addr);
my_addr.sin6_scope_id=5;

/* zero the rest of the struct */

if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr)) == -1)
    perror("Server-bind() error");
    return 0;//exit(1);
}
else
    printf("Server-bind() is OK...\n");

if(listen(sockfd, BACKLOG) == -1)
{
    perror("Server-listen() error");
    return 0;//exit(1);
}
printf("Server-listen() is OK...Listening...\n");

sin_size = sizeof(struct sockaddr_in6);

if((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
{
    perror("Server-accept() error");
    //continue;
}
else
    printf("Server-accept() is OK...\n");

printf("Server-new socket, new_fd is OK...\n");
printf("Server: Got connection from \n");

/* this is the child process */

/* child doesn’t need the listener */
char buf[1024];
int numbytes=0;
if((numbytes = recv(new_fd, buf, 1024, 0)) == -1)
{
    perror("recv()");
    return 1;//exit(1);
}
else
    printf("Client-The recv() is OK...\n");

buf[numbytes] = '\0';printf("Client-Received: %s", buf);
if(send(new_fd, "This is a test string from server!\n", 37, 0) == -1)
        perror("Server-send() error lol!");

/* parent doesn’t need this*/
close(new_fd);
printf("Server-new socket, new_fd closed successfully...\n");
return 0;
}

谢谢 ....

4

3 回答 3

1

2345:1111:aaaa::500 是网络掩码,IP 是 fe80::203:baff:fe50:cbe5

我认为您将需要范围 id (2) 才能 bind() 才能工作,因为它是 Link-Local IPv6 地址。

  • 马勒斯
于 2012-08-17T16:06:50.727 回答
1

是否2345:1111:aaaa::500在您的任何接口上配置?在绑定到该地址之前,这是必需的。

于 2012-06-13T14:49:16.587 回答
0

我不确定在 Solaris 上,但在 Windows 7 上,它需要管理员权限才能将套接字绑定到全局或多播地址。

您可以使用提升的权限运行测试吗?

-杰西

于 2012-06-13T14:52:15.227 回答