Thread: accept() fails

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    903

    accept() fails

    Hey guys. I'm working on my app right now and there's something not working. I tried connecting to one of my friend's computer -- to whom I gave the application -- but the message box test located in ListeningThreadProc() never popped up which means that accept() failed. Here's the code:
    Code:
    #include <windows.h>
    #include "Scintilla.h"
    #include "SciLexer.h"
    #include "tabctrl.h"
    #include "mdi_child.h"
    #include "network.h"
    #include "resource.h"
    
    HWND client;
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    BOOL CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
    	OC_Interface::RegisterMDIChildClass();
    	OC_Net::Initialize();
    
    	WNDCLASSEX wc;
    	ZeroMemory(&wc, sizeof(WNDCLASSEX));
    	wc.cbSize			= sizeof(WNDCLASSEX);
    	wc.hbrBackground	= (HBRUSH)(COLOR_WINDOW + 1);
    	wc.hCursor			= LoadCursor(0, IDC_ARROW);
    	wc.hIcon			= LoadIcon(0, IDI_WINLOGO);
    	wc.lpfnWndProc		= WndProc;
    	wc.lpszClassName	= "Online Coder";
    	if(!RegisterClassEx(&wc)) MessageBox(0, "Couldn't register class", 0, 0);
    
    	HWND hwnd = CreateWindow("Online Coder", "", WS_OVERLAPPEDWINDOW, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, 0);
    
    	UpdateWindow(hwnd);
    	ShowWindow(hwnd, true);
    
    	DialogBox(0, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, DlgProc);
    
    	MSG msg;
    	while(true) {
    		while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE) != 0) {
    			if(msg.message == WM_QUIT) break;
    			if(!TranslateMDISysAccel(client, &msg)) {
    				TranslateMessage(&msg);
    				DispatchMessage(&msg);
    			}
    		}
    		if(msg.message == WM_QUIT) break;
    	}
    
    	return 0;
    }
    
    HWND whatever;
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wprm, LPARAM lprm) {
    	switch(msg) {
    	case WM_CREATE:
    		{
    			CLIENTCREATESTRUCT ccs;
    			ZeroMemory(&ccs, sizeof(CLIENTCREATESTRUCT));
    			ccs.idFirstChild = MDIFIRSTCHILD;
    
    			client = CreateWindowEx(0, "mdiclient", 0, WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
    				CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hwnd, 0, 0, (LPVOID)&ccs);
    
    			OC_Interface::CreateNewMDIChild(client, "whatever");
    		}
    		break;
    	case WM_DESTROY:
    		OC_Interface::UnregisterMDIChildClass();
    		PostQuitMessage(0);
    		break;
    	default:
    		return DefFrameProc(hwnd, client, msg, wprm, lprm);
    	}
    	return 0;
    }
    
    BOOL CALLBACK DlgProc(HWND hdlg, UINT msg, WPARAM wprm, LPARAM lprm) {
    	switch(msg) {
    		case WM_INITDIALOG:
    			return TRUE;
    		case WM_COMMAND:
    			switch(LOWORD(wprm)) {
    				case IDOK:
    					char temp[20];
    					GetDlgItemText(hdlg, IDC_IPBOX, temp, 20);
    					OC_Net::ConnectToIP(temp);
    					EndDialog(hdlg, 0);
    					return TRUE;
    			}
    			return TRUE;
    		default:
    			return FALSE;
    	}
    	return FALSE;
    }
    Code:
    #ifndef _NETWORK_H_INCLUDED_
    #define _NETWORK_H_INCLUDED_
    
    #define NET_DEFAULTPORT 1234
    #define NET_ERR_NOTCONNECTEd 0x
    
    #include <winsock.h>
    #include <windows.h>
    
    #pragma comment(lib, "wsock32.lib")
    
    namespace OC_Net {
    	DWORD WINAPI ListeningThreadProc(void*);
    	void ConnectToIP(const char*);
    	void Initialize();
    
    	extern SOCKET ConnSocket;
    	extern HANDLE ListeningThread;
    };
    
    #endif // _NETWORK_H_INCLUDED_
    Code:
    #include "network.h"
    
    namespace OC_Net {
    	SOCKET ConnSocket = INVALID_SOCKET;
    	HANDLE ListeningThread = 0;
    
    	DWORD WINAPI ListeningThreadProc(void* unused) {
    		sockaddr_in sin;
    		ZeroMemory(&sin, sizeof(sockaddr_in));
    		sin.sin_addr.s_addr	= inet_addr("127.0.0.1");
    		sin.sin_family	= AF_INET;
    		sin.sin_port	= htons(NET_DEFAULTPORT);
    		
    		SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    		if(s != INVALID_SOCKET) {
    			bind(s, (sockaddr*)&sin, sizeof(sin));
    			if(listen(s, SOMAXCONN) != SOCKET_ERROR) {
    				int add_len = sizeof(sin);
    				while(ConnSocket == INVALID_SOCKET)
    					ConnSocket = accept(s, NULL, NULL);
    				MessageBox(0,"ok",0,0);
    			}
    		}
    		return 0;
    	}
    
    	void ConnectToIP(const char* ip_addr) {
    		if(ConnSocket == INVALID_SOCKET) {
    			DWORD exit_code = 0;
    			GetExitCodeThread(ListeningThread, &exit_code);
    			if(exit_code == STILL_ACTIVE) {
    				sockaddr_in sin;
    				ZeroMemory(&sin, sizeof(sockaddr_in));
    				sin.sin_addr.s_addr	= inet_addr(ip_addr);
    				sin.sin_family	= AF_INET;
    				sin.sin_port	= htons(NET_DEFAULTPORT);
    				
    				SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    				if(s != INVALID_SOCKET) {
    					int err = connect(s, (sockaddr*)&sin, sizeof(sin));
    					if(err != SOCKET_ERROR) {
    						ConnSocket = s;
    						TerminateThread(ListeningThread, 0);
    					}
    				}
    			}
    		}
    	}
    
    	void Initialize( ) {
    		WSADATA data;
    		WSAStartup(MAKEWORD(1,1), &data);
    
    		ListeningThread = CreateThread(0, 0, ListeningThreadProc, 0, 0, 0);
    	}
    };
    All return values are fine so I assume that accept() is failing, is the fact that I am under a router the explication to all of this ?

    Thanks.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    PS: Further info. The value returned by GetLastError() tells me that the connection timed out. Any idea why this could happen ? (error code 10060 if you want to see for yourself).

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    I'll assume you mean WSAGetLastError as that is the function for geting socket related errors.

    Your connection attempts might not be reaching the server, this would produce a timeout. If the server is located behind a router then the router may need to have port forwarding activated.

    When the router recieves a connection request there could be multiple hosts on it's network, it needs to be told which host and port to direct the connection request to.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    I tried redirecting the port to my computer to no avail. It didn't change the results. I still get WSAETIMEDOUT or something like this whenever someone tries to connect on my computer and vice versa.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mac - File locking with fcntl() fails on shared volumes!?
    By idelovski in forum Linux Programming
    Replies: 3
    Last Post: 11-10-2008, 07:37 PM
  2. could not accept socket
    By Elkvis in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2008, 08:42 AM
  3. Replies: 0
    Last Post: 05-23-2005, 11:39 AM
  4. async Client/Server app, accept() stalls?
    By JaWiB in forum Networking/Device Communication
    Replies: 14
    Last Post: 01-31-2005, 05:59 PM
  5. Make accept() stop
    By b00l34n in forum Networking/Device Communication
    Replies: 28
    Last Post: 12-20-2004, 06:50 PM