回射程序:服务端将客户端发送的数据输出并返回给客户端
客户端
1 #include "net.h"
2
3 int main(int argc, char **argv)
4 {
5 int sockfd, n;
6 char sendline[MAXLINE], recvline[MAXLINE + 1];
7 struct sockaddr_in servaddr;
8
9 if (argc != 2)
10 {
11 printf("Error arg!
");
12 exit(1);
13 }
14
15 if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
16 {
17 printf("Error socket!
");
18 exit(1);
19 }
20
21 bzero(&servaddr, sizeof(servaddr));
22 servaddr.sin_family = AF_INET;
23 servaddr.sin_port = htons(SERV_PORT);
24
25 if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
26 {
27 printf("Error inet_pton!
");
28 exit(1);
29 }
30
31 if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
32 {
33 printf("Error connect!
");
34 exit(1);
35 }
36
37 printf("Success init!
");
38
39 while (fgets(sendline, MAXLINE, stdin) != NULL)
40 {
41 printf("Success fget from stdin!
");
42
43 if (write(sockfd, sendline, strlen(sendline)) < 0)
44 {
45 printf("Error write!
");
46 exit(1);
47 }
48
49 if ( (n = read(sockfd, recvline, MAXLINE)) > 0 )
50 {
51 recvline[n] = '