网络套接字创建问题

时间:2018-09-09 23:16:01

标签: c++ sockets networking

我在使用套接字编程创建回显程序时遇到问题。运行socket(),bind()和listen()命令时,没有任何错误代码出现。它们全部返回一个整数,值为3。这是导致accept()命令出现问题的部分。为了测试该程序,我将可执行文件作为后台进程运行,并运行telnet来测试套接字连接。该程序冻结在accept()命令中。有人可以帮忙吗?

// Include all of the headers required 
#include "echo_s.h"
// simple logger
#include "log.h"

// ***
// Class ClientConnection
// - created to help simply methods and functions used to create TCP connection
//  for this program to work
class ClientConnection {
    public:
        void createSocket();
        void setupAddress();
        void bindSocket();
        void listenSocket();
        void waitingConnection();
        int  processConnection(int connection);
    private:
        int sock = -1;                      // file descriptor for the server
        int lst  = -1;                      // file descriptor for the listener
        struct sockaddr_in
        {
            short       family;             // address family
            u_short         port;               // port number
            struct in_addr  sin_addr;           // internet address
            char        sin_zero[8];
        };
        struct sockaddr_in servAddr;                // struct needed for the server addr
        struct sockaddr_in clientAddr;              // struct needed for the listener addr
        char buffer[256];   
};

Log* logger = new Log();

// ***
// main ()
// - sets up the socket and accepts new connection until CLOSE or QUIT
int main (int argc, char *argv[])
{
    // Process the command arguments 
    int opt = 0;
    ClientConnection client;

    while ((opt = getopt(argc, argv, "v")) !=  -1) 
    {
        switch (opt) 
        {
            case 'v':
                logger->setLogger(true);
                logger->printLog("Verbose Action captured");
                break;
            case ':':
            case '?':
            default:
                std::cout<< "Invalid option: " << argv[0] << std::endl;
                exit(-1);
        }
    }

    // create the socket
    client.createSocket();
    // set up socket address
    client.setupAddress();
    // bind socket
    client.bindSocket();
    // listen socket
    client.listenSocket();
    // Wait for the connection with the accept call 
    client.waitingConnection();
}

void ClientConnection::createSocket () 
{
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
    {
        logger->printLog ("Error opening socket");
        exit (-1);
    }
    else 
    {
        logger->printLog("Socket was created");
        logger->printLog("Socket info: " + std::to_string(sock)); 
    }
}

void ClientConnection::setupAddress ()
{
    // define the struct
    srand(time(NULL));
    int port = (rand() % 10000 + 1024);

    // zero the whole struct
    bzero((char *)&servAddr, sizeof(servAddr));

    // Fill in the struct with the information need for the address of the host
    servAddr.family = AF_INET;  
    servAddr.sin_addr.s_addr = INADDR_ANY;
    servAddr.port = htons(port);
    logger->printLog("Address has been created for socket");
}

void ClientConnection::bindSocket ()
{
    int bindSuccess = 0;
    int attempts = 0;
    std::string errorString;
    if (bind(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
    {
        errorString = strerror(errno);
        logger->printLog("bind() failed: " + errorString);
        exit(-1);
    }
    else
    {
        logger->printLog("bind() successful");
        logger->printLog("Bind() function returned: " + std::to_string(sock));
        std::cout << "Port number: " << servAddr.port << std::endl;
    }
}

void ClientConnection::listenSocket ()
{
    int listenSocket = 5;
    std::string errorString;
    if (listen(sock, listenSocket) < 0) 
    {
        errorString = strerror(errno);
        logger->printLog("listen() failed: " + errorString);
        exit(-1);
    }
    else
    {
        logger->printLog("listen() successful");
        logger->printLog("listen() fucntion returns: " + std::to_string(sock));
    }
}

void ClientConnection::waitingConnection ()
{
    logger->printLog("Attempting accept()");
    int quit = -1;
    int attempts = 0;
    socklen_t sizeClient = sizeof(clientAddr);
    while(quit == -1)
    {
        logger->printLog("Inside while loop");
        //lst = accept(sock, (struct sockaddr *) &servAddr, sizeof(servAddr));
        lst = accept(sock, (struct sockaddr *) NULL, NULL);
        logger->printLog("accept() function reurns: " +  std::to_string(lst));
        if (lst < 0)
        {
            std::string errorString = strerror(errno);
            logger->printLog(errorString);          
            attempts++;
            exit(-1);
        }
        else
        {
            logger->printLog("accept() successful");
            quit = processConnection(lst);
        }
    }
}

int ClientConnection::processConnection(int connection)
{
    std::cout << "connection made. TEST" << std::endl;
    int n = 0;
    if ((n = read(connection, buffer, 255)) < 0) 
    {
        logger->printLog("Error reading data");
    }
    else
    {
        std::string message = buffer;
        logger->printLog(message);
        if (message.find("QUIT"))
        {
            close(connection);
            return 1;
        }
        if (message.find("CLOSE"))
        {
            close(connection);
            return 0;
        }
        n = write(connection, buffer, sizeof(buffer));
    }
    return 0;
}
`

0 个答案:

没有答案