Problem mit mehreren Verbindungen (Solved)



  • Moin
    habe ein Problem mit mehren gleichzeitigen Verbindungen.
    Die Verbindung als solches funktioniert nur leider, schaltet er zwischen
    den Verbindungen nicht hin und her was er eigentlich in setConnections machen soll. Habe bereits mehrfach die Bedingungen geprüft leider ohne Ergebnis.

    hat jemand vielleicht noch eine Idee was helfen könnte ?

    socket.cpp

    #include <iostream>
    #include <fcntl.h>
    #include <sys/ioctl.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <linux/sockios.h>
    #include <sys/un.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    #include "socket.h"
    
    void lSocket::Listen(){
      sc=0;
      Cons=std::string::npos;
      lsockaddr.sin_family = AF_INET;
      if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
      }
      lsockaddr.sin_port = htons(Port);
      if(Address==0){
        lsockaddr.sin_addr.s_addr = INADDR_ANY;
      }else{
        lsockaddr.sin_addr.s_addr = inet_addr(Address);
      }
      if (bind(s, (struct sockaddr *)&lsockaddr, sizeof(struct sockaddr)) == -1) {
        perror("bind");
        exit(1);
      }
      if (listen(s, 5) == -1) {
        perror("listen");
        exit(1);
      }
    }
    
    void lSocket::runDaemon(){
      int cs = 0;
      while(1){
        rs=sizeof(rsockaddr);
        if ((cs = accept(s, (struct sockaddr *)&rsockaddr, (socklen_t*)&rs)) == -1){
          perror("accept");
        }else{
          int flags = fcntl(sc, F_GETFL); 
          fcntl(sc, F_SETFL, flags | O_NONBLOCK); 
          Connections.push_back(cs);
          std::cerr << "new connection" << std::endl;
          std::cerr << "  ip:     " << inet_ntoa(rsockaddr.sin_addr) << std::endl;
          std::cerr << "  port:   " << rsockaddr.sin_port << std::endl;
          std::cerr << "  socket: " << cs << std::endl << std::endl;
          std::cout << "Connections in Queue: " << Connections.size() << "\n";
        }
      }
    }
    
    const char *lSocket::getData(){
      memset(Buffer,0,BUFFER_SIZE);
      if(sc <= 0){
        return 0;
      }
      std::cerr << "recv data (" << sc << ")..." << std::endl;
      int r=recv(sc, Buffer, BUFFER_SIZE,MSG_DONTWAIT);
      std::cerr << "data size: " << r << std::endl;
      if(r >= 0){
        std::cerr << "done" << std::endl;
        return Buffer;
      }else if(r < 0) {
        perror("recv");
        return 0;
      }
      sc = -1;
      return 0;
    }
    
    void lSocket::sendData(const char *data, size_t dsize){
      if(sc <= 0){
        std::cerr << "sc is null" << std::endl;
        return;
      }
      std::cerr << "send data (" << sc << ")..." << std::endl;
      send(sc,data,dsize, 0);
    }
    
    void lSocket::closeConnection(){
      close(sc);
      if(Connections.empty()){
        return;
      }
      std::vector<int>::iterator cit;
      for (cit=Connections.begin(); cit < Connections.end(); ++cit ){
        if(*cit==sc){
          Connections.erase(cit);
          break;
        }
      }
      std::cout << "Client Connection closed\n";
    }
    
    void lSocket::closeSocket(){
      close(s);
    }
    
    void lSocket::setBindAddress(const char* address){
      Address=address;
    }
    
    void lSocket::setPort(int port){
      Port=port;
    }
    
    void lSocket::setConnections(){  
      std::cout << Connections.size() << " are connected\n";
      if(Cons==std::string::npos && !Connections.empty()){
        Cons=0;
        sc=Connections[Cons];
        return;
      }else if(!Connections.empty() && Cons<=Connections.size()){
        std::cout << "swith to first connection\n";
        Cons=0;
        sc=Connections[Cons];
        return;
      }else if(!Connections.empty() && Connections[Cons] > 0){    
        Cons++;
        sc=Connections[Cons];
        return;
      }
    }
    
    Socket::Socket(){
      local = new lSocket;
    }
    
    Socket::~Socket(){
      local->closeSocket();
      t1->join();
      delete t1;
      delete local;
    }
    
    void Socket::Listen(){
      local->Listen();
      t1= new std::thread([local] () { local->runDaemon(); });
    }
    
    void Socket::setBindAddress(const char *addr){
      local->setBindAddress(addr);
    }
    
    void Socket::setPort(int port){
      local->setPort(port);
    }
    
    const char *Socket::getData(){
      return local->getData();
    }
    
    void Socket::sendData(const char *data, size_t dsize){
      if(dsize==0){
        dsize=strlen(data);
      }
      local->sendData(data,dsize);
    }
    
    void Socket::closeConnection(){
      local->closeConnection();
    }
    
    void Socket::setConnections(){
      local->setConnections();
    }
    

    socket.h

    #include <iostream>
    #include <vector>
    #include <thread>
    
    #include <arpa/inet.h>
    
    #ifndef SOCKET_H
    #define SOCKET_H
    
    #define BUFFER_SIZE 1024
    
    struct lSocket {
      void Listen();
      void runDaemon();
      const char *getData();
      void sendData(const char *data, size_t dsize);
      void closeConnection();
      void closeSocket();
      void setBindAddress(const char* address);  
      void setPort(int port);
      void setConnections();
    private:
      const char *Address;
      char Buffer[BUFFER_SIZE];
      int s,sc,rs,len,Port;
      size_t Cons;
      std::vector<int> Connections;
      sockaddr_in lsockaddr,rsockaddr;
    };
    
    class Socket {
    public:
      Socket();
      ~Socket();
    
      void Listen(); 
      void setBindAddress(const char *addr);
      void setPort(int port);
      const char *getData();  
      void sendData(const char *data, size_t dsize=0); 
      void closeConnection();
      void setConnections();
    
    private:
      std::string Buffer;
      std::thread *t1;
      lSocket *local;
    };
    
    #endif
    


  • Ich habs nen else if ist ein bißchen sinn befreit ^^ oh man


Anmelden zum Antworten