Chinaunix首页 | 论坛 | 博客
  • 博客访问: 222741
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 412
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-24 10:27
个人简介

Continuous pursuit technical details

文章分类

全部博文(48)

文章存档

2014年(1)

2013年(47)

分类: C/C++

2013-12-10 15:10:20

#include
#include
#include
#include
#include
#include


#define BUFLEN 512
#define NPACK 10
#define PORT 9930


void diep(char *s)
{
perror(s);
exit(1);
}


int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];


if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");


 memset((char *) &si_me, 0, sizeof(si_me));
 si_me.sin_family = AF_INET;
 si_me.sin_port = htons(PORT);
 si_me.sin_addr.s_addr = htonl(INADDR_ANY);
 if (bind(s, &si_me, sizeof(si_me))==-1)
 diep("bind");


  for (i=0; i   if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
  diep("recvfrom()");
  printf("Received packet from %s:%d\nData: %s\n\n",
  inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
  }


   close(s);
   return 0;
}

阅读(1371) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~