-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
56 lines (55 loc) · 1.08 KB
/
client.c
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
#include <func.h>
int main(int argc,char *argv[])
{
args_check(argc,3);
int socketfd=socket(AF_INET,SOCK_STREAM,0);
if(-1==socketfd)
{
perror("socket");
return -1;
}
printf("socketfd=%d\n",socketfd);
struct sockaddr_in ser;
memset(&ser,0,sizeof(ser));
ser.sin_family=AF_INET;
ser.sin_port=htons(atoi(argv[2]));
ser.sin_addr.s_addr=inet_addr(argv[1]);
int ret;
char buf[128];
ret=connect(socketfd,(struct sockaddr *)&ser,sizeof(struct sockaddr));
if(-1==ret)
{
perror("connect");
return -1;
}
fd_set rdset;
while(1)
{
FD_ZERO(&rdset);
FD_SET(STDIN_FILENO,&rdset);
FD_SET(socketfd,&rdset);
ret=select(socketfd+1,&rdset,NULL,NULL,NULL);
if(ret>0)
{
if(FD_ISSET(STDIN_FILENO,&rdset))
{
memset(buf,0,sizeof(buf));
read(STDIN_FILENO,buf,sizeof(buf)-1);
send(socketfd,buf,strlen(buf),0);
}
if(FD_ISSET(socketfd,&rdset))
{
memset(buf,0,sizeof(buf));
ret=recv(socketfd,buf,sizeof(buf)-1,0);
if(0==ret)
{
printf("client close,byebye\n");
break;
}
printf("%s\n",buf);
}
}
}
close(socketfd);
return 0;
}