Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1320553
  • 博文数量: 175
  • 博客积分: 2743
  • 博客等级: 少校
  • 技术积分: 4024
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-30 01:41
文章分类

全部博文(175)

文章存档

2015年(1)

2013年(53)

2012年(71)

2011年(50)

分类: LINUX

2011-11-22 16:50:49

在Linux下写了个小的socket程序,分为客户端和服务器端,服务端开一个端口(2000),做为一个daemon,等待客户的连接请求.一旦有客 户连接,服务器端打印出客户端的IP地址和端口,并且向服务器端发送欢迎信息和时间.下面是服务端的代码(tcpserver.c).由于这只是个简单的 程序,所以只用了单线程实现!

[代码] tcpserver.c
  1. 01    /**
  2. 02     * Tcp Server program, It is a simple example only.
  3. 03     * zhengsh 200520602061 2
  4. 04     * when client connect to server, send a welcome message and timestamp in server.
  5. 05     */
  6. 06    
  7. 07    #include <stdio.h>
  8. 08    #include <sys/socket.h>
  9. 09    #include <unistd.h>
  10. 10    #include <sys/types.h>
  11. 11    #include <netinet/in.h>
  12. 12    #include <stdlib.h>
  13. 13    #include <time.h>
  14. 14    
  15. 15    #define SERVER_PORT 20000 // define the defualt connect port id
  16. 16    #define LENGTH_OF_LISTEN_QUEUE 10 // length of listen queue in server
  17. 17    #define BUFFER_SIZE 255
  18. 18    #define WELCOME_MESSAGE "welcome to connect the server. "
  19. 19    
  20. 20    int main(int argc, char** argv)
  21. 21    {
  22. 22     int servfd,clifd;
  23. 23     struct sockaddr_in servaddr,cliaddr;
  24. 24    
  25. 25     if ((servfd = socket(AF_INET,SOCK_STREAM, 0 )) < 0 )
  26. 26     {
  27. 27     printf( " create socket error!\n " );
  28. 28     exit( 1 );
  29. 29     }
  30. 30    
  31. 31     bzero( & servaddr, sizeof (servaddr));
  32. 32    
  33. 33     servaddr.sin_family = AF_INET;
  34. 34     servaddr.sin_port = htons(SERVER_PORT);
  35. 35     servaddr.sin_addr.s_addr = htons(INADDR_ANY);
  36. 36    
  37. 37     if (bind(servfd,( struct sockaddr * ) & servaddr, sizeof (servaddr)) < 0 )
  38. 38     {
  39. 39     printf( " bind to port %d failure!\n " ,SERVER_PORT);
  40. 40     exit( 1 );
  41. 41     }
  42. 42    
  43. 43     if (listen(servfd,LENGTH_OF_LISTEN_QUEUE) < 0 )
  44. 44     {
  45. 45     printf( " call listen failure!\n " );
  46. 46     exit( 1 );
  47. 47     }
  48. 48    
  49. 49     while ( 1 )
  50. 50     { // server loop will nerver exit unless any body kill the process
  51. 51    
  52. 52     char buf[BUFFER_SIZE];
  53. 53     long timestamp;
  54. 54     socklen_t length = sizeof (cliaddr);
  55. 55     clifd = accept(servfd,( struct sockaddr * ) & cliaddr, & length);
  56. 56    
  57. 57     if (clifd < 0 )
  58. 58     {
  59. 59     printf( " error comes when call accept!\n " );
  60. 60     break ;
  61. 61     }
  62. 62    
  63. 63     strcpy(buf,WELCOME_MESSAGE);
  64. 64    
  65. 65     // inet_ntop(INET_ADDRSTRLEN,cliaddr.sin_addr,buf,BUFFER_SIZE);
  66. 66    
  67. 67     printf( " from client,IP:%s,Port:%d\n " ,
  68. 68     inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));
  69. 69    
  70. 70     timestamp = time(NULL);
  71. 71    
  72. 72     strcat(buf, " timestamp in server: " );
  73. 73     strcat(buf,ctime( & timestamp));
  74. 74    
  75. 75     send(clifd,buf,BUFFER_SIZE, 0 );
  76. 76    
  77. 77     close(clifd);
  78. 78    
  79. 79     } // exit
  80. 80    
  81. 81     close(servfd);
  82. 82    
  83. 83     return 0 ;
  84. 84    }

[代码] tcpclient.c
  1. 01    /**
  2. 02     * Tcp client program, It is a simple example only.
  3. 03     * zhengsh 200520602061 2
  4. 04     * connect to server, and echo a message from server.
  5. 05     */
  6. 06    
  7. 07    
  8. 08    #include <stdio.h>
  9. 09    #include <sys/socket.h>
  10. 10    #include <unistd.h>
  11. 11    #include <sys/types.h>
  12. 12    #include <netinet/in.h>
  13. 13    #include <stdlib.h>
  14. 14    
  15. 15    
  16. 16    #define SERVER_PORT 20000 // define the defualt connect port id
  17. 17    #define CLIENT_PORT ((20001+rand())%65536) // define the defualt client port as a random port
  18. 18    #define BUFFER_SIZE 255
  19. 19    #define REUQEST_MESSAGE "welcome to connect the server.\n"
  20. 20    
  21. 21    
  22. 22    void usage(char* name)
  23. 23    {
  24. 24     printf( " usage: %s IpAddr\n " ,name);
  25. 25    }
  26. 26    
  27. 27    
  28. 28    int main(int argc, char** argv)
  29. 29    {
  30. 30     int servfd,clifd,length = 0;
  31. 31     struct sockaddr_in servaddr,cliaddr;
  32. 32     socklen_t socklen = sizeof (servaddr);
  33. 33     char buf[BUFFER_SIZE];
  34. 34    
  35. 35     if (argc < 2 )
  36. 36     {
  37. 37     usage(argv[ 0 ]);
  38. 38     exit( 1 );
  39. 39     }
  40. 40    
  41. 41     if ((clifd = socket(AF_INET,SOCK_STREAM, 0 )) < 0 )
  42. 42     {
  43. 43     printf( " create socket error!\n " );
  44. 44     exit( 1 );
  45. 45     }
  46. 46    
  47. 47     srand(time(NULL)); // initialize random generator
  48. 48    
  49. 49     bzero( & cliaddr, sizeof (cliaddr));
  50. 50     cliaddr.sin_family = AF_INET;
  51. 51     cliaddr.sin_port = htons(CLIENT_PORT);
  52. 52     cliaddr.sin_addr.s_addr = htons(INADDR_ANY);
  53. 53    
  54. 54     bzero( & servaddr, sizeof (servaddr));
  55. 55     servaddr.sin_family = AF_INET;
  56. 56     inet_aton(argv[ 1 ], & servaddr.sin_addr);
  57. 57     servaddr.sin_port = htons(SERVER_PORT);
  58. 58     // servaddr.sin_addr.s_addr = htons(INADDR_ANY);
  59. 59    
  60. 60     if (bind(clifd, (struct sockaddr* ) &cliaddr, sizeof (cliaddr)) < 0 )
  61. 61     {
  62. 62     printf( " bind to port %d failure!\n " ,CLIENT_PORT);
  63. 63     exit( 1 );
  64. 64     }
  65. 65    
  66. 66     if (connect(clifd,( struct sockaddr * ) & servaddr, socklen) < 0 )
  67. 67     {
  68. 68     printf( " can't connect to %s!\n ", argv[ 1 ]);
  69. 69     exit( 1 );
  70. 70     }
  71. 71    
  72. 72     length = recv(clifd, buf, BUFFER_SIZE, 0);
  73. 73     if (length < 0)
  74. 74     {
  75. 75     printf( " error comes when recieve data from server %s! ", argv[1] );
  76. 76     exit( 1 );
  77. 77     }
  78. 78    
  79. 79     printf( " from server %s :\n\t%s", argv[1], buf);
  80. 80    
  81. 81     close(clifd);
  82. 82     return 0;
  83. 83    }


阅读(1206) | 评论(0) | 转发(0) |
0

上一篇:udev的实现原理

下一篇:linux socket 通信

给主人留下些什么吧!~~