简体   繁体   中英

TCP Socket - Server not accepting any connections UNIX

I am trying to code a simple multi-threaded TCP server/client application (I am learning socket programming). In my program, the server is blocking at accept() function forever even though the client trying to connect.

This is my code;

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <memory.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <stdarg.h>
#include <mutex>
#include <thread>

using namespace std;

//Declaring globals
const char* IP_ADDR = "127.0.0.1"; //Test IP address (Loop back)
mutex consoleWriteMutex; //For locking std::cout
mutex clientWakeUpMutex; //Client wake up mutex
condition_variable serverReady;

void client()
{
    int socketClient = socket(AF_INET, SOCK_STREAM, 0); //Create a TCP socket
    sockaddr_in serverAdd_in;
    const string message = "Please connect me. I'm lonely."; //Clients message

    if (socketClient < 0)
    {
        lock_guard<mutex> consoleWriteLock(consoleWriteMutex); //Lock cout
        cout << "Failed to create a client TCP socket" << endl;
    }
    else
    {
        lock_guard<mutex> consoleWriteLock(consoleWriteMutex); //Lock cout
        cout << "Successfully created a client TCP socket" << endl;
        serverAdd_in.sin_port = htons(24000); //Port number byte conversion using htons()
        serverAdd_in.sin_addr.s_addr = inet_addr(IP_ADDR);
        serverAdd_in.sin_family = AF_INET; //Family is IPv4
    }

    unique_lock<mutex> consoleWriteULock(consoleWriteMutex);
    cout << "Waiting before connecting to server: " << gethostbyname(IP_ADDR) << endl;
    cout << "Attempting to connect to server now..." << endl;
    consoleWriteULock.unlock();

    //Connect the client
    int connectStatus = connect(socketClient, (sockaddr *) &serverAdd_in,sizeof(serverAdd_in));
    unique_lock<mutex> clientWakeUpLock(clientWakeUpMutex);
    serverReady.wait(clientWakeUpLock,
                     [connectStatus](){ return (connectStatus > 0) ? true:false;});

    consoleWriteULock.lock();
    cout << "Server status: LISTENING" << endl;
    consoleWriteULock.unlock();

    char* reply;
    while (true)
    {
        consoleWriteMutex.lock();
        cout << "Connected to server" << endl;
        consoleWriteMutex.unlock();

        recv(socketClient, &reply,sizeof(reply),0);
        if (reply == NULL)
        {
            lock_guard<mutex> consoleWriteLock(consoleWriteMutex);
            puts(reply);
            //cout << "No reply from server: " << reply << endl;
        }
        else
        {
            lock_guard<mutex> consoleWriteLock(consoleWriteMutex);
            cout << "Message recieved from server: " << gethostbyname(IP_ADDR) << endl;
        }
    }
}

void server()
{
    //Create server socket
    int socketServer = socket(AF_INET, SOCK_STREAM, NULL);
    sockaddr_in clientAdd; //Client address structure
    socklen_t sizeClientAdd =  sizeof(clientAdd);
    unique_lock<mutex> consoleWriteULock(consoleWriteMutex);
    string message = "Dont feel lonely you are connected";

    if (socketServer < 0)
    {
        cout << "Failed to create a server TCP socket" << endl;
    }
    else
    {
        clientAdd.sin_family = AF_INET;
        clientAdd.sin_port = htons(24008);
        clientAdd.sin_addr.s_addr = INADDR_ANY;
        cout << "Successfully created a server TCP socket" << endl;
    }

    consoleWriteULock.unlock();
    bind(socketServer,(sockaddr*) &clientAdd, sizeClientAdd);
    listen(socketServer,0);
    //serverReady.notify_one(); //Notify client

    while (true)
    {
        consoleWriteMutex.lock();
        cout << "Listening..." << endl;
        consoleWriteMutex.unlock();

        if (int sockNewConnection = accept(socketServer, (sockaddr*) &clientAdd, &sizeClientAdd))
        {
            lock_guard<mutex> consoleWriteLock(consoleWriteMutex);
            cout << "Accepted new connection" << endl;
            send(sockNewConnection, &message, sizeof(message), 0);
        }
        else
        {
            lock_guard<mutex> consoleWriteLock(consoleWriteMutex);
            cout << "Failed to accept incoming connection" << endl;
        }
    }
}

int main(int argc, const char * argv[])
{
    thread serverThread(server);
    thread clientThread(client);
    clientThread.join();
    serverThread.join();

    return 0;
}

Here 2 threads serverThread and clientThread are created and made to communicate concurrently. But the server() keeps blocking at accept() function . How do I solve this? I am using loopback IP to test my code; is that the reason this is not working? Please advice.

One error is sending and receiving std::string objects using C-style APIs send / receive . In:

string reply;
recv(socketClient, &reply, sizeof(reply),0);

the above does not read data into std::string , rather it corrupts the object.

See https://stackoverflow.com/a/20248772/412080 for samples of how to send and receive strings through sockets.


Another error is not checking the return codes of C-style functions.

If you add error checking, or run it under strace -ff this is what you may see:

bind(3, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EACCES (Permission denied)

Binding to ports 1-1023 normally requires special privileges. Try using a non-privileged port, eg 8080.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM