-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.c
50 lines (40 loc) · 1.06 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
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#define PORT 9990
#define SIZE 1024
int main(int argc, char *argv[])
{
char *hostArgv = argv[1];
char *portArgv = argv[2];
int port = atoi(portArgv);
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
inet_aton(hostArgv, &(addr.sin_addr));
int server_socket = connect(client_socket, (struct sockaddr *)&addr, sizeof(addr)); //连接服务器
printf("Connect result ==> %d\n", server_socket);
if (server_socket < 0) {
perror("Connect result");
exit(1);
}
char buf[SIZE] = {0};
while (1)
{
printf("Send to server:");
scanf("%s", buf);
write(client_socket, buf, strlen(buf));
printf("\n");
if (strncmp(buf, "end", 3) == 0)
{
break;
}
}
close(server_socket);
return 0;
}