当前位置: 技术问答>linux和unix
socket编程大牛,帮我看看这个错误,bind失败
来源: 互联网 发布时间:2016-12-31
本文导语: #include #include #include #include #include #include #include #include 程序是这样的: #define MYPORT 8634 int main() { int sockfd, new_fd; struct sockaddr_in my_addr; struct sockaddr_in their_addr; int sin_size; char *addr = "10.244....
#include
#include
#include
#include
#include
#include
#include
#include
程序是这样的:
#define MYPORT 8634
int main()
{
int sockfd, new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
char *addr = "10.244.6.186";
int ret;
unsigned long addr_data;
addr_data = inet_addr(addr);
printf("the address is 0x%xn", addr_data);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = addr_data;
errno=0;
if( (ret = bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))) == -1 )
printf("bind failed! strerror(%d)=%sn", errno, strerror(errno));
return 0;
}
打印信息如下:
the address is 0xaf406ba
bind failed! strerror(68)=Can't assign requested address
这是为什么啊?我的程序有什么问题吗?
#include
#include
#include
#include
#include
#include
#include
程序是这样的:
#define MYPORT 8634
int main()
{
int sockfd, new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
char *addr = "10.244.6.186";
int ret;
unsigned long addr_data;
addr_data = inet_addr(addr);
printf("the address is 0x%xn", addr_data);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = addr_data;
errno=0;
if( (ret = bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))) == -1 )
printf("bind failed! strerror(%d)=%sn", errno, strerror(errno));
return 0;
}
打印信息如下:
the address is 0xaf406ba
bind failed! strerror(68)=Can't assign requested address
这是为什么啊?我的程序有什么问题吗?
|
使用my_addr之前,先清0
memset(&my_addr, 0, sizeof(struct sockaddr_in));
memset(&my_addr, 0, sizeof(struct sockaddr_in));