multithreaded code does not go into do/while loop

Hi,
I recently started working with threads and sockets in order to make a simple tchat server. When I run my code, it prints :

1
2
3
4
5
6
7
1
2
4
6
8
10
12


Where as, it should print those numbers along with the number 13 that is in the loop. The only cause that I can come up with is that I am not allowed to join the accept thread to the listen thread.
Here's my 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#undef UNICODE

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <thread>

// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")

//Global values
//I put them as global values in order to get the server up and running.
//I will try to pass them as params later on
int iResult;
struct addrinfo *result = NULL;
struct addrinfo hints;


//Prototypes
int listen(SOCKET ListenSocket, SOCKET ClientSocket);
int accept(SOCKET ListenSocket, SOCKET ClientSocket);

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "1016"

int main()
{
	std::cout << 1 << std::endl;
	WSADATA wsaData;

	SOCKET ListenSocket = INVALID_SOCKET;
	SOCKET ClientSocket = INVALID_SOCKET;

	int iSendResult;
	char recvbuf[DEFAULT_BUFLEN];
	int recvbuflen = DEFAULT_BUFLEN;

	// Initialize Winsock
	std::cout << 2 << std::endl;
	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != 0) {
		std::cout << 3 << std::endl;
		printf("WSAStartup failed with error: %d\n", iResult);
		return 1;
	}

	ZeroMemory(&hints, sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_flags = AI_PASSIVE;

	std::thread ListenThread{ [ListenSocket, ClientSocket](){listen(ListenSocket, ClientSocket); } };
	ListenThread.join();

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

			// Echo the buffer back to the sender
			std::cout << 16 << std::endl;
			iSendResult = send(ClientSocket, recvbuf, iResult, 0);
			if (iSendResult == SOCKET_ERROR) {
				std::cout << 17 << std::endl;
				printf("send failed with error: %d\n", WSAGetLastError());
				closesocket(ClientSocket);
				WSACleanup();
				return 1;
			}
			printf("Bytes sent: %d\n", iSendResult);
		}
		else if (iResult == 0) {
			std::cout << 18 << std::endl;
			printf("Connection closing...\n");
		}
		else {
			std::cout << 19 << std::endl;
			printf("recv failed with error: %d\n", WSAGetLastError());
			closesocket(ClientSocket);
			WSACleanup();
			return 1;
		}

	} while (iResult > 0);

	// shutdown the connection since we're done
	std::cout << 20 << std::endl;
	iResult = shutdown(ClientSocket, SD_SEND);
	if (iResult == SOCKET_ERROR) {
		std::cout << 21 << std::endl;
		printf("shutdown failed with error: %d\n", WSAGetLastError());
		closesocket(ClientSocket);
		WSACleanup();
		return 1;
	}

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

	return 0;
}

int listen(SOCKET ListenSocket, SOCKET ClientSocket)
{
	// Resolve the server address and port
	std::cout << 4 << std::endl;
	iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
	if (iResult != 0) {
		std::cout << 5 << std::endl;
		printf("getaddrinfo failed with error: %d\n", iResult);
		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;
	}

	// 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::thread AcceptThread{ [ListenSocket, ClientSocket](){accept(ListenSocket, ClientSocket); } };
	AcceptThread.join();

}

int accept(SOCKET ListenSocket, SOCKET ClientSocket)
{
	// Accept a client socket
	std::cout << 12 << std::endl;
	do {
		ClientSocket = accept(ListenSocket, NULL, NULL);
		std::cout << 13 << std::endl;
		if (ClientSocket == INVALID_SOCKET)
		{
			printf("accept failed with error: %d\n", WSAGetLastError());
		}
	}
	while (ClientSocket == INVALID_SOCKET);

	return 0;
}
Topic archived. No new replies allowed.