Netzwerk code funktionier nicht



  • Versuche Daten zu übermitteln leider klappt das nicht, server erstellt socket client greift nicht drauf zu, problem ist das er sagt port belegt obwohl dieser frei ist.
    netclient.cpp

    /*
    Writen by Jan Koster release under lgpl
    
    */
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <easyc.h>
    #include "netclient.h"
    
    #define BUF 1024
    
    using namespace std;
    using namespace easyc;
    
    struct sockaddr_in address;
    
    bool netclient::opensocket()
    {
    if ((create_socket=socket (AF_INET, SOCK_STREAM,0)) > 0)
    {
        cout << "Socket Angelegt" << endl;
    address.sin_family= AF_INET;
    address.sin_port = htons(6660);
    return 0;
    }
    else
    cout << "Socket konnte nicht Angelegt Werden" <<endl;
    return 1;
    }
    
    bool netclient::netconnect(std::string ipadr)
    {
    inet_aton ((stringtochar(ipadr)), &address.sin_addr);
    if (connect (create_socket, (struct sockaddr *) &address, sizeof (address)) == 0)
      {
      cout << "Verbindung Hergestellt" << inet_ntoa(address.sin_addr) << endl;
      return 0;
      }
      else
         cout <<"Verbindung Fehlgeschlagen"<< endl;
      return 1;
     }
    
    bool netclient::netsend(std::string data)
    {
    send(create_socket,(stringtochar(data)),strlen(stringtochar(data)),0 );
    }
    
    void netclient::netend()
    {
    close (create_socket);
    }
    

    netclient.h

    /*
    Writen by Jan Koster release under lgpl
    
    */
    class netclient
    {
    private:
    int create_socket;
    
    public:
    bool opensocket();
    bool netconnect(std::string ipadr);
    bool netsend(std::string data);
    void netend();
    };
    

    main.cpp

    /*
    Writen by Jan Koster release under lgpl
    
    */
    #include <iostream>
    #include <istream>
    #include <fstream>
    #include <string>
    #include <map>
    #include <utility> 
    #include <easyc.h>
    #include "main.h"
    #include "module.h"
    #include "netclient.h"
    
    using namespace std;
    using namespace easyc;
    
    int main()
    {
    ......
    modul("/usr/lib/nettesttool/modules/libpi.so");
    
    string ipadr = (readconf("/etc/nettest.conf","serverip="));
    
    netclient mainnet;
    
    if(mainnet.opensocket()==0)
    {
    while(mainnet.netconnect(ipadr)==0)
    {
    
    for( multimap<string,string>::iterator ii=testdata.begin(); ii!=testdata.end(); ++ii)
       {
          mainnet.netsend((*ii).first);
          mainnet.netsend((*ii).second);
       }
    
    mainnet.netend();
    return 0;
    }
    }
    }
    

    netserver.cpp

    #include <iostream>
    #include <string>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include "netserver.h"
    
    #define BUF 1024
    
    using namespace std;
    
    struct sockaddr_in address;
    int create_socket, serversocket;
    socklen_t addrlen;
    ssize_t size;
    const int y = 1;
    
    bool netserver::opensocket()
    {
    if((create_socket=socket(AF_INET, SOCK_STREAM, 0)) >0)
    {
    cout << "Socket wurde angelegt" <<endl;
    return 0;
    }
    else 
    return 1;
    }
    
    bool netserver::getport()
    {
    setsockopt(create_socket, SOL_SOCKET, SO_REUSEADDR, &y,sizeof(int));
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(6600);
    if (bind (create_socket,(struct sockaddr *) &address, sizeof (address) !=0))
    {
    addrlen = sizeof (struct sockaddr_in);
    return 0;
    }
    else
    cout << "Port Belegt" << endl;
    return 1; 
    }
    
    bool netserver::serverlisten()
    {
    while(1)
    {
    serversocket = accept (create_socket, (struct sockaddr *) &address, &addrlen); 
    if (serversocket > 0)
    {
    cout << "Client verbunden" << endl;
    return 0;
    }
    }
    }
    
    string netserver::getdata()
    {
    string data;
    char *temp;
    size = recv(serversocket, temp, BUF-1,0 );
    if (size > 0)
    {
    data = temp;
    return data;
    }
    else
    return "nodata"; 
    }
    
    void netserver::closeconnect()
    {
    close (serversocket);
    }
    

    netserver.h

    #ifndef NETSERVER_H_
    #define NETSERER_H_
    
    class netserver
    {
    private:
    
    public:
    bool opensocket();
    bool getport();
    bool serverlisten();
    std::string getdata();
    void closeconnect();
    };
    
    #endif
    

    server main.cpp

    #include <iostream>
    #include <string>
    
    #include "main.h"
    #include "netserver.h"
    
    using namespace std;
    
    int main()
    {
    string data;
    netserver mainserver;
    
    if(mainserver.opensocket() == 0)
    {
        if(mainserver.getport() == 0)
        {
          if(mainserver.serverlisten()== 0)
          {
    	data = mainserver.getdata();
    	cout << data <<endl;
          }
    
        }
    }
    else 
    cout << "Konnte Socket nicht anlegen"<< endl;
    }
    


  • habe den Fehler:
    if (bind (create_socket,(struct sockaddr 😉 &address, sizeof (address) !=0))
    zu
    if (bind (create_socket,(struct sockaddr 😉 &address, sizeof (address)) !=0)
    und es geht 😃


Log in to reply