当前位置: 技术问答>linux和unix
udp socket通讯,recvfrom()一直返回-1,perror提示"recvfrom: Bad address"
来源: 互联网 发布时间:2016-02-27
本文导语: 本人在设备中两个进程间socket通讯,udp方式,相同IP不同端口号.其中一个进程向另一个进程发送数据后,接收对方响应数据,recvfrom()一直返回-1,perror提示"recvfrom: Bad address",但是接收到的响应数据又是对的,不知道错在哪里?...
本人在设备中两个进程间socket通讯,udp方式,相同IP不同端口号.其中一个进程向另一个进程发送数据后,接收对方响应数据,recvfrom()一直返回-1,perror提示"recvfrom: Bad address",但是接收到的响应数据又是对的,不知道错在哪里?代码如下.另一个进程工作正常.
...
myTagType tag;//256 bytes
int tsresponse_sock;
struct sockaddr_in TSResponse;//local IP
struct sockaddr_in source_resp;//dest IP
int command_port = 3200; //udp control port;
int on = 1;
...
bzero(&TSResponse, sizeof(TSResponse));
TSResponse.sin_family = AF_INET;
TSResponse.sin_addr.s_addr = htonl(INADDR_ANY);
TSResponse.sin_port = htons(command_port+1);
tsresponse_sock = socket(AF_INET, SOCK_DGRAM, 0);
setsockopt(tsresponse_sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
bind(tsresponse_sock, (struct sockaddr *)&TSResponse, sizeof(struct sockaddr_in));
bzero(&source_resp, sizeof(source_resp));
source_resp.sin_family = AF_INET;
source_resp.sin_addr.s_addr = local_ipaddr;
source_resp.sin_port = htons(command_port);
....
sendto(tsresponse_sock, &tag, 20, 0, (struct sockaddr *)&source_resp, sizeof(struct sockaddr_in));
....
timeout.tv_sec=3;
ret = select(tsresponse_sock+1, &fdread, NULL,&fdexcept, &timeout);
....
else if (FD_ISSET(tsresponse_sock,&fdread)) {
{
bzero (&tag, sizeof (tag));
nsize = recvfrom(tsresponse_sock, &tag, sizeof(tag), 0, (struct sockaddr *)&source_resp, sizeof(struct sockaddr_in));
printf("recv nsize = %dn",nsize);
if(nsize == -1) perror("recvfrom");
....
}
...
...
myTagType tag;//256 bytes
int tsresponse_sock;
struct sockaddr_in TSResponse;//local IP
struct sockaddr_in source_resp;//dest IP
int command_port = 3200; //udp control port;
int on = 1;
...
bzero(&TSResponse, sizeof(TSResponse));
TSResponse.sin_family = AF_INET;
TSResponse.sin_addr.s_addr = htonl(INADDR_ANY);
TSResponse.sin_port = htons(command_port+1);
tsresponse_sock = socket(AF_INET, SOCK_DGRAM, 0);
setsockopt(tsresponse_sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
bind(tsresponse_sock, (struct sockaddr *)&TSResponse, sizeof(struct sockaddr_in));
bzero(&source_resp, sizeof(source_resp));
source_resp.sin_family = AF_INET;
source_resp.sin_addr.s_addr = local_ipaddr;
source_resp.sin_port = htons(command_port);
....
sendto(tsresponse_sock, &tag, 20, 0, (struct sockaddr *)&source_resp, sizeof(struct sockaddr_in));
....
timeout.tv_sec=3;
ret = select(tsresponse_sock+1, &fdread, NULL,&fdexcept, &timeout);
....
else if (FD_ISSET(tsresponse_sock,&fdread)) {
{
bzero (&tag, sizeof (tag));
nsize = recvfrom(tsresponse_sock, &tag, sizeof(tag), 0, (struct sockaddr *)&source_resp, sizeof(struct sockaddr_in));
printf("recv nsize = %dn",nsize);
if(nsize == -1) perror("recvfrom");
....
}
...
|
recvfrom最后一个参数是socklen_t *, 你用的是socklen_t
|
recvfrom最后一个参数是int *,而你是int
int addr_len =sizeof(struct sockaddr_in);
nsize = recvfrom(tsresponse_sock,&tag,sizeof(tag),0,(struct sockaddr*)&source_resp,&addr_len);
另外....2007-01的帖子怎么还没结贴~~~~不是有强制结贴机制么~~~
int addr_len =sizeof(struct sockaddr_in);
nsize = recvfrom(tsresponse_sock,&tag,sizeof(tag),0,(struct sockaddr*)&source_resp,&addr_len);
另外....2007-01的帖子怎么还没结贴~~~~不是有强制结贴机制么~~~