From 349a66d2b2f8e725df8634f9d2947319245fb291 Mon Sep 17 00:00:00 2001 From: haller Date: Sun, 27 Nov 2005 23:27:49 +0000 Subject: Ebauche de classe socket Mise en place de la structure automake/autoconf pour le simulotron --- i/simulotron/src/socket/socket.cc | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 i/simulotron/src/socket/socket.cc (limited to 'i/simulotron/src/socket/socket.cc') diff --git a/i/simulotron/src/socket/socket.cc b/i/simulotron/src/socket/socket.cc new file mode 100644 index 0000000..2f7139c --- /dev/null +++ b/i/simulotron/src/socket/socket.cc @@ -0,0 +1,41 @@ +#include + +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); +} + + -- cgit v1.2.3