Chinaunix首页 | 论坛 | 博客
  • 博客访问: 82561
  • 博文数量: 25
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 98
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-10 00:42
文章分类

全部博文(25)

分类: C/C++

2017-09-01 12:34:02

服务器:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <arpa/inet.h>
  7. #include <netdb.h>
  8. #include <unistd.h>

  9. int port = 6789;

  10. int main(int argc, char *agrv[])
  11. {
  12.     int fdSock;
  13.     fdSock = socket(AF_INET, SOCK_DGRAM, 0);

  14.     struct sockaddr_in address;
  15.     bzero(&address, sizeof(address));
  16.     address.sin_family = AF_INET;
  17.     address.sin_port = htons(port);
  18.     address.sin_addr.s_addr = htonl(INADDR_ANY); //inet_addr("127.0.0.1");

  19.     char buf[80];
  20.     sprintf(buf, "Are you OK ?");

  21.     while(1)
  22.     {
  23.         sendto(fdSock, buf, sizeof(buf), 0, (struct sockaddr*)&address, sizeof(address));
  24.         sleep(3);
  25.         printf("send: %s\n", buf);
  26.     }

  27.     return 0;
  28. }

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