![]() |
| | #1 |
| Registered User Join Date: May 2006
Posts: 894
| accept() fails 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);
}
};
Thanks. |
| Desolation is offline | |
| | #2 |
| Registered User Join Date: May 2006
Posts: 894
| 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). |
| Desolation is offline | |
| | #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. |
| Quantum1024 is offline | |
| | #4 |
| Registered User Join Date: May 2006
Posts: 894
| 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. |
| Desolation is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Mac - File locking with fcntl() fails on shared volumes!? | idelovski | Linux Programming | 3 | 11-10-2008 07:37 PM |
| could not accept socket | Elkvis | Linux Programming | 3 | 02-26-2008 08:42 AM |
| D3D Device creation fails becasue I switched to VC++ 7 (.NET) | Boomba | Game Programming | 0 | 05-23-2005 11:39 AM |
| async Client/Server app, accept() stalls? | JaWiB | Networking/Device Communication | 14 | 01-31-2005 05:59 PM |
| Make accept() stop | b00l34n | Networking/Device Communication | 28 | 12-20-2004 06:50 PM |