首页 新闻 会员 周边 捐助

send to:Address family not supported by protocol

0
悬赏园豆:20 [已关闭问题] 关闭于 2013-03-09 15:23

#include<stdio.h>
#include<string.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<errno.h>
#define PORT 8848

int main(void)
{
 int sockfd, sendnum, recvnum;
 int on = 1, sin_size =1;
 char buf[100];
 struct sockaddr_in serveraddr, clientaddr;

 serveraddr.sin_family = AF_INET;
 serveraddr.sin_port = htons(PORT);
 serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
 bzero(&(serveraddr.sin_zero), 8);

 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
 if(bind(sockfd, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr)) == -1)
 {
  perror("bind");
  exit(1);
 }
 

 sendnum = sendto(sockfd, "hello client, welcome to server.\n", 25, 0, (struct sockaddr *)&clientaddr, sizeof(struct sockaddr));
 if(sendnum == -1)
 {
  perror("sendto");
  exit(1);
 }
 printf("send message to client ok\n");

 recvnum = recvfrom(sockfd, buf, 100, 0, (struct sockaddr*)&clientaddr, &sin_size);
 if(recvnum == -1)
 {
  perror("recvnum");
  exit(1);
 }
 printf("receive %s from client\n", buf);
 close(sockfd);
 return 0;
}

 

上面的程序运行出错sendto: Address family not supported by protocol,

  如果程序中把sendto函数去掉,只有recvfrom函数则不会出错,这是怎么回事?求高手指导

问题补充:

server.c

#include<stdio.h>
#include<string.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<errno.h>
#define PORT 8848

int main(void)
{
 int sockfd, sendnum, recvnum;
 int on = 1, sin_size =1;
 char buf[100];
 struct sockaddr_in serveraddr, clientaddr;

 serveraddr.sin_family = AF_LOCAL;
 serveraddr.sin_port = htons(PORT);
 serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
 bzero(&(serveraddr.sin_zero), 8);

 sockfd = socket(PF_INET, SOCK_DGRAM, 0);
 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
 if(bind(sockfd, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr)) == -1)
 {
  perror("bind");
  exit(1);
 }
 

/* sendnum = sendto(sockfd, "hello client, welcome to server.\n", 25, 0, (struct sockaddr *)&clientaddr, sizeof(struct sockaddr));
 if(sendnum == -1)
 {
  perror("sendto");
  exit(1);
 }
 printf("send message to client ok\n");
*/
 recvnum = recvfrom(sockfd, buf, 100, 0, (struct sockaddr*)&clientaddr, &sin_size);
 if(recvnum == -1)
 {
  perror("recvnum");
  exit(1);
 }
 printf("receive %s\n", buf);
 close(sockfd);
 return 0;
}

 

client.c

#include<stdio.h>
#include<string.h>

#include<stdlib.h>
#include<sys/types.h>
#include<errno.h>
#include<netdb.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#define PORT 8848
int main(int argc, char **argv)
{
 int sockfd, sendnum, recvnum, sin_size=1;
 struct sockaddr_in serveraddr;
 char rbuf[100];
 struct hostent *hptr;
 hptr = gethostbyname(argv[1]);
 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
 serveraddr.sin_family = AF_INET;
 serveraddr.sin_port = htons(PORT);
 serveraddr.sin_addr = *((struct in_addr*)hptr->h_addr);
 bzero(&(serveraddr.sin_zero), 8);
 
 sendnum = sendto(sockfd, "hello server, i am your client\n", 50, 0, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr));
 if(sendnum == -1)
 {
  perror("sendto");
  exit(1);
 }
 printf("send ok\n");

 recvnum = recvfrom(sockfd, rbuf, 100, 0, (struct sockaddr*)&serveraddr, &sin_size);
 close(sockfd);
 return 0;
 
}

 

server.c中的注释部分取消注释后就出现如标题的错误,但是在client.c中可以运行啊,怎么回事?纠结中。。。求解答

ITtecman的主页 ITtecman | 初学一级 | 园豆:187
提问于:2013-03-08 10:33
< >
分享
所有回答(1)
0

  首先说明一下什么是AF_INET:

  AF 表示ADDRESS FAMILY 地址族,PF 表示PROTOCOL FAMILY 协议族。在windows操作系统中这两个宏定义是一样的,使用哪个都没有关系。而在Unix/Linux系统中,在不同的版本中这两者有微小差别。对于BSD,是AF,对于POSIX是PF UNIX系统支持AF_INET,AF_UNIX,AF_NS等,而DOS,Windows中仅支持AF_INET,它是网际网区域。在函数socketpair与socket的domain参数中有AF_UNIX,AF_LOCAL,AF_INET,PF_UNIX,PF_LOCAL,PF_INET.这几个参数有AF_UNIX=AF_LOCAL, PF_UNIX=PF_LOCAL, AF_LOCAL=PF_LOCAL, AF_INET=PF_INET.

  建议:对于socketpair与socket的domain参数,使用PF_LOCAL系列,而在初始化套接口地址结构时,则使用AF_LOCAL.
例如: z = socket(PF_LOCAL, SOCK_STREAM, 0); adr_unix.sin_family = AF_LOCAL;
岬淢箫声 | 园豆:190 (初学一级) | 2013-03-08 11:02

部分文字有链接,希望这些链接对你有帮助。

支持(0) 反对(0) 岬淢箫声 | 园豆:190 (初学一级) | 2013-03-08 11:04

多谢你的回答。不过我按照你说的修改后还是出现同样的错误。另外,sendto函数在client.c中可以正确运行不会出现上述错误,只是在server.c中出现这个错误

支持(0) 反对(0) ITtecman | 园豆:187 (初学一级) | 2013-03-08 11:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册