Raj*_*u V 8 c sockets linux udp
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main()
{
struct sockaddr_in addr;
int fd, cnt,ret;
char ch = 'y',msg[] ="How are you";
if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
printf("Error: socket");
exit(1);
}
printf("\nDone socket\n");
/* set up destination address */
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr("128.88.143.113");
addr.sin_port=htons(9090);
ret=connect(fd,(struct sockaddr *)&addr,sizeof(addr));
perror("Connect:");
while(ch == 'y'){
cnt = send(fd,msg,sizeof(msg),0);
if(cnt < 0)
perror("send:");
printf("\nNumber of bytes sent = %d , \n",cnt);
printf("Continue (y/n)\n");
scanf(" %c",&ch);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码编译为在Linux机器上运行.
假设上面的代码将数据发送到IP地址的机器128.88.143.113.没有UDP套接字绑定到端口9090的128.88.143.113.
在while循环中,第一次调用send()成功(数据包实际上在线上;使用它检查trace),第二次调用send()失败Connection refused.third send()成功和第四次失败等等.
我怀疑在第一次send()堆栈收到ICMP错误消息(在tcpdumpLinux机器上看到)后保存在套接字结构中.第二个send()在看到此错误时失败,并且实际上没有发送任何数据包.第二个send()也清除套接字结构中的错误.因此,第三个send()成功,第四个成功,等等.
问题:
send()成功吗?