C++ Multiconnection basic console tchat server Winsock2 Second client not receiving anything

Hey guys,
I was wondering if you guys could go check out my post on stack overflow because nobody is answering and I'm really stuck. I know your not really suppose to ask things like this but I'm really stuck.
Here's the link :
http://stackoverflow.com/questions/32443877/c-multiconnection-basic-console-tchat-server-winsock2-second-client-not-receiv

thanks,

mindoo
What you are doing is accept five different connections. How do the clients know what port to connect to?

So five different ports and if five clients connected to this ports no further connection is accepted.

The solution is like they told you: Make a loop and accept the same address again.

Otherwise you will not see "12"/"13" again apart from the five accept threads.
Thanks for your answer.
What do you think about my updated code ? What did I do wrong ?
Well:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
...
SOCKET ClientSocket[5];
...
int main()
{
...
    listen(ListenSocket);

    return 0;
}

int listen(SOCKET ListenSocket)
{
...
    for (int i = 1; i < 5; i++)
    {
        std::cout << 10 << std::endl;
        iResult = listen(ListenSocket, SOMAXCONN);
        if (iResult == SOCKET_ERROR) {
            std::cout << 11 << std::endl;
            printf("listen failed with error: %d\n", WSAGetLastError());
            closesocket(ListenSocket);
            WSACleanup();
            return -1;
        }

        std::cout << 23 << std::endl;

    bool is_accepting = true;
    while(is_accepting)
    {
        accept(ListenSocket);
    }
    for (auto it : thread_vec) it->join();
    return 0;
}

int accept(SOCKET ListenSocket)
{
    // Accept a client socket
    std::cout << 12 << std::endl;
    std::cout << 13 << std::endl;

    SOCKET client_socket = accept(ListenSocket, NULL, NULL);
    if (client_socket == INVALID_SOCKET)
    {
        printf("accept failed with error: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }
    else{
        std::cout << "Client  connected" << std::endl;
    }

    sendReceiveThread[currentNumClients] = new std::thread([client_socket](){sendReceive(client_socket); }); // Note: Passing client_socket
    (*sendReceiveThread[currentNumClients]).join();
    delete sendReceiveThread[currentNumClients];

    return 0;
}

int sendReceive(SOCKET client_socket) // Note
{
    int currentNumClients = numClients;
    int iSendResult;
    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;

    // Receive until the peer shuts down the connection
    while(true)
    {
        std::cout << 14 << std::endl;
        iResult = recv(client_socket, recvbuf, recvbuflen, 0);
        std::cout << iResult << std::endl;
        if (iResult > 0) {
            std::cout << 15 << std::endl;
            printf("Bytes received: %d\n", iResult);

            // Echo the buffer back to the clients
            std::cout << 16 << std::endl;
            for (int i = 1; i <= numClients; i++)
            {
                iSendResult = send(client_socket, recvbuf, iResult, 0); // Note
                if (iSendResult == SOCKET_ERROR) {
                    std::cout << 17 << std::endl;
                    printf("send failed with error: %d\n", WSAGetLastError());
                    closesocket(client_socket); // Note
                    WSACleanup();
                    return 1;
                }
                printf("Bytes sent: %d\n", iSendResult);
            }
        }
        else if (iResult == 0) {
            std::cout << 18 << std::endl;
            printf("Connection closing...\n");
            break;
        }
        else {
            std::cout << 19 << std::endl;
            printf("recv failed with error: %d\n", WSAGetLastError());
            std::cout << "On client #" << currentNumClients << std::endl;
            break;
        }

    }

    iResult = shutdownFunction(client_socket); // Note

    std::cout << 22 << std::endl;
    // cleanup
    closesocket(client_socket); // Note
    WSACleanup();

    return 0;
}

...
Note: Not tested
Ok so I tried doing what you told me to do but my code crashes.
It says Debug Error, R6010 abort() has been called.
I tried debugging but what it seems to do is continue after the listen function even though nothing tried to connet to it. Then it arrives at the accept part and crashes.

Here is my listen function code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
int listen(SOCKET ListenSocket)
{
	int numPort = 1;
	std::vector<std::thread*> thread_vec;
		
	iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
	if (iResult != 0)
	{
		std::cout << 5 << std::endl;
		std::cout << "getaddrinfo failed with error : " << iResult << std::endl;
		WSACleanup();
		return -1;
	}

	// Create a SOCKET for connecting to server
	std::cout << 6 << std::endl;
	ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
	if (ListenSocket == INVALID_SOCKET) {
		std::cout << 7 << std::endl;
		printf("socket failed with error: %ld\n", WSAGetLastError());
		freeaddrinfo(result);
		WSACleanup();
		return -1;
	}

	char optval = 1;
	iResult = ::setsockopt(ListenSocket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
	if (iResult == SOCKET_ERROR) {
		std::cout << 9 << std::endl;
		printf("setsockopt failed with error: %d\n", WSAGetLastError());
		return -1;
	}

	// Setup the TCP listening socket
	std::cout << 8 << std::endl;
	iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
	if (iResult == SOCKET_ERROR) {
		std::cout << 9 << std::endl;
		printf("bind failed with error: %d\n", WSAGetLastError());
		freeaddrinfo(result);
		closesocket(ListenSocket);
		WSACleanup();
		return -1;
	}

	freeaddrinfo(result);

	std::cout << 10 << std::endl;
	iResult = listen(ListenSocket, SOMAXCONN);
	if (iResult == SOCKET_ERROR) {
		std::cout << 11 << std::endl;
		printf("listen failed with error: %d\n", WSAGetLastError());
		closesocket(ListenSocket);
		WSACleanup();
		return -1;
	}

	std::cout << 23 << std::endl;

	static std::thread AcceptThread{ [ListenSocket](){accept(ListenSocket); } };
	thread_vec.push_back(&AcceptThread);
	return 0;
}

Do you have any idea of why it is crashing ?
Do you have any idea of why it is crashing ?
Yes: the programm ends while waiting for accept.

Do you know what line 60 does?
What is line 61 good for?
I have absolutely no clue...
It was a quick fix by a friend for a bug I had previously.
Is that what is making my code crash ?
Is that what is making my code crash ?
As I said:
Yes: the programm ends while waiting for accept.


Line 60 makes accept(...) process parallel. listen(...) ends and so does main(...) while the accept thread is starting. You should really read more about thread and their use.

What you may do is:

- Asking the user whether the program should stop.
- When stopping: Set a flag for no more accept (ex.: is_accepting = false)
- Connect as a client would do.
- AcceptThread.join();

Line 61 is useless. Or explain the use of thread_vec.
Topic archived. No new replies allowed.