-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
61 lines (51 loc) · 1.38 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
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream>
#include <string.h>
#include "net/utils/log.h"
using namespace std;
#define SERVER_ADDRESS "127.0.0.1"
#define SERVER_PORT 3000
#define SEND_DATA "helloworld"
int startClient()
{
int clientFd=socket(AF_INET, SOCK_STREAM, 0);
if (clientFd == -1)
{
Logger::GetInstance()->error( "create client socket error.");
return -1;
}
sockaddr_in serveraddr;
serveraddr.sin_family=AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);
serveraddr.sin_port=htons(SERVER_PORT);
int ret=0;
ret=connect(clientFd, (sockaddr*)&serveraddr, sizeof(serveraddr));
if(ret==-1)
{
Logger::GetInstance()->error("connect socket error.");
return -1;
}
ret=send(clientFd, SEND_DATA,strlen(SEND_DATA),0);
if (ret != strlen(SEND_DATA))
{
std::cout << "send data error." << std::endl;
return -1;
}
std::cout << "send data successfully, data: " << SEND_DATA << std::endl;
char recvBuf[32]={0};
ret = recv(clientFd,recvBuf,sizeof(recvBuf),0);
if(ret>0)
{
std::cout << "recv data successfully, data: " << recvBuf << std::endl;
}
else
{
std::cout << "recv data error, data: " << recvBuf << std::endl;
}
close(clientFd);
return 0;
}