accept() fails

Started by
8 comments, last by White Scorpion 17 years, 11 months ago
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:
#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:
					static 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;
}





#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_




#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. 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). [Edited by - White Scorpion on May 16, 2006 9:15:18 PM]
Advertisement
Unless you have port forwarding set up, then accept() will never get the connection in the first place.
enum Bool { True, False, FileNotFound };
no bind listening socket to 127.0.0.1

comment out

Kuphryn

1. I made it so my router forwards the connection to my computer.
2. What does the above post tries to say ? O_o
Quote:Original post by White Scorpion
1. I made it so my router forwards the connection to my computer.
2. What does the above post tries to say ? O_o
You're binding your socket to 127.0.0.1 which is accessible only to your local computer.
What should I bind it to then ? Isn't it the hole point to bind the port on my computer ? I don't really know what bind() does to tell the truth.
Quote:Original post by White Scorpion
What should I bind it to then ? Isn't it the hole point to bind the port on my computer ? I don't really know what bind() does to tell the truth.
You want to bind it to a port, but not an address. bind() will let the socket only accept connections from the address it's bound to. You want to change:
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
to:
sin.sin_addr.s_addr = INADDR_ANY;

Annoying. It's still not working =/ Keep suggestions coming = /
What's the return value of WSAGetLastError() before the return 0 in ListeningThreadProc?
I finally found the problem. It's because of my ugly design. I re-designed it with a friend. The deal was that when connect() ended successfully, it called TerminateThread() on ListeningThreadProc() which would make it impossible for it to call accept(). My friend told me to make a separate server program and then a client program.

Thank you guys though.

This topic is closed to new replies.

Advertisement