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/Makefile.am | 7 +++++++ i/simulotron/src/main.cc | 5 +++++ i/simulotron/src/socket/socket.cc | 41 +++++++++++++++++++++++++++++++++++++++ i/simulotron/src/socket/socket.hh | 29 +++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 i/simulotron/src/Makefile.am create mode 100644 i/simulotron/src/main.cc create mode 100644 i/simulotron/src/socket/socket.cc create mode 100644 i/simulotron/src/socket/socket.hh (limited to 'i/simulotron/src') diff --git a/i/simulotron/src/Makefile.am b/i/simulotron/src/Makefile.am new file mode 100644 index 0000000..2970e77 --- /dev/null +++ b/i/simulotron/src/Makefile.am @@ -0,0 +1,7 @@ +bin_PROGRAMS = simulotron +simulotron_SOURCES = main.cc \ + socket/socket.cc socket/socket.hh + +simulotron_CXXFLAGS = -g -Wall -fmessage-length=0 +simulotron_LDADD = +INCLUDES = -I../../src diff --git a/i/simulotron/src/main.cc b/i/simulotron/src/main.cc new file mode 100644 index 0000000..2992206 --- /dev/null +++ b/i/simulotron/src/main.cc @@ -0,0 +1,5 @@ + +int main(void) +{ + return 0; +} 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); +} + + diff --git a/i/simulotron/src/socket/socket.hh b/i/simulotron/src/socket/socket.hh new file mode 100644 index 0000000..c1b47df --- /dev/null +++ b/i/simulotron/src/socket/socket.hh @@ -0,0 +1,29 @@ +class Socket +{ + private: + /// Ze socket + int socket_; + public: + /// Constructeur + Socket(const Address & adress, bool block = false); + /// Constructeur à partir d'un fd + Socket(int socket); + /// Connect le client au serveur + void connect(void); + /// Ecoute le port + void listen(void); + /// accepte une connection + void accept(void); + /// lit du socket + void read(void); + /// écrit dans le socket + void write(void); + /// lit un charactère du socket + void getChar(void); + /// écrit un charactère dans le socket + void putChar(void); + /// Drapeau connecté + bool isConnected(void); + private: + void bind(); +}; -- cgit v1.2.3