Click here to Skip to main content
15,906,574 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Controlling CCriticalSection unlock order? Pin
led mike4-Jan-08 5:17
led mike4-Jan-08 5:17 
AnswerRe: Controlling CCriticalSection unlock order? Pin
peterchen4-Jan-08 6:30
peterchen4-Jan-08 6:30 
AnswerRe: Controlling CCriticalSection unlock order? Pin
Matthew Faithfull4-Jan-08 7:43
Matthew Faithfull4-Jan-08 7:43 
GeneralVideo libraries or controls Pin
dseverns54-Jan-08 4:59
professionaldseverns54-Jan-08 4:59 
GeneralRe: Video libraries or controls Pin
led mike4-Jan-08 5:22
led mike4-Jan-08 5:22 
GeneralRe: Video libraries or controls Pin
Mark Salsbery4-Jan-08 6:34
Mark Salsbery4-Jan-08 6:34 
QuestionRe: Video libraries or controls Pin
Hamid_RT4-Jan-08 18:12
Hamid_RT4-Jan-08 18:12 
GeneralMFC using WSAAsyncSelect to create asynchronous socket ... problem in creating hWnd [modified] Pin
drfankw4-Jan-08 4:38
drfankw4-Jan-08 4:38 
Hi~
Actually, I am a student, and working on a chat client program.

I would like to create asynchronous socket but not the default CAsynsocket one.
But I encounter a problem, that is how to create hWnd for WSAAsyncSelect.

I have read several articles in this site as well as some found in the internet.

here is code
<code>
define WM_WINSOCK 12345

SOCKET theSocket = socket(AF_INET, SOCK_DGRAM,0);

	if(theSocket == SOCKET_ERROR)
	{
		MessageBox("Failed socket()");

	}

		//Fill in the sockaddr_in struct

	bool sock_opt = TRUE;
	setsockopt(theSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&sock_opt, sizeof(sock_opt) );
	// i have to use socket reuse later


	HWND Window; 
	
	HINSTANCE m_hInstance = AfxGetInstanceHandle();

	Window = CreateWindow("Hidden_Winsock_Window",
			"Winsock Window",
			WS_OVERLAPPEDWINDOW,
			CW_USEDEFAULT,
			0,
			CW_USEDEFAULT,
			0,
			NULL,
			NULL,
			m_hInstance,
			NULL);

	// initialize the window class attributes.
	WNDCLASS *windowClass;
	windowClass = new WNDCLASS;
	windowClass->lpszClassName = "Hidden_Winsock_Window";
	windowClass->style = CS_HREDRAW | CS_VREDRAW;

// THE problem is here........
	windowClass->lpfnWndProc = MsgRouter;
///
	windowClass->cbClsExtra = 0;
	windowClass->cbWndExtra = 0;
	windowClass->hInstance = m_hInstance;
	windowClass->hIcon = NULL;
	windowClass->hCursor = NULL;
	windowClass->hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	windowClass->lpszMenuName = NULL;
	::RegisterClass(windowClass);

	WSAAsyncSelect(theSocket, Window, WM_WINSOCK, FD_READ | FD_WRITE | FD_CONNECT | FD_ACCEPT | FD_CLOSE);
	// binding for reuse. p57 windows socket net prog, actually optional for simple prog//
	bind(theSocket, (LPSOCKADDR)&clientInfo, sizeof(clientInfo));


///// other function
LRESULT CTest2Dlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if ((message == WM_WINSOCK))
	..... // a case loop here .. ///


	return CDialog::WindowProc(message, wParam, lParam);
}

LRESULT CALLBACK CTest2Dlg::MsgRouter(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	CTest2Dlg* pWindow = (CTest2Dlg*)GetWindowLong( hWnd, GWL_USERDATA );
	return pWindow->WindowProc(message, wParam, lParam);
}

</code>




Questions here~

Q1)
I dont know whether my method is corret. I would be much appreciated if you can give me advice~
1) call WSAasynselect
2) create hWnd window
3) route the WindowProc function (as it is a member function in a class)
4) register the window

Q2)
Is there any simple method to do the same task?

Q3)
I have found that the program does not work (although it is OK to be compiled and executed)
WindowProc seems cannot catch the message and go into the loop
Is there any errors in my code?

Thank you very much~~

modified on Friday, January 04, 2008 10:51:16 AM

GeneralRe: MFC using WSAAsyncSelect to create asynchronous socket ... problem in creating hWnd Pin
led mike4-Jan-08 5:08
led mike4-Jan-08 5:08 
GeneralRe: MFC using WSAAsyncSelect to create asynchronous socket ... problem in creating hWnd Pin
drfankw4-Jan-08 5:32
drfankw4-Jan-08 5:32 
GeneralRe: MFC using WSAAsyncSelect to create asynchronous socket ... problem in creating hWnd Pin
led mike4-Jan-08 5:51
led mike4-Jan-08 5:51 
GeneralRe: MFC using WSAAsyncSelect to create asynchronous socket ... problem in creating hWnd Pin
drfankw4-Jan-08 6:33
drfankw4-Jan-08 6:33 
GeneralRe: MFC using WSAAsyncSelect to create asynchronous socket ... problem in creating hWnd Pin
Mark Salsbery4-Jan-08 6:40
Mark Salsbery4-Jan-08 6:40 
GeneralRe: MFC using WSAAsyncSelect to create asynchronous socket ... problem in creating hWnd Pin
led mike4-Jan-08 9:54
led mike4-Jan-08 9:54 
GeneralRe: MFC using WSAAsyncSelect to create asynchronous socket ... problem in creating hWnd Pin
Mark Salsbery4-Jan-08 10:08
Mark Salsbery4-Jan-08 10:08 
GeneralRe: MFC using WSAAsyncSelect to create asynchronous socket ... problem in creating hWnd Pin
led mike4-Jan-08 11:47
led mike4-Jan-08 11:47 
GeneralRe: MFC using WSAAsyncSelect to create asynchronous socket ... problem in creating hWnd Pin
Mark Salsbery4-Jan-08 13:30
Mark Salsbery4-Jan-08 13:30 
GeneralRe: MFC using WSAAsyncSelect to create asynchronous socket ... problem in creating hWnd Pin
led mike7-Jan-08 4:18
led mike7-Jan-08 4:18 
GeneralRe: MFC using WSAAsyncSelect to create asynchronous socket ... problem in creating hWnd Pin
Mark Salsbery7-Jan-08 6:14
Mark Salsbery7-Jan-08 6:14 
QuestionSTL sort throw exception? Pin
George_George4-Jan-08 4:06
George_George4-Jan-08 4:06 
AnswerRe: STL sort throw exception? [modified] Pin
Maxwell Chen4-Jan-08 6:42
Maxwell Chen4-Jan-08 6:42 
GeneralRe: STL sort throw exception? Pin
George_George4-Jan-08 21:59
George_George4-Jan-08 21:59 
AnswerRe: STL sort throw exception? Pin
Nemanja Trifunovic4-Jan-08 7:34
Nemanja Trifunovic4-Jan-08 7:34 
GeneralRe: STL sort throw exception? Pin
George_George4-Jan-08 21:53
George_George4-Jan-08 21:53 
AnswerRe: STL sort throw exception? Pin
Eytukan4-Jan-08 22:37
Eytukan4-Jan-08 22:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.