summaryrefslogtreecommitdiff
path: root/i/chuck/src/socket/server_socket.cc
diff options
context:
space:
mode:
authorbecquet2007-05-10 18:49:20 +0000
committerbecquet2007-05-10 18:49:20 +0000
commit8f486613be58ced269db1d437e560c16558604e8 (patch)
tree41e94b2122a118cb06abf6fc2a0038cd1dfbec4a /i/chuck/src/socket/server_socket.cc
parent4daa2c76c2a028e4b2c8ab379e7d1e0f535a0a31 (diff)
Création de chuck, le programme du robot 2007.
Diffstat (limited to 'i/chuck/src/socket/server_socket.cc')
-rw-r--r--i/chuck/src/socket/server_socket.cc86
1 files changed, 86 insertions, 0 deletions
diff --git a/i/chuck/src/socket/server_socket.cc b/i/chuck/src/socket/server_socket.cc
new file mode 100644
index 0000000..539b94e
--- /dev/null
+++ b/i/chuck/src/socket/server_socket.cc
@@ -0,0 +1,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;
+}