#include
#include
#include
#include
#include
#include
#include
#include
#define N 64
typedef struct sockaddr SA;
int main(int argc, char *argv[])
{
int sockfd;
char buf[N];
struct sockaddr_in servaddr;
struct timeval mytime;
if (argc < 3)
{
printf("Usage : %s \n", argv[0]);
exit(-1);
}
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("fail to socket");
exit(-1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = PF_INET;
servaddr.sin_port = htons(atoi(argv[2]));
servaddr.sin_addr.s_addr = inet_addr(argv[1]);
mytime.tv_sec = 3;
mytime.tv_usec = 0;
//设定超时时间
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &mytime, sizeof(mytime)) < 0)
{
perror("fail to setsockopt");
exit(-1);
}
sendto(sockfd, buf, N, 0, (SA *)&servaddr, sizeof(servaddr));
if (recvfrom(sockfd, buf, N, 0, NULL, NULL) < 0)
{
perror("fail to recvfrom");
}
return 0;
}
阅读(1211) | 评论(0) | 转发(0) |