Repository navigation
Expand file tree
/
Copy pathclient.c
More file actions
161 lines (140 loc) · 3.36 KB
/
Copy pathclient.c
File metadata and controls
161 lines (140 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#define PORT 8888
#define MAX_LINE 2048
int max(int a , int b)
{
return a > b ? a : b;
}
/*readline函数实现*/
ssize_t readline(int fd, char *vptr, size_t maxlen)
{
ssize_t n, rc;
char c, *ptr;
ptr = vptr;
for (n = 1; n < maxlen; n++) {
if ( (rc = read(fd, &c,1)) == 1) {
*ptr++ = c;
if (c == '\n')
break; /* newline is stored, like fgets() */
} else if (rc == 0) {
*ptr = 0;
return(n - 1); /* EOF, n - 1 bytes were read */
} else
return(-1); /* error, errno set by read() */
}
*ptr = 0; /* null terminate like fgets() */
return(n);
}
/*普通客户端消息处理函数*/
void str_cli(int sockfd)
{
/*发送和接收缓冲区*/
char sendline[MAX_LINE] , recvline[MAX_LINE];
while(fgets(sendline , MAX_LINE , stdin) != NULL)
{
write(sockfd , sendline , strlen(sendline));
bzero(recvline , MAX_LINE);
if(readline(sockfd , recvline , MAX_LINE) == 0)
{
perror("server terminated prematurely");
exit(1);
}//if
if(fputs(recvline , stdout) == EOF)
{
perror("fputs error");
exit(1);
}//if
bzero(sendline , MAX_LINE);
}//while
}
/*采用select的客户端消息处理函数*/
void str_cli2(FILE* fp , int sockfd)
{
int maxfd;
fd_set rset;
/*发送和接收缓冲区*/
char sendline[MAX_LINE] , recvline[MAX_LINE];
FD_ZERO(&rset);
while(1)
{
/*将文件描述符和套接字描述符添加到rset描述符集*/
FD_SET(fileno(fp) , &rset);
FD_SET(sockfd , &rset);
maxfd = max(fileno(fp) , sockfd) + 1;
select(maxfd , &rset , NULL , NULL , NULL);
if(FD_ISSET(fileno(fp) , &rset))
{
if(fgets(sendline , MAX_LINE , fp) == NULL)
{
printf("read nothing~\n");
close(sockfd); /*all done*/
return ;
}//if
sendline[strlen(sendline) - 1] = '\0';
write(sockfd , sendline , strlen(sendline));
}//if
if(FD_ISSET(sockfd , &rset))
{
if(readline(sockfd , recvline , MAX_LINE) == 0)
{
perror("handleMsg: server terminated prematurely.\n");
exit(1);
}//if
if(fputs(recvline , stdout) == EOF)
{
perror("fputs error");
exit(1);
}//if
}//if
}//while
}
int main(int argc , char **argv)
{
/*声明套接字和链接服务器地址*/
int sockfd;
struct sockaddr_in servaddr;
/*判断是否为合法输入*/
if(argc != 2)
{
perror("usage:tcpcli <IPaddress>");
exit(1);
}//if
/*(1) 创建套接字*/
if((sockfd = socket(AF_INET , SOCK_STREAM , 0)) == -1)
{
perror("socket error");
exit(1);
}//if
/*(2) 设置链接服务器地址结构*/
bzero(&servaddr , sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
if(inet_pton(AF_INET , argv[1] , &servaddr.sin_addr) < 0)
{
printf("inet_pton error for %s\n",argv[1]);
exit(1);
}//if
/*(3) 发送链接服务器请求*/
if(connect(sockfd , (struct sockaddr *)&servaddr , sizeof(servaddr)) < 0)
{
perror("connect error");
exit(1);
}//if
/*调用普通消息处理函数*/
str_cli(sockfd);
/*调用采用select技术的消息处理函数*/
//str_cli2(stdin , sockfd);
exit(0);
}