-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
78 lines (66 loc) · 1.43 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
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <arpa/inet.h> // socket 相关 ...
#include <unistd.h> // close()
#include <thread>
#include <string>
#include <cstring>
using namespace std;
int sockfd;
struct sockaddr_in serv_addr;
void SendMsg(const string &msg) {
if(-1 == send(sockfd, msg.c_str(), msg.size(), 0))
cout<<"Failed to send"<<endl;
}
void RMsg(){
int len;
char recv_buf[1024];
while (1) {
memset(recv_buf, 0, sizeof(recv_buf));
len = recv(sockfd, recv_buf, sizeof(recv_buf), 0);
if (-1 == len) {
cout<<"Failed to recv"<<endl;
}
if(0 == len){
close(sockfd);
cout<<"与服务器断开链接!"<<endl;
break;
}
cout<<"Receive message: "<<recv_buf<<endl;
}
}
int main()
{
string ip = "127.0.0.1";
int port;
cout<<"please input server port:";
cin>>port;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd) {
cout<<"Failed to sockfd"<<endl;
return -1;
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = inet_addr(ip.c_str());
int stat = connect(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr));
if (-1 == stat) {
cout<<"Failed to connect"<<endl;
return -1;
}
std::thread(RMsg).detach();
while(1)
{
string send_msg;
cin>>send_msg;
if (send_msg == "q")
{
cout<<"结束客户端!"<<endl;
break;
}
else
SendMsg(send_msg);
}
close(sockfd);
return 0;
}