# Winsock2: Listen not working

## Recommended Posts

I'm currently building a Socket class in C++ which uses winsock 2. I started with simple initialize, connect and a listen functions which were working perfect. When I tried to make an asynchronous listen function it didn't work. I created a window and placed a WSAAsyncSelect() in the listen function, but the thing just wouldn't listen (literally). After that, my normal (blocking) listen function wouldn't work anymore either. I don't know why, because I had changed nothing in that part of the code. Even after removing all the things I added for the asynchronous listener it wouldn't work (I restored the old project with subversion). I even tried the example code from a tutorial which was working before but with no effect. The problem is that the listen function doesn't wait while it should. The windows firewall does ask me if I want to unblock the program but there is no listening connection popping up in netstat (there was before all of this). Does anyone know how to fix this? It's really hard to find anything on google since it doesn't give any error or something. For completeness, this is a part of the code:
	if (bind(m_socket, (LPSOCKADDR)&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR)
return;

listen(m_socket, 1)
If I replace those lines with this one it does connect properly:
	if (connect(m_socket, (LPSOCKADDR)&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR)
return false;

##### Share on other sites
listen() is not enough, you also need to accept(). And then it waits.

HTH
Chris

##### Share on other sites
Hmm okay. I'll try it and hope it works.

I'm wondering why it did work this way before though.

EDIT:
Wooow it's working :D. Thanks a lot.

But still, I was very very certain that I didn't include an accept() before. Weird..

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627715
• Total Posts
2978782

• 9
• 21
• 14
• 12
• 42