Chinaunix首页 | 论坛 | 博客
  • 博客访问: 450194
  • 博文数量: 133
  • 博客积分: 3259
  • 博客等级: 中校
  • 技术积分: 1255
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-14 13:49
文章存档

2012年(6)

2011年(112)

2010年(16)

分类: LINUX

2011-06-02 15:12:34

tcpserver.c 01 /** 02 * Tcp Server program, It is a simple example only. 03 * zhengsh 200520602061 2 04 * when client connect to server, send a welcome message and timestamp in server. 05 */ 06 07 #include 08 #include 09 #include 10 #include 11 #include in.h> 12 #include 13 #include 14 15 #define SERVER_PORT 20000 // define the defualt connect port id 16 #define LENGTH_OF_LISTEN_QUEUE 10 // length of listen queue in server 17 #define BUFFER_SIZE 255 18 #define WELCOME_MESSAGE "welcome to connect the server. " 19 20 int main(int argc, char** argv) 21 { 22 int servfd,clifd; 23 struct sockaddr_in servaddr,cliaddr; 24 25 if ((servfd = socket(AF_INET,SOCK_STREAM, 0 )) < 0 ) 26 { 27 printf( " create socket error!\n " ); 28 exit( 1 ); 29 } 30 31 bzero( & servaddr, sizeof (servaddr)); 32 33 servaddr.sin_family = AF_INET; 34 servaddr.sin_port = htons(SERVER_PORT); 35 servaddr.sin_addr.s_addr = htons(INADDR_ANY); 36 37 if (bind(servfd,( struct sockaddr * ) & servaddr, sizeof (servaddr)) < 0 ) 38 { 39 printf( " bind to port %d failure!\n " ,SERVER_PORT); 40 exit( 1 ); 41 } 42 43 if (listen(servfd,LENGTH_OF_LISTEN_QUEUE) < 0 ) 44 { 45 printf( " call listen failure!\n " ); 46 exit( 1 ); 47 } 48 49 while ( 1 ) 50 { // server loop will nerver exit unless any body kill the process 51 52 char buf[BUFFER_SIZE]; 53 long timestamp; 54 socklen_t length = sizeof (cliaddr); 55 clifd = accept(servfd,( struct sockaddr * ) & cliaddr, & length); 56 57 if (clifd < 0 ) 58 { 59 printf( " error comes when call accept!\n " ); 60 break ; 61 } 62 63 strcpy(buf,WELCOME_MESSAGE); 64 65 // inet_ntop(INET_ADDRSTRLEN,cliaddr.sin_addr,buf,BUFFER_SIZE); 66 67 printf( " from client,IP:%s,Port:%d\n " , 68 inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port)); 69 70 timestamp = time(NULL); 71 72 strcat(buf, " timestamp in server: " ); 73 strcat(buf,ctime( & timestamp)); 74 75 send(clifd,buf,BUFFER_SIZE, 0 ); 76 77 close(clifd); 78 79 } // exit 80 81 close(servfd); 82 83 return 0 ; 84 } [代码] tcpclient.c 01 /** 02 * Tcp client program, It is a simple example only. 03 * zhengsh 200520602061 2 04 * connect to server, and echo a message from server. 05 */ 06 07 08 #include 09 #include 10 #include 11 #include 12 #include in.h> 13 #include 14 15 16 #define SERVER_PORT 20000 // define the defualt connect port id 17 #define CLIENT_PORT ((20001+rand())%65536) // define the defualt client port as a random port 18 #define BUFFER_SIZE 255 19 #define REUQEST_MESSAGE "welcome to connect the server.\n" 20 21 22 void usage(char* name) 23 { 24 printf( " usage: %s IpAddr\n " ,name); 25 } 26 27 28 int main(int argc, char** argv) 29 { 30 int servfd,clifd,length = 0; 31 struct sockaddr_in servaddr,cliaddr; 32 socklen_t socklen = sizeof (servaddr); 33 char buf[BUFFER_SIZE]; 34 35 if (argc < 2 ) 36 { 37 usage(argv[ 0 ]); 38 exit( 1 ); 39 } 40 41 if ((clifd = socket(AF_INET,SOCK_STREAM, 0 )) < 0 ) 42 { 43 printf( " create socket error!\n " ); 44 exit( 1 ); 45 } 46 47 srand(time(NULL)); // initialize random generator 48 49 bzero( & cliaddr, sizeof (cliaddr)); 50 cliaddr.sin_family = AF_INET; 51 cliaddr.sin_port = htons(CLIENT_PORT); 52 cliaddr.sin_addr.s_addr = htons(INADDR_ANY); 53 54 bzero( & servaddr, sizeof (servaddr)); 55 servaddr.sin_family = AF_INET; 56 inet_aton(argv[ 1 ], & servaddr.sin_addr); 57 servaddr.sin_port = htons(SERVER_PORT); 58 // servaddr.sin_addr.s_addr = htons(INADDR_ANY); 59 60 if (bind(clifd, (struct sockaddr* ) &cliaddr, sizeof (cliaddr)) < 0 ) 61 { 62 printf( " bind to port %d failure!\n " ,CLIENT_PORT); 63 exit( 1 ); 64 } 65 66 if (connect(clifd,( struct sockaddr * ) & servaddr, socklen) < 0 ) 67 { 68 printf( " can't connect to %s!\n ", argv[ 1 ]); 69 exit( 1 ); 70 } 71 72 length = recv(clifd, buf, BUFFER_SIZE, 0); 73 if (length < 0) 74 { 75 printf( " error comes when recieve data from server %s! ", argv[1] ); 76 exit( 1 ); 77 } 78 79 printf( " from server %s :\n\t%s", argv[1], buf); 80 81 close(clifd); 82 return 0;
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
阅读(654) | 评论(0) | 转发(0) |
0

上一篇:多线编程例子

下一篇:MCP是mult-chips-packge

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