summaryrefslogtreecommitdiff
path: root/i/simulotron/src/socket/socket.cc
blob: f231cf5b0ebe4113beb47155cf4ca108a81f3f62 (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
42
43
44
45
46
47
48
49
50
#include <sys/socket.h>
#include <cerrno>
#include "socket/socket.hh"
#include "utils/errno_exception.hh"
#include "socket/address.hh"

Socket::Socket(const Address & address)
{
    // 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);
}

Socket::Socket(int socket)
{
    socket_ = socket;
}

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);
}