summaryrefslogtreecommitdiff
path: root/i/simulotron/src/socket/socket.cc
blob: 2f7139c5d8df37c8359f704cae0e4c1e8f6175d0 (plain)
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
#include <sys/socket.h>

Socket::Socket(const std::string & address, int port, bool block)
{
    // Cr�e le file descriptor du socket
    socket_ = socket (PF_INET, SOCK_STREAM, 0);
    if(socket_ < 0)
	throw errno_exception ("La chaussette n'a pas pu �tre tricot�", errno);

    // On bind le socket
    if(bind (socket_, address.getSockaddr(), sizeof(*address.getSockaddr())) < 0)
	throw errno_exception("Impossible d'assigner la chausette", errno);
}

void Socket::connect(const Address & address)
{
    if (connect (socket_, address.getSockaddr(), sizeof(*address.getSockaddr()) < 0))
	throw errno_exception("Impossible de mettre la chaussette", errno);
}

void Socket::listen(int maxQueue)
{
    if( listen(socket_, maxQueue) < 0 )
	throw errno_exception("Impossible d'�couter la mer dans la chaussette", errno);
}

Socket Socket::accept(Address & address)
{
    sockaddr addr;
    socklen_t length;
    
    int socket = accept(socket_, &addr, &length);

    if (socket < 0)
	throw errno_exception("Chaussette bloqu� � la douane", errno);
    address = Address(addr, length);
 
    return Socket(socket);
}