Browse Source

Import source code

DricomDragon 4 years ago
parent
commit
ee79f7e895

+ 68 - 0
netChat/client/Client.cpp

@@ -0,0 +1,68 @@
+#include "Client.h"
+
+Client::Client(std::string IP, int port): m_IP(IP), m_port(port)
+{
+    m_csock.recsize = sizeof(m_csock.sin);
+    std::cout << "-°*,-Programme Client-,*°-" << std::endl;
+    std::cout << "Initialisation..." << std::endl;
+    #ifdef WIN32
+        m_erreur = WSAStartup(MAKEWORD(2,2), &m_WSAData);
+    #else
+        m_erreur = 0;
+    #endif // WIN32
+}
+
+Client::~Client()
+{
+    std::cout << "Fermeture de la socket client" << std::endl;
+    closesocket(m_csock.sock);
+    std::cout << "Fermeture du client termine" << std::endl;
+    #ifdef WIN32
+        WSACleanup();
+    #endif // WIN32
+}
+
+
+
+bool Client::rendreUtilisable()
+{
+    if(!m_erreur)
+    {
+        m_csock.sock = socket(AF_INET,SOCK_STREAM, 0);
+        if(m_csock.sock != INVALID_SOCKET)
+        {
+            std::cout << "La socket " << m_csock.sock << " est maintenant ouverte en mode TCP/IP" << std::endl;
+            m_csock.sin.sin_addr.s_addr = inet_addr(m_IP.c_str());
+            m_csock.sin.sin_family = AF_INET;
+            m_csock.sin.sin_port = htons(m_port);
+
+            if(connect(m_csock.sock, (SOCKADDR*)&m_csock.sin, m_csock.recsize) != SOCKET_ERROR)
+            {
+                std::cout << "Connection a " << inet_ntoa(m_csock.sin.sin_addr) << " sur le port " << htons(m_csock.sin.sin_port) << " reussie" << std::endl;
+            }
+            else
+                return false;
+        }
+        else
+            return false;
+    }
+    else
+        return false;
+    return true;
+}
+
+void Client::envoyer(void* donnee, int tailleByte)
+{
+    if(tailleByte != 0)
+        send(m_csock.sock, (char*)donnee, tailleByte, 0);
+    else
+        send(m_csock.sock, (char*)donnee, sizeof(donnee), 0);
+}
+
+void Client::recevoir(void* donnee, int tailleByte)
+{
+    if(tailleByte != 0)
+        recv(m_csock.sock, (char*)donnee, tailleByte, 0);
+    else
+        recv(m_csock.sock, (char*)donnee, sizeof(donnee), 0);
+}

+ 54 - 0
netChat/client/Client.h

@@ -0,0 +1,54 @@
+#ifndef CLIENT_H_INCLUDED
+#define CLIENT_H_INCLUDED
+
+#ifdef WIN32
+    #include <winsock2.h>
+    typedef int socklen_t;
+#elif defined (linux)
+    #include <sys/types.h>
+    #include <sys/socket.h>
+    #include <netinet/in.h>
+    #include <arpa/inet.h>
+    #include <unistd.h>
+
+    #define INVALID_SOCKET -1
+    #define SOCKET_ERROR -1
+    #define closesocket(s) close (s)
+
+    typedef int SOCKET;
+    typedef struct sockaddr_in SOCKADDR_IN;
+    typedef struct sockaddr SOCKADDR;
+
+#endif // WIN32
+#include <iostream>
+
+struct Sock
+{
+    SOCKADDR_IN sin;
+    SOCKET sock;
+    socklen_t recsize;
+};
+
+class Client
+{
+public:
+    Client(std::string IP, int port);
+    ~Client();
+
+    bool rendreUtilisable();
+    void envoyer(void* donnee, int tailleByte);
+    void recevoir(void* donnee, int tailleByte);
+
+private:
+    #ifdef WIN32
+    WSADATA m_WSAData;
+    #endif
+
+    Sock m_csock;
+
+    std::string m_IP;
+    int m_port;
+    int m_erreur;
+};
+
+#endif // CLIENT_H_INCLUDED

+ 53 - 0
netChat/client/main.cpp

@@ -0,0 +1,53 @@
+#include <iostream>
+#include "Client.h"
+
+using namespace std;
+
+struct Perso
+{
+    char nom[30];
+    int vie;
+    int mana;
+};
+
+int main()
+{
+    /// Initialisation
+    cout << "Client :" << endl;
+    Client lan("192.168.1.87", 9995);
+    Perso guerrier;
+    string nameInMain;
+
+    /// Démarrage
+    if (!lan.rendreUtilisable())
+    {
+        cout << "Problème d'innitiaisation des connexions." << endl;
+        return 1;
+    }
+
+    ///Entrées
+    //Nom
+    cout << "Entrer le nom : ";
+    getline(cin, nameInMain);
+    for (unsigned int i(0); i<nameInMain.size() && i<29; i++)
+        guerrier.nom[i] = nameInMain[i];
+    guerrier.nom[29] = '\0';
+
+    //Vie
+    cout << "Entrer la vie : ";
+    cin >> guerrier.vie;
+
+    //Mana
+    cout << "Entrer le mana : ";
+    cin >> guerrier.mana;
+
+    /// Echanges
+    lan.envoyer(&guerrier, sizeof(Perso));
+    lan.recevoir(&guerrier, sizeof(Perso));
+
+    /// Presentation
+    cout << "Nom : " << guerrier.nom << "\nVie : " << guerrier.vie << "\nMana : " << guerrier.mana << endl;
+
+    ///Fin
+    return 0;
+}

+ 90 - 0
netChat/server/Serveur.cpp

@@ -0,0 +1,90 @@
+#include "Serveur.h"
+
+Serveur::Serveur(int port, int nbrClient): m_port(port), m_nbrClient(nbrClient)
+{
+    #ifdef WIN32
+        m_erreur = WSAStartup(MAKEWORD(2,2), &m_WSAData);
+    #else
+        m_erreur = 0;
+    #endif // WIN32
+    m_sock.recsize = sizeof(m_sock.sin);
+
+    m_csock = new Sock[m_nbrClient];
+    for(int i = 0;i<m_nbrClient;i++)
+    {
+        m_csock[i].recsize = sizeof(m_csock[i].sin);
+    }
+
+}
+
+Serveur::~Serveur()
+{
+    #ifdef WIN32
+        WSACleanup();
+    #endif // WIN32
+    std::cout << "Fermeture des sockets clientes" << std::endl;
+    for(int i = 0;i<m_nbrClient;i++)
+        closesocket(m_csock[i].sock);
+    std::cout << "Fermeture de la socket serveur" << std::endl;
+    closesocket(m_sock.sock);
+    std::cout << "Fermeture du serveur termine" << std::endl;
+}
+
+
+bool Serveur::rendreUtilisable()
+{
+    if(!m_erreur)
+    {
+        m_sock.sock = socket(AF_INET, SOCK_STREAM, 0);// SOCK_STREAM pour TCP/IP, SOCK_DGRAM pour UDP/IP
+        if(m_sock.sock != INVALID_SOCKET)
+        {
+            std::cout << "La socket " << m_sock.sock << " est maintenant ouverte en mode TCP/IP" << std::endl;
+            m_sock.sin.sin_addr.s_addr = htonl(INADDR_ANY);
+            m_sock.sin.sin_family = AF_INET;
+            m_sock.sin.sin_port = htons(m_port);
+            if(bind(m_sock.sock, (SOCKADDR*)&m_sock.sin, m_sock.recsize) != SOCKET_ERROR)
+            {
+                std::cout << "Listage du port " << m_port << "..." << std::endl;
+                if(listen(m_sock.sock, 5) != SOCKET_ERROR)
+                {
+                    std::cout << "Patientez pendant que les " << m_nbrClient << " clients se connectent sur le port " << m_port << "..." << std::endl;
+                    for(int i = 0;i<m_nbrClient;i++)
+                    {
+                        m_csock[i].sock = accept(m_sock.sock, (SOCKADDR*)&m_csock[i].sin, &m_csock[i].recsize);// renvoi INVALID_SOCKET si il y a un probleme
+                        if(m_csock[i].sock != INVALID_SOCKET)
+                        {
+                            std::cout << "Un clien se connect avec la socket " << m_csock[i].sock << " de " << inet_ntoa(m_csock[i].sin.sin_addr) << " : " << htons(m_csock[i].sin.sin_port) << std::endl;
+                        }
+                        else
+                            return false;
+                    }
+                }
+                else
+                    return false;
+            }
+            else
+                return false;
+        }
+        else
+            return false;
+    }
+    else
+        return false;
+    return true;
+}
+
+void Serveur::envoyer(void* donnee, int indice, int tailleByte)
+{
+    if(tailleByte != 0)
+        send(m_csock[indice].sock, (char*)donnee, tailleByte, 0);
+    else
+        send(m_csock[indice].sock, (char*)donnee, sizeof(donnee), 0);
+}
+
+void Serveur::recevoir(void* donnee, int indice, int tailleByte)
+{
+    if(tailleByte != 0)
+        recv(m_csock[indice].sock, (char*)donnee, tailleByte, 0);
+    else
+        recv(m_csock[indice].sock, (char*)donnee, sizeof(donnee), 0);
+}

+ 55 - 0
netChat/server/Serveur.h

@@ -0,0 +1,55 @@
+#ifndef SERVEUR_H_INCLUDED
+#define SERVEUR_H_INCLUDED
+#ifdef WIN32
+    #include <winsock2.h>
+    typedef int socklen_t;
+#elif defined (linux)
+    #include <sys/types.h>
+    #include <sys/socket.h>
+    #include <netinet/in.h>
+    #include <arpa/inet.h>
+    #include <unistd.h>
+
+    #define INVALID_SOCKET -1
+    #define SOCKET_ERROR -1
+    #define closesocket(s) close (s)
+
+    typedef int SOCKET;
+    typedef struct sockaddr_in SOCKADDR_IN;
+    typedef struct sockaddr SOCKADDR;
+
+#endif // WIN32
+#include <iostream>
+
+struct Sock
+{
+    SOCKADDR_IN sin;
+    SOCKET sock;
+    socklen_t recsize;
+};
+
+class Serveur
+{
+public:
+    Serveur(int port, int nbrClient);
+    ~Serveur();
+
+    bool rendreUtilisable();
+    void envoyer(void* donnee, int indice, int tailleByte);
+    void recevoir(void* donnee, int indice, int tailleByte);
+
+
+private:
+    #ifdef WIN32
+    WSADATA m_WSAData;
+    #endif
+
+    Sock m_sock;
+    Sock *m_csock;
+
+    int m_port;
+    int m_erreur;
+    const int m_nbrClient;
+};
+
+#endif // SERVEUR_H_INCLUDED

+ 9 - 0
netChat/server/main.cpp

@@ -0,0 +1,9 @@
+#include <iostream>
+
+using namespace std;
+
+int main()
+{
+    cout << "Hello world!" << endl;
+    return 0;
+}