-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.c
More file actions
62 lines (61 loc) · 1.42 KB
/
Client.c
File metadata and controls
62 lines (61 loc) · 1.42 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
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/select.h>
#include<sys/time.h>
#include<unistd.h>
int main(){
int cfd = socket(AF_INET,SOCK_STREAM,0);
if(cfd == -1){
perror("socket");
exit(2);
}
struct sockaddr_in add;
add.sin_family = AF_INET;
add.sin_port = htons(8080);
add.sin_addr.s_addr = htonl(INADDR_ANY);
int r = connect(cfd,(struct sockaddr*)&add,sizeof(add));
if(r == -1){
perror("connect");
exit(1);
}
printf("connect success\n");
int kfd = fileno(stdin);
int maxfd = kfd>cfd?kfd:cfd;
fd_set set;
while(1){
FD_ZERO(&set);
FD_SET(cfd,&set);
FD_SET(kfd,&set);
int r = select(maxfd+1,&set,NULL,NULL,NULL);
if(r < 0){
perror("select");
exit(3);
}
char buf[1024];
if(FD_ISSET(cfd,&set)){
r = read(cfd, buf,1024);
if(r = 0){
close(cfd);
break;
}
printf("%s",buf);
fflush(stdout);
}
if(FD_ISSET(kfd,&set)){
int r = read(0,buf,sizeof(buf));
if(r < 0){
break;
}
write(cfd,buf,strlen(buf));
}
}
return 0;
}