-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathclient.cpp
62 lines (49 loc) · 1.42 KB
/
client.cpp
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 <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
int sockfd = -1;
sockfd = socket( AF_INET, SOCK_STREAM, 0 );
if ( -1 == sockfd )
{
perror( "sock created" );
exit( -1 );
}
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
if(inet_aton("127.0.0.1",&server_addr.sin_addr) == 0) //服务器的IP地址来自程序的参数
{
printf("Server IP Address Error!\n");
exit(1);
}
server_addr.sin_port = htons(9877);
socklen_t server_addr_length = sizeof(server_addr);
//向服务器发起连接,连接成功后client_socket代表了客户机和服务器的一个socket连接
if(connect(sockfd,(struct sockaddr*)&server_addr, server_addr_length) < 0)
{
printf("Can Not Connect!\n");
exit(1);
}
printf("connect successfull\n");
char recvBuf[1024] = { 0 };
string request;
while( getline(cin,request) )
{
write( sockfd, request.c_str(), request.size() );
read( sockfd, recvBuf, sizeof( recvBuf ) );
fputs( recvBuf, stdout );
printf("\n");
memset( recvBuf, 0, sizeof( recvBuf ) );
}
close( sockfd );
return 0;
}