日期:2014-05-16  浏览次数:20653 次

求助:ipv6编程过程中,bind函数报错,errno为29,即Illegal seek
如题,请大侠多多指导,求助。谢谢。
具体源代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <signal.h>
#include <time.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/file.h>

#define MAXBUF 1024
int main(int argc, char **argv)
{
  int sockfd, new_fd;
  socklen_t len;
  //char addrtmp[128];
  int errvalue=0;

  /* struct sockaddr_in my_addr, their_addr; */ // IPv4
  //struct sockaddr_in6 my_addr, their_addr; // IPv6
  struct sockaddr_in6 their_addr;
  struct addrinfo hints,* res;

  unsigned int myport, lisnum;
  char buf[MAXBUF + 1];

  if (argv[1])
  myport = atoi(argv[1]);
  else
  myport = 9998;

  if (argv[2])
  lisnum = atoi(argv[2]);
  else
  lisnum = 2;

  bzero(&hints,sizeof(struct addrinfo));
  hints.ai_flags=AI_PASSIVE;
  hints.ai_family=AF_INET6;
  hints.ai_socktype=SOCK_STREAM;
  errvalue=0;
  if((errvalue=getaddrinfo(argv[3],argv[1],&hints,&res))!=0)
  {
  perror("getaddrinfo");
  exit(1);
  }
  if(res==NULL)
  {
  printf("res pointer is null\n");
  exit(1);
  }
  if((sockfd=socket(res->ai_family,res->ai_socktype,res->ai_protocol))==-1)
  {
  perror("socket");
  exit(1);
  }
  const int on=1;
  setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
  if((errvalue=bind(sockfd,res->ai_addr,res->ai_addrlen))!=0) //出错位置
  {
  perror("bind");
  printf("strerror(errno)=%s\n",strerror(errno));
  printf("errvalue=%d\n",errno);
  exit(1);
  }

  if (listen(sockfd, lisnum) == -1) {
  perror("listen");
  exit(1);
  } else
  printf("begin listen\n");

  while (1) {
  len = sizeof(struct sockaddr);
  if ((new_fd =
  accept(sockfd, (struct sockaddr *) &their_addr,
  &len)) == -1) {
  perror("accept");
  exit(errno);
  } else
  printf("server: got connection from %s, port %d, socket %d\n",
  /* inet_ntoa(their_addr.sin_addr), */ // IPv4
  inet_ntop(AF_INET6, &their_addr.sin6_addr, buf, sizeof(buf)), // IPv6
  /* ntohs(their_addr.sin_port), new_fd); */ // IPv4
  their_addr.sin6_port, new_fd); // IPv6

  /* ............... */
  bzero(buf, MAXBUF + 1);
  strcpy(buf,
  "......................\n...new_fd...accept......socket.