Advertisement Jump to content


This topic is now archived and is closed to further replies.


the accept(...) kills the app

This topic is 5827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi. the latest days i''ve been coding on a socket application and everything''s fine, except that my accept(...) function kills the app. The init part of my winsock app:
WSADATA	winsock_info; ZeroMemory(&winsock_info, sizeof(WSADATA));
sockaddr_in socket_settings; ZeroMemory(&socket_settings, sizeof(sockaddr_in));
SOCKET m_Socket; ZeroMemory(&m_Socket, sizeof(SOCKET));

// initialize winsock //

if(WSAStartup(0x0202, &winsock_info))
     MessageBox(NULL, "failed to init winsock", "Winsock Error", MB_OK);

// test winsock version //

if(winsock_info.wVersion != 0x0202)
     MessageBox(NULL, "wrong winsock version", "Winsock Error", MB_OK);

// create the socket //

     MessageBox(NULL, "failed to create socket", "Winsock Error", MB_OK);

// settings for the connection //

socket_settings.sin_family	= AF_INET;
socket_settings.sin_port	= htons(1337);
socket_settings.sin_addr.s_addr =  htonl(INADDR_ANY);

// bind the socket //

if(SOCKET_ERROR == bind(m_Socket, reinterpret_cast<struct sockaddr *>(&socket_settings), sizeof(socket_settings)))
     MessageBox(NULL, "failed to bind socket", "Winsock Error", MB_OK);

// enable listening //

if(SOCKET_ERROR == listen(m_Socket, 8))
     MessageBox(NULL, "failed to start listening", "Winsock Error", MB_OK));
the update function of my winsock app:
SOCKET connection; ZeroMemory(&connection, sizeof(SOCKET));
sockaddr_in user; ZeroMemory(&user, sizeof(sockaddr_in));
 int size = sizeof(user);
 if(SOCKET_ERROR == (connection = accept(m_Socket, reinterpret_cast<struct sockaddr *>(&user), &size))) // OBSERVE THIS LINE

      MessageBox(NULL, "failed to accept connection", "Winsock Error", MB_OK) 
my app stops responding och the line i noted with "OBSERVER THIS LINE".. can someone figure ut the problem?

Share this post

Link to post
Share on other sites
I cannot spot anything in particular. Are you sure it does not hit that line before any attempt to connect.

Hm, I thought I had an idea about what it could be, which was the reason I replied, but that was not it, so I''ll give you my "standard" code snippet so you can compare:

// Create our socket (not used for communication
Log(LOG_CRITICAL, "Failed to create socket");
return false;

// Bind() the socket
struct sockaddr_in addr;
memset(&addr, ''\0'', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(this->GetListenPort());
if(bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) {
Log(LOG_CRITICAL, "Failed to bind socket");
return false;

// Listen()
if(listen(sock, SOMAXCONN) == SOCKET_ERROR) {
Log(LOG_CRITICAL, "Failed to place socket in listen state");
return false;

// Set system to alert us of incoming connections
if(WSAAsyncSelect(sock, win, WM_WSAASYNC, FD_ACCEPT) == SOCKET_ERROR) {
Log(LOG_CRITICAL, "Failed to select socket for FD_ACCEPT");
return false;
Log(LOG_DEBUG, "Successfully setup socket");
return true;
Then later on, when accepting:

if((s = accept(sock, 0, 0)) == INVALID_SOCKET) {
Log(LOG_ERROR, "Failed to accept incoming connection");
Which works great.

Share this post

Link to post
Share on other sites
CWizard: your there is one thing in your code that wasn''t in mine (but now it is though) i had forgotten the WSAAsyncSelect

thank you!

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!