在Linux下写了个小的socket程序,分为客户端和服务器端,服务端开一个端口(2000),做为一个daemon,等待客户的连接请求.一旦有客
户连接,服务器端打印出客户端的IP地址和端口,并且向服务器端发送欢迎信息和时间.下面是服务端的代码(tcpserver.c).由于这只是个简单的
程序,所以只用了单线程实现!
[代码] 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 <stdio.h>
-
08 #include <sys/socket.h>
-
09 #include <unistd.h>
-
10 #include <sys/types.h>
-
11 #include <netinet/in.h>
-
12 #include <stdlib.h>
-
13 #include <time.h>
-
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 <stdio.h>
-
09 #include <sys/socket.h>
-
10 #include <unistd.h>
-
11 #include <sys/types.h>
-
12 #include <netinet/in.h>
-
13 #include <stdlib.h>
-
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;
-
83 }
阅读(1206) | 评论(0) | 转发(0) |