-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTCP_tool.h
More file actions
92 lines (79 loc) · 1.78 KB
/
TCP_tool.h
File metadata and controls
92 lines (79 loc) · 1.78 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef _TCP_TOOL_H_
#define _TCP_TOOL_H_
using namespace std;
namespace ftp_server
{
class TCP_tool
{
public:
TCP_tool()
{
};
~TCP_tool()
{
};
/** 设置socket为非阻塞 */
int set_nonblock(int fd)
{
int flags;
flags = fcntl(fd, F_GETFL);
if (flags < 0)
{
return flags;
}
flags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, flags) < 0)
{
return -1;
}
return 0;
};
int get_free_port(TCP_client *client)
{
int port = -1;
int fd = -1;
socklen_t len = 0;
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(0);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
fd = socket(AF_INET, SOCK_STREAM, 0);
// cout<<"get_free_port():"<<fd<<endl;
if(fd < 0)
{
cout<<"socket() error:"<<strerror(errno)<<endl;
return -1;
}
if(bind(fd, (struct sockaddr *)&sin, sizeof(sin)) != 0)
{
cout<<"bind() error:"<<strerror(errno)<<endl;
close(fd);
return -1;
}
// cout<<"get_free_port():"<<fd<<endl;
len = sizeof(sin);
if(getsockname(fd, (struct sockaddr *)&sin, &len) != 0)
{
cout<<"getsockname() error:"<<strerror(errno)<<endl;
close(fd);
return -1;
}
if (set_nonblock(fd) < 0)
{
LOG(WARN, "failed to set notify socket to non-blocking");
}
if(listen(fd,100) < 0)
{
LOG(WARN, "listen failed");
return -1;
}
// cout<<"get_free_port():"<<fd<<endl;
//cout<<"create listen_fd:"<<fd<<fsm_session -> file_name<<endl;
port = ntohs(sin.sin_port);
client -> set_listen_fd(fd);
return port;
};
};
}
#endif /* _TCP_TOOL_H_ */