forked from linw7/Skill-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_select.cpp
More file actions
55 lines (53 loc) · 1.17 KB
/
client_select.cpp
File metadata and controls
55 lines (53 loc) · 1.17 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
#include <sys/time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/select.h>
#define DEFAULT_PORT 6666
int main() {
int connfd = 0;
int cLen = 0;
struct sockaddr_in client;
if(argc < 2){
printf("Usage : client [server ip address] \n");
return -1;
}
client.sin_family = AF_INET;
client.sin_port = htons(DEFAULT_PORT);
client.sin_addr.a_addr = inet_addr[argv[1]];
connfd = socket(AF_INET, SOCK_STREAM, 0);
if(connfd < 0){
perror("socket");
return -1;
}
if(connect(connfd, (struct sockaddr *)&client, sizeof(client)) < 0){
perror("connect");
return -1;
}
char buff[1024];
bzero(buff, sizeof(buff));
recv(connfd, buff, 1024, 0);
printf("recv : %s \n", buff);
bzero(buff, sizeof(buff));
strcpy(buff, "this is client \n");
send(connfd, buff, 1024, 0);
while(1){
bzero(buff, sizeof(buff));
scanf("%s", buff);
int p = strlen(buff);
buff[p] = '\0';
send(connfd, buff, 1024, 0);
printf("i have send buff \n");
}
close(fd);
return 0;
}