From 8020ef753b8d434c026299650fe911d71d844e78 Mon Sep 17 00:00:00 2001 From: haller Date: Tue, 29 Nov 2005 01:58:16 +0000 Subject: Modification dans l'A/A Codage du test de socket(qui foire) --- i/simulotron/autogen.sh | 5 +++ i/simulotron/configure.ac | 5 +-- i/simulotron/src/Makefile.am | 16 ++++---- i/simulotron/src/socket/Makefile.am | 8 ---- i/simulotron/src/socket/Makefile.incl.am | 5 +++ i/simulotron/src/socket/socket.cc | 45 +++++++++++++++++++++ i/simulotron/src/socket/socket.hh | 18 +++++---- i/simulotron/src/socket/test_socket.cc | 69 ++++++++++++++++++++++++++++++++ i/simulotron/src/utils/Makefile.am | 5 --- i/simulotron/src/utils/Makefile.incl.am | 4 ++ 10 files changed, 149 insertions(+), 31 deletions(-) create mode 100755 i/simulotron/autogen.sh delete mode 100644 i/simulotron/src/socket/Makefile.am create mode 100644 i/simulotron/src/socket/Makefile.incl.am delete mode 100644 i/simulotron/src/utils/Makefile.am create mode 100644 i/simulotron/src/utils/Makefile.incl.am diff --git a/i/simulotron/autogen.sh b/i/simulotron/autogen.sh new file mode 100755 index 0000000..3ae2425 --- /dev/null +++ b/i/simulotron/autogen.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +#Script initialisant le automake autoconf + +aclocal && autoconf && automake -a && ./configure -C diff --git a/i/simulotron/configure.ac b/i/simulotron/configure.ac index 49ac34e..86f89cf 100644 --- a/i/simulotron/configure.ac +++ b/i/simulotron/configure.ac @@ -7,6 +7,7 @@ AC_CONFIG_AUX_DIR(config) AM_INIT_AUTOMAKE(AC_PACKAGE_NAME, AC_PACKAGE_VERSION) AC_CONFIG_SRCDIR([src/main.cc]) + CXXFLAGS= # Checks for programs @@ -27,7 +28,5 @@ AC_CHECK_FUNCS([socket]) AC_CONFIG_FILES([Makefile doc/Makefile - src/Makefile - src/utils/Makefile - src/socket/Makefile]) + src/Makefile]) AC_OUTPUT diff --git a/i/simulotron/src/Makefile.am b/i/simulotron/src/Makefile.am index 3a767e2..5695d4d 100644 --- a/i/simulotron/src/Makefile.am +++ b/i/simulotron/src/Makefile.am @@ -1,14 +1,14 @@ -SUBDIRS = utils socket +check_PROGRAMS = + +include socket/Makefile.incl.am +include utils/Makefile.incl.am bin_PROGRAMS = simulotron simulotron_SOURCES = main.cc \ - socket/address.hh socket/address.cc \ - socket/socket.cc socket/socket.hh \ - utils/errno_exception.hh + $(socket_S) -simulotron_CXXFLAGS = -g -Wall -fmessage-length=0 +AM_CXXFLAGS = -g -Wall -fmessage-length=0 simulotron_LDADD = -INCLUDES = -I../../src +INCLUDES = -I$(srcdir) -TESTS = socket/test_socket \ - utils/test_errno_exception +TESTS = $(check_PROGRAMS) diff --git a/i/simulotron/src/socket/Makefile.am b/i/simulotron/src/socket/Makefile.am deleted file mode 100644 index f4ff19b..0000000 --- a/i/simulotron/src/socket/Makefile.am +++ /dev/null @@ -1,8 +0,0 @@ -check_PROGRAMS = test_socket -test_socket_SOURCES = test_socket.cc \ - socket.cc socket.hh \ - address.cc address.hh \ - ../utils/errno_exception.hh - -AM_CXXFLAGS=-g -W -Wall -fmessage-length=0 -INCLUDES= -I../../src diff --git a/i/simulotron/src/socket/Makefile.incl.am b/i/simulotron/src/socket/Makefile.incl.am new file mode 100644 index 0000000..7b16a9f --- /dev/null +++ b/i/simulotron/src/socket/Makefile.incl.am @@ -0,0 +1,5 @@ +address_S = socket/address.cc socket/address.hh +socket_S = socket/socket.cc socket/socket.hh $(address_S) $(errno_exception_S) + +check_PROGRAMS += test_socket +test_socket_SOURCES = socket/test_socket.cc $(socket_S) diff --git a/i/simulotron/src/socket/socket.cc b/i/simulotron/src/socket/socket.cc index f231cf5..8b0ef99 100644 --- a/i/simulotron/src/socket/socket.cc +++ b/i/simulotron/src/socket/socket.cc @@ -21,6 +21,11 @@ Socket::Socket(int socket) socket_ = socket; } +Socket::~Socket(void) +{ + close(socket_); +} + void Socket::connect(const Address & address) { if (::connect (socket_, address.getSockaddr(), sizeof(*address.getSockaddr()) < 0)) @@ -47,4 +52,44 @@ Socket Socket::accept(Address & address) return Socket(socket); } +std::string Socket::read(void) +{ + std::string str; + int charReaded; + char buffer[BUFFER_SIZE]; + + do + { + charReaded = ::read(socket_, buffer, BUFFER_SIZE - 1); + buffer[charReaded + 1] = 0; + str += buffer; + }while(charReaded == BUFFER_SIZE - 1); + return str; +} + +void Socket::write(const std::string & str) +{ + char buffer[BUFFER_SIZE]; + unsigned int pos = 0; + while(str.size() != pos) + { + int size = str.size() - pos < BUFFER_SIZE - 1 ? str.size() - pos:BUFFER_SIZE - 1; + str.copy(buffer, size, pos); + pos += size; + buffer[size] = 0; + ::write(socket_, buffer, size); + } +} + +char Socket::getChar(void) +{ + char getted; + ::read(socket_, &getted, 1); + return getted; +} + +void Socket::putChar(char c) +{ + ::write(socket_, &c, 1); +} diff --git a/i/simulotron/src/socket/socket.hh b/i/simulotron/src/socket/socket.hh index 27f3f5d..a1d6d65 100644 --- a/i/simulotron/src/socket/socket.hh +++ b/i/simulotron/src/socket/socket.hh @@ -1,3 +1,4 @@ +#include class Address; class Socket @@ -5,27 +6,30 @@ class Socket private: /// Ze socket int socket_; + /// constantes + /// Taille max du buffer de lecture/ecriture + static const int BUFFER_SIZE = 256; public: /// Constructeur Socket(const Address & address); /// Constructeur à partir d'un fd Socket(int socket); + /// Destructeur + ~Socket(void); /// Connect le client au serveur void connect(const Address & address); /// Ecoute le port void listen(int maxQueue); /// accepte une connection - Socket accept(Address & adress); + Socket accept(Address & address); /// lit du socket - void read(void); + std::string read(void); /// écrit dans le socket - void write(void); + void write(const std::string & str); /// lit un charactère du socket - void getChar(void); + char getChar(void); /// écrit un charactère dans le socket - void putChar(void); + void putChar(char c); /// Drapeau connecté bool isConnected(void); - private: - void bind(); }; diff --git a/i/simulotron/src/socket/test_socket.cc b/i/simulotron/src/socket/test_socket.cc index 2992206..67ff638 100644 --- a/i/simulotron/src/socket/test_socket.cc +++ b/i/simulotron/src/socket/test_socket.cc @@ -1,5 +1,74 @@ +#include "socket/socket.hh" +#include "socket/address.hh" + +#include int main(void) { + Address adr; + Address adr2; + // Création de deux socket; + Socket sockServ(Address(4242)); + Socket sockClient(adr); + + // Mise en écoute du socket serveur + sockServ.listen(12); + + // Demande de connection + sockClient.connect(Address(std::string("localhost"), 4242)); + Socket sockFork = sockServ.accept(adr2); + //TODO if(adr != adr2) + //{ +// std::cout << "L'adresse du client et l'adresse retourné par le accept sont différentes !!!" << std:endl; +// return 1; + // } + + // Ecriture du client dans le serveur + std::string strDepart = "J'aime la prog système."; + std::string strDest; + + sockClient.write(strDepart); + strDest = sockFork.read(); + + if(strDepart != strDest) + { + std::cout << "CHIER!! Le message d'origine est altéré en ecrivant du client au serveur!!" << std::endl; + return 1; + } + + // Ecriture du serveur au client + sockFork.write(strDepart); + strDest = sockClient.read(); + + if(strDepart != strDest) + { + std::cout << "CHIER!! Le message d'origine est altéré en ecrivant du serveur au client!!" << std::endl; + return 1; + } + + // Ecriture d'un char du client au serveur + char a = 'f'; + char b; + + sockClient.putChar(a); + b = sockFork.getChar(); + + if(a != b) + { + std::cout << "CHIER!! Le char d'origine est altéré en ecrivant du client au serveur!!" << std::endl; + return 1; + } + + // Ecriture du serveur vers le client + sockFork.putChar(a); + b = sockClient.getChar(); + + if(a != b) + { + std::cout << "CHIER!! Le char d'origine est altéré en ecrivant du serveur au client!!" << std::endl; + return 1; + } + + return 0; } diff --git a/i/simulotron/src/utils/Makefile.am b/i/simulotron/src/utils/Makefile.am deleted file mode 100644 index 6bd6053..0000000 --- a/i/simulotron/src/utils/Makefile.am +++ /dev/null @@ -1,5 +0,0 @@ -check_PROGRAMS = test_errno_exception -test_errno_exception_SOURCES = errno_exception.hh test_errno_exception.cc - -AM_CXXFLAGS=-g -W -Wall -fmessage-length=0 -INCLUDES= -I../../src diff --git a/i/simulotron/src/utils/Makefile.incl.am b/i/simulotron/src/utils/Makefile.incl.am new file mode 100644 index 0000000..68564f1 --- /dev/null +++ b/i/simulotron/src/utils/Makefile.incl.am @@ -0,0 +1,4 @@ +errno_exception_S = utils/errno_exception.hh +check_PROGRAMS += test_errno_exception +test_errno_exception_SOURCES = utils/test_errno_exception.cc \ + $(errno_exception_S) -- cgit v1.2.3