| 1 | // Implementation of the ServerSocket class
 | 
|---|
| 2 | // Franck RICHARD
 | 
|---|
| 3 | // BAORadio
 | 
|---|
| 4 | 
 | 
|---|
| 5 | #include "ServerSocket.h"
 | 
|---|
| 6 | #include "SocketException.h"
 | 
|---|
| 7 | #include <net/if.h>
 | 
|---|
| 8 | #include <sys/types.h>
 | 
|---|
| 9 | #include <sys/socket.h>
 | 
|---|
| 10 | #include <sys/ioctl.h>
 | 
|---|
| 11 | #include <netinet/in.h>
 | 
|---|
| 12 | 
 | 
|---|
| 13 | 
 | 
|---|
| 14 | ServerSocket::ServerSocket ( int port )
 | 
|---|
| 15 | {
 | 
|---|
| 16 |     if ( ! Socket::create() )
 | 
|---|
| 17 |     {
 | 
|---|
| 18 |         throw SocketException ( "Could not create server socket." );
 | 
|---|
| 19 |     }
 | 
|---|
| 20 | 
 | 
|---|
| 21 |     if ( ! Socket::bind ( port ) )
 | 
|---|
| 22 |     {
 | 
|---|
| 23 |         throw SocketException ( "Could not bind to port." );
 | 
|---|
| 24 |     }
 | 
|---|
| 25 | 
 | 
|---|
| 26 |     if ( ! Socket::listen() )
 | 
|---|
| 27 |     {
 | 
|---|
| 28 |         throw SocketException ( "Could not listen to socket." );
 | 
|---|
| 29 |     }
 | 
|---|
| 30 | 
 | 
|---|
| 31 |     Socket::set_non_blocking(true);
 | 
|---|
| 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 | 
 | 
|---|
| 62 | void ServerSocket::accept ( ServerSocket& sock )
 | 
|---|
| 63 | {
 | 
|---|
| 64 |     if ( ! Socket::accept ( sock ) )
 | 
|---|
| 65 |     {
 | 
|---|
| 66 |         throw SocketException ( "Could not accept socket." );
 | 
|---|
| 67 |     }
 | 
|---|
| 68 | }
 | 
|---|
| 69 | 
 | 
|---|
| 70 | void ServerSocket::shutdown ( )
 | 
|---|
| 71 | {
 | 
|---|
| 72 |     if ( ! Socket::shutdown (  ) )
 | 
|---|
| 73 |     {
 | 
|---|
| 74 |         throw SocketException ( "Could not shutdown socket." );
 | 
|---|
| 75 |     }
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | void ServerSocket::create ( )
 | 
|---|
| 79 | {
 | 
|---|
| 80 |     if ( ! Socket::create (  ) )
 | 
|---|
| 81 |     {
 | 
|---|
| 82 |         throw SocketException ( "Could not create socket." );
 | 
|---|
| 83 |     }
 | 
|---|
| 84 | }
 | 
|---|
| 85 | 
 | 
|---|
| 86 | bool ServerSocket::connect(std::string IP ) 
 | 
|---|
| 87 | {
 | 
|---|
| 88 |    return Socket::connect (IP, 8000);  
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | std::string ServerSocket::recupip( ServerSocket& sock)
 | 
|---|
| 92 | { 
 | 
|---|
| 93 |    return ( Socket::recupip() ); 
 | 
|---|
| 94 | }
 | 
|---|