Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1550367
  • 博文数量: 596
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 173
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-06 15:50
个人简介

在线笔记

文章分类

全部博文(596)

文章存档

2016年(1)

2015年(104)

2014年(228)

2013年(226)

2012年(26)

2011年(11)

分类: Android平台

2014-12-25 13:51:43


  1. //server.c
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<unistd.h>
  5. #include<sys/types.h>
  6. #include<stdlib.h>
  7. #if 1
  8. #include<sys/socket.h>
  9. #include<netinet/in.h>
  10. #include<arpa/inet.h>
  11. #define CLDUDTSOCKET int
  12. #define udt_socket socket
  13. #define udt_accept accept
  14. #define udt_getlasterror_desc() "not impl"
  15. #define udt_bind bind
  16. #define udt_listen listen
  17. #define udt_send send
  18. #define udt_recv recv
  19. #define udt_close close
  20. #else
  21. #define CLD_SUPPORT_UDT
  22. #include "cld_udt.h"
  23. #endif
  24.  
  25. #define PORT 5701
  26. #define MAXDATASIZE 100
  27.  
  28. int main()
  29. {
  30.     CLDUDTSOCKET sockfd;
  31.     struct sockaddr_in server;
  32.     struct sockaddr_in client;
  33.     socklen_t addrlen;
  34.     int num;
  35.     char buf[MAXDATASIZE];
  36.  
  37.     if((sockfd = udt_socket(AF_INET, SOCK_STREAM, 0)) == -1)
  38.     {
  39.        perror("Creatingsocket failed.");
  40.        exit(1);
  41.     }
  42.  
  43.     bzero(&server,sizeof(server));
  44.     server.sin_family=AF_INET;
  45.     server.sin_port=htons(PORT);
  46.     server.sin_addr.s_addr= htonl (INADDR_ANY);
  47.     if(udt_bind(sockfd, (struct sockaddr *)&server, sizeof(server)) == -1)
  48.     {
  49.        perror("Bind()error.");
  50.        exit(1);
  51.     }

  52.     if (udt_listen(sockfd, 2)< 0)
  53.     {
  54.        perror("listen()error.");
  55.         exit(1);
  56.     }
  57.     while(1)
  58.     {
  59.         struct sockaddr client;
  60.         int len = sizeof(client);
  61.         CLDUDTSOCKET so = udt_accept(sockfd, &client, &len);
  62.        num =udt_recv(so, buf, MAXDATASIZE,0);
  63.  
  64.        if (num < 0)
  65.        {
  66.            printf("udt_recv fail, %s\n", udt_getlasterror_desc());
  67.           exit(1);
  68.        }
  69.  
  70.        buf[num] = '\0';
  71.        printf("You got a message (%s) from client.\n", buf);
  72.        udt_send(so,"Welcometo my server.\n",22,0);
  73.         udt_close(so);
  74.     }
  75.     udt_close(sockfd);
  76. }

  1. //client.c
  2. //执行命令./ udpclient 127.0.0.1 hello

  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <netdb.h>
  9. #if 1
  10. #include<sys/socket.h>
  11. #include<netinet/in.h>
  12. #include<arpa/inet.h>

  13. #define CLDUDTSOCKET int
  14. #define udt_socket socket
  15. #define udt_connect connect
  16. #define udt_accept accept
  17. #define udt_bind bind
  18. #define udt_listen listen
  19. #define udt_send send
  20. #define udt_recv recv
  21. #define udt_close close
  22. #else
  23. #define CLD_SUPPORT_UDT
  24. #include "cld_udt.h"
  25. #endif
  26.  
  27. #define PORT 5701
  28. #define MAXDATASIZE 100
  29.  
  30. int main(int argc, char *argv[])
  31. {
  32.     CLDUDTSOCKET sockfd, num;
  33.     char buf[MAXDATASIZE];
  34.  
  35.     struct hostent *he;
  36.     struct sockaddr_in server,peer;
  37.  
  38.     if (argc !=3)
  39.     {
  40.        printf("Usage: %s \n",argv[0]);
  41.        exit(1);
  42.     }
  43.  
  44.     if ((sockfd=udt_socket(AF_INET, SOCK_STREAM,0))==-1)
  45.     {
  46.        printf("socket() error\n");
  47.        exit(1);
  48.     }
  49.  
  50.     bzero(&server,sizeof(server));
  51.     server.sin_family = AF_INET;
  52.     server.sin_port = htons(PORT);
  53.     server.sin_addr.s_addr= inet_addr(argv[1]);

  54.     if (udt_connect(sockfd, (struct sockaddr*)&server, sizeof(server)) < 0)
  55.     {
  56.        printf("udt_connect() error\n");
  57.        exit(1);
  58.     }

  59.     udt_send(sockfd, argv[2],strlen(argv[2]),0);
  60.     while (1)
  61.     {
  62.         num=udt_recv(sockfd,buf,MAXDATASIZE,0);
  63.        if(num <=0)
  64.        {
  65.           printf("udt_recv() error\n");
  66.           exit(1);
  67.        }
  68.  
  69.        printf("Server Message:%s\n",buf);
  70.        break;
  71.     }
  72.  
  73.     udt_close(sockfd);
  74. }


  1. Makefile

  2. sc:
  3.     gcc 1.c -o s
  4.     gcc c2.c -o c
  5. all:
  6.     gcc 1.c ./libudt.so -pthread -o s
  7.     gcc c2.c -o c ./libudt.so -pthread



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