Skip to content

Commit a1eae74

Browse files
committed
Classe ServerSocket
Dá funcionalidade de servidor ao socket.
1 parent 47a76d1 commit a1eae74

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

src/httpServer/ServerSocket.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* http-cpp-server-base
2+
* Base de um servidor http simples em c++ para escrever
3+
* serviços para projetos de robótica.
4+
* Implementation of the ServerSocket class
5+
*
6+
* @author : Rob Tougher (mail-to: rtougher@yahoo.com)
7+
* @source : http://tldp.org/LDP/LG/issue74/tougher.html
8+
* @date : 2002
9+
*/
10+
11+
#include "ServerSocket.h"
12+
#include "SocketException.h"
13+
14+
15+
ServerSocket::ServerSocket ( int port )
16+
{
17+
if ( ! Socket::create() )
18+
{
19+
throw SocketException ( "Could not create server socket." );
20+
}
21+
22+
if ( ! Socket::bind ( port ) )
23+
{
24+
throw SocketException ( "Could not bind to port." );
25+
}
26+
27+
if ( ! Socket::listen() )
28+
{
29+
throw SocketException ( "Could not listen to socket." );
30+
}
31+
32+
}
33+
34+
ServerSocket::~ServerSocket()
35+
{
36+
}
37+
38+
39+
const ServerSocket& ServerSocket::operator << ( const std::string& s ) const
40+
{
41+
if ( ! Socket::send ( s ) )
42+
{
43+
throw SocketException ( "Could not write to socket." );
44+
}
45+
46+
return *this;
47+
48+
}
49+
50+
51+
const ServerSocket& ServerSocket::operator >> ( std::string& s ) const
52+
{
53+
if ( ! Socket::recv ( s ) )
54+
{
55+
throw SocketException ( "Could not read from socket." );
56+
}
57+
58+
return *this;
59+
}
60+
61+
void ServerSocket::accept ( ServerSocket& sock )
62+
{
63+
if ( ! Socket::accept ( sock ) )
64+
{
65+
throw SocketException ( "Could not accept socket." );
66+
}
67+
}

src/httpServer/ServerSocket.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* http-cpp-server-base
2+
* Base de um servidor http simples em c++ para escrever
3+
* serviços para projetos de robótica.
4+
* Definition of the ServerSocket class
5+
*
6+
* @author : Rob Tougher (mail-to: rtougher@yahoo.com)
7+
* @source : http://tldp.org/LDP/LG/issue74/tougher.html
8+
* @date : 2002
9+
*/
10+
11+
#ifndef ServerSocket_class
12+
#define ServerSocket_class
13+
14+
#include "Socket.h"
15+
16+
17+
class ServerSocket : private Socket
18+
{
19+
public:
20+
21+
ServerSocket ( int port );
22+
ServerSocket (){};
23+
virtual ~ServerSocket();
24+
25+
const ServerSocket& operator << ( const std::string& ) const;
26+
const ServerSocket& operator >> ( std::string& ) const;
27+
28+
void accept ( ServerSocket& );
29+
30+
};
31+
32+
33+
#endif

0 commit comments

Comments
 (0)