0

I have a program that serves both as client and as server without multi-threading (as far as I know accept should let the program continue up until a certain connection is occurs).

The thing is, that my friend has a very similar program (not multithreaded) that also serves as both client AND server and it totally works, I'm trying to accomplish the same thing and accept() stops the program.

The code is as the following:

//main.cpp
#include <iostream>
#include "Client.h"
#include "Server.h"
#pragma comment(lib, "Ws2_32.lib")
int main()
{
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2,2), &wsaData);
    Server s(6666);
    Client c("127.0.0.1", 6666);
    cout << "Done";

    WSACleanup();
    return 0;
}

Server.cpp (two variables, SOCKET _socket and struct sockaddr_in _details):

Server::Server(unsigned short Port) : _socket(0)
{
    this->_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (_socket < 0)
        throw "Invalid socket";

    ZeroMemory(&this->_details, sizeof(this->_details));
    this->_details.sin_family = AF_INET;
    this->_details.sin_port = htons(Port);
    this->_details.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

    if (bind(this->_socket, (const struct sockaddr*)&this->_details, sizeof(this->_details)) != 0)
    {
        throw "Bind Unsuccessful";
    }

    this->AcceptConnections();
}

void Server::AcceptConnections()
{
    if (listen(this->_socket, SOMAXCONN) != 0)
        throw "Listen Unsuccessful";
    void* buf = NULL;
    string ans("Accepted");
    int client;
    struct sockaddr_in client_addr;
    int addrlen = sizeof(client_addr);

    client = accept(this->_socket, (struct sockaddr*)&client_addr, &addrlen);
    /*THIS IS WHERE THE PROGRAM STOPS... AWAITING CONNECTIONS*/

    //NEVER REACHING THE CODE HERE
    int recvBytes = this->Receive(buf, MY_MAX_LEN);
    if (recvBytes <= 0)
    {
        throw "Client disconnected";
    }
    this->Send((void*)ans.c_str(), ans.length());

    closesocket(client);
    closesocket(this->_socket);
}

And client.cpp is irrelevant as it doesn't even encounter its code.

Why does this happen? How come my friend has a code with no multi-threading that has both client and server. By the way, Send and Receive are functions implemented by me.

  • 4
    What? 'as far as I know accept should let the program continue up until a certain connection is occurs' - err.. no. Accept() is a blocking call. It returns when a client connects. – Martin James Mar 13 '15 at 11:48
  • 1
    Also, it does not stop the 'program', it blocks the calling thread only. – Martin James Mar 13 '15 at 11:51
  • @MartinJames So how come that my friend's program acts multi-threaded? – user3147306 Mar 13 '15 at 12:02
  • It's likely a so-called 'non-blocking' design. That means it blocks on epoll() or select() instead of accept(). – Martin James Mar 13 '15 at 12:03
  • You can set sockets into a non-blocking mode, and/or you can use functions like select and poll to know that a client has attempted to connect, from which you can guess that a call to `accept()` won't block. I say guess because there's a race condition - if the client connection attempt happens to have failed completely before your thread gets to the `accept()` call, you'll still block if the socket's not non-blocking. – Tony Delroy Mar 13 '15 at 12:07
  • @TonyD And how do I do that? Could you link me to somewhere with an example code or a tutorial? – user3147306 Mar 13 '15 at 12:09
  • 1
    More generally... when people have issues with their programs, we expect them to post the code here so claims about it can be verified and observations confirmed and explained. Talking about your recollection/understanding of your friend's program without listing relevant code is a step in the other direction. If you want to know how it works - why not have a look or ask him/her. – Tony Delroy Mar 13 '15 at 12:11
  • 1
    @user3147306: SO's not a code writing service. Armed with the knowledge that there are non-blocking sockets, a 5 second google search turns up e.g. [this](http://stackoverflow.com/questions/1543466/how-do-i-change-a-tcp-socket-to-be-non-blocking), which is adequate. And links to books, tutorials etc are I believe off topic too. – Tony Delroy Mar 13 '15 at 12:13
  • If you do your best to find examples / tutorials etc. and write some updated code that you can't get working and don't understand why, then you have grounds for another question.... – Tony Delroy Mar 13 '15 at 12:18

0 Answers0