代码之家  ›  专栏  ›  技术社区  ›  KS7X

创建套接字时出现分段错误

  •  -3
  • KS7X  · 技术社区  · 6 年前

    if(socketClient::sock = socket(AF_INET,SOCK_STREAM,0) < 0);
            return false;
    
    int on = 1;
    if (setsockopt(socketClient::sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)) == -1)
        return false;
    
    if(socketClient::connect("172.16.0.37", 50001))
    {
        socketClient::connected_ = true;
        return true;
    }
    

    我已经使用gdb来确认在创建套接字时抛出分段错误 socket(AF_INET,SOCK_STREAM,0)

    在这一点上我很难堪。我的代码编译时没有警告,并链接成可执行文件。为什么会这样?

    socketClient.h:(删除)

    #include <iostream>
    #include "clientSocket.h"
    #include <vector>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <unistd.h>
    #include <string>
    #include <arpa/inet.h>
    #include <vector>
    
    #include <string.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <iostream>
    #include <sys/sendfile.h>
    #include <sys/stat.h>
    
    #ifndef SOCKETCLIENT_H
    #define SOCKETCLIENT_H
    
    using namespace std;
    
    class socketClient final
    {
    public:
        socketClient(std::string, int);
        ~socketClient();
    
    
    
        static bool connect(std::string, int);
    
    
    
    
    private:
        static int sock;
            };
    
    #endif // SOCKETCLIENT_H
    

    //主.cpp

    #include <cstdio>
    #include "logger.h"
    #include "startDaemon.h"
    #include "hostFinder.h"
    #include "localDatabase.h"
    #include <boost/thread.hpp>
    #include <boost/chrono.hpp>
    #include <boost/format.hpp>
    #include "socketClient.h"
    
    
    int main()
    {
        try
        {
            socketClient::connect("127.0.0.1", 50001);          
    
    
        }
        catch (std::exception& e)
        {
            printf("We caught something bad");
            return 0;
        }
    
    }
    

    //socketClient.cpp文件

    bool socketClient::connect(std::string hostName, int port)
    {
        std::string eMessage;
        boost::format fmt;
        try
        {
            sock = socket(AF_INET,SOCK_STREAM,0);
            return true;    
    
        }
        catch (std::exception& e)
        {
            fmt = boost::format("Process Shutdown: %s") % e.what();
            eMessage = fmt.str();
            logger::instance().log(eMessage, logger::kLogLevelError);
            return false;
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   ralf htp    6 年前

    https://www.binarytides.com/code-a-simple-socket-client-class-in-c/ 是工作代码。必须实现构造函数并创建类的实例。然后调用 connect() 从这个实例cf 这个 static 没有必要。。。

    :

    /**
        TCP Client class
    */
    class tcp_client
    {
    private:
        int sock;
        std::string address;
        int port;
        struct sockaddr_in server;
    
    public:
        tcp_client();
        bool conn(string, int);
        bool send_data(string data);
        string receive(int);
    };
    

    :

    tcp_client::tcp_client()
    {
        sock = -1;
        port = 0;
        address = "";
    }
    

    :

    /**
        Connect to a host on a certain port number
    */
    bool tcp_client::conn(string address , int port)
    {
        //create socket if it is not already created
        if(sock == -1)
        {
            //Create socket
            sock = socket(AF_INET , SOCK_STREAM , 0);
            if (sock == -1)
            {
                perror("Could not create socket");
            }
    
        cout<<"Socket created\n";
    }
    // [ ... ]
    }
    

    main.cpp :

    int main(int argc , char *argv[])
    {
        tcp_client c; // create instance
        string host;
    
        cout<<"Enter hostname : ";
        cin>>host;
    
        //connect to host
        c.conn(host , 80);  // invoke method
    
        //[ ... ]
    }
    

    代码是来自的不完整副本