summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/socket/server_socket.cc
blob: 539b94ef623af94793ebea5f8db8d3cd438e1416 (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
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
// server_socket.cc
// robert - programme du robot 2005. {{{
//
// Copyright (C) 2005 Dufour J�r�my
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// Contact :
//        Web: %WEB%
//      Email: <dufourj@efrei.fr>
// }}}
#include "server_socket.hh"

#include "address.hh"

#include <sys/types.h>
#include <sys/socket.h>
#include <stdexcept>

/// Constructeur par d�faut.
ServerSocket::ServerSocket (int port)
{
    bind (port);
}

/// Bind le serveur sur un port d'�coute.
void
ServerSocket::bind (int port)
{
    static const int reuse_s = 1;
    // Adresse sur un port (pas d'hote, �coutera de partout)
    Address a (port);
    // Cr�ation du socket IPv4 - TCP
    socket_ = socket (PF_INET, SOCK_STREAM, 0);
    if (socket_ == -1)
	throw std::runtime_error ("Erreur de cr�ation du socket");
    // Socket r�utilisable en cas d'arret "brutale"
    if (setsockopt (socket_, SOL_SOCKET, SO_REUSEADDR,
		    &reuse_s, sizeof (int)) == -1)
	throw std::runtime_error ("Erreur setsockopt : reusable");
    // Bindage du socket sur le port et l'adresse
    if (::bind (socket_, a.getSockaddr (), sizeof (struct sockaddr_in)) == -1)
	throw std::runtime_error ("Erreur de bind du socket");
    // On met le socket en attente de connexion
    if (listen (socket_, 0) == -1)
	throw std::runtime_error ("Erreur de listen");
}

// Accepte une nouvelle connexion.
int
ServerSocket::accept (void) const
{
    int s = ::accept (socket_, 0, 0);
    if (s == -1)
	throw std::runtime_error ("Erreur d'accept de nouvelles connexions");
    return s;
}

// Accepte une nouvelle connexion et remplie Address.
int
ServerSocket::accept (Address &a) const
{
    sockaddr_in sa;
    socklen_t sl = sizeof sa;
    int s = ::accept (socket_, reinterpret_cast<sockaddr *> (&sa),
		      &sl);
    if (s == -1)
      {
	throw std::runtime_error ("Erreur d'accept nouvelle connexion");
      }
    // R�cup�ration de l'adresse
    a = Address (reinterpret_cast<const sockaddr *> (&sa), sl);
    return s;
}