-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPSocket.cpp
More file actions
executable file
·116 lines (98 loc) · 2.55 KB
/
TCPSocket.cpp
File metadata and controls
executable file
·116 lines (98 loc) · 2.55 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "TCPSocket.hh"
using namespace std;
TCPSocket::TCPSocket()
{
this->socketfd = socket( AF_INET, SOCK_STREAM, 0);
if( this->socketfd < 0)
cout << "Fehler beim oeffnen des Sockets" << endl;
this->homeAddr = NULL;
this->remoteAddr = NULL;
}
TCPSocket::TCPSocket( int fd, const InetAddr* hAddr, const InetAddr* rAddr)
{
this->socketfd = fd;
this->homeAddr = new InetAddr( hAddr);
if( rAddr != NULL)
this->remoteAddr = new InetAddr( rAddr);
}
TCPSocket::~TCPSocket()
{
shutdown( this->socketfd, 2);
if( this->homeAddr != NULL)
delete this->homeAddr;
if( this->remoteAddr != NULL)
delete this->remoteAddr;
}
const InetAddr* TCPSocket::getHomeAddr() const
{
return this->homeAddr;
}
const InetAddr* TCPSocket::getRemoteAddr() const
{
return this->remoteAddr;
}
void TCPSocket::setHomeAddr( string ip, in_port_t port)
{
if( this->homeAddr == NULL)
this->homeAddr = new InetAddr();
this->homeAddr->setIp( ip);
this->homeAddr->setPort( port);
}
void TCPSocket::setRemoteAddr( string ip, in_port_t port)
{
if( this->remoteAddr == NULL)
this->remoteAddr = new InetAddr();
this->remoteAddr->setIp( ip);
this->remoteAddr->setPort( port);
}
int TCPSocket::Bind()
{
if( this->homeAddr == NULL)
{
cout << "HomeAddr not set" << endl;
return -1;
}
int stat = bind( this->socketfd, (struct sockaddr*) this->homeAddr->getAddrStruct(), (this->homeAddr->size()));
if( stat < 0 )
cout << "fehler beim bind: " << strerror( errno) << endl;
return 0;
}
//int TCPSocket::Bind( char* Addr, in_port_t Port)
//{
// this->SetSocketAddr( Addr);
// this->SetPort( Port);
// this->Bind();
//
// return 0;
//}
TCPSocket* TCPSocket::Accept()
{
socklen_t len = this->remoteAddr->size();
if( this->remoteAddr == NULL)
this->remoteAddr = new InetAddr();
int fd = accept( this->socketfd, (struct sockaddr*) this->remoteAddr->getAddrStruct(), &len);
TCPSocket* ns = new TCPSocket( fd, this->homeAddr, this->remoteAddr);
delete this->remoteAddr;
this->remoteAddr = NULL;
return ns;
}
void TCPSocket::Connect()
{
int state = connect( this->socketfd, (struct sockaddr*) this->remoteAddr->getAddrStruct(), sizeof( InetAddr));
if( state < 0 )
cout << "fehler beim aton: " << strerror( errno) << endl;
}
void TCPSocket::Listen()
{
int state = listen( this->socketfd, 5);
if( state < 0 )
cout << "fehler beim aton: " << strerror( errno) << endl;
}
ssize_t TCPSocket::Recv( void* buffer, size_t length)
{
return recv( this->socketfd, buffer, length, MSG_DONTWAIT);
}
ssize_t TCPSocket::Send( const void* buffer, size_t len)
{
return send( this->socketfd, buffer, len, 0);
}