-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcptool.cpp
More file actions
45 lines (43 loc) · 1.11 KB
/
Tcptool.cpp
File metadata and controls
45 lines (43 loc) · 1.11 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
#include "Tcptool.h"
/*
* @Description: 从socket句柄中读取数据
* @Param: 无
* @return: 返回从socket文件描述符中读取的字符个数
* @data: 4/5/21
* @author: Bin Yin
* @version:
*/
int Tcptool::read() {
char buffer[1024];
memset(buffer,0,sizeof(buffer));
int ret=recv(_fd,buffer, sizeof(buffer),0);
if (ret==0)
std::cout << "断开连接 !" <<std::endl;
else if (ret>0)
std::cout << "read from socket :" << buffer <<std::endl;
else if (ret<0){
std::cout << "recv error " << std::endl;
// std::cout<<stderr<<std::endl;
}
return ret;
}
/*
* @Description: 通过socket句柄发送数据
* @Param: 无
* @return: 写入缓冲区的字符个数
* @data: 4/5/21
* @author: Bin Yin
* @version:
*/
int Tcptool::write() {
char buffer[1024];
memset(buffer,0,sizeof(buffer));
const char * str="This is yb server";
memcpy(buffer,str, sizeof(str));
int ret=send(_fd,buffer,sizeof(buffer),0);
if (ret>0)
std::cout<<"发送成功 "<<std::endl;
else if(ret==-1)
std::cout<<"发送失败" <<std::endl;
return ret;
}