MFC Sockets - Urgent!!!
-
The following is my code: CAsyncSocket socksrv; char buff[500]; ... UINT ClientThread(LPVOID lParam) { CSocket* s = new CSocket; s = (CSocket*) lParam; while (1) { s->Receive(buff, 500*sizeof(char), 0); if (strlen(buff)) { ::SetFocus(NULL); AfxMessageBox(buff); } } return 0; } UINT ServerThread(LPVOID lParam) { CSocket s; socksrv.Create(55599); socksrv.Listen(); while (1) { if (socksrv.Accept(s)) { AfxBeginThread(ClientThread, (LPVOID) &s); } } return 0; } void CServerDlg::OnStartServer() { AfxBeginThread(ServerThread, NULL); } When I execute the following, and try to connect the client, an exception occurs. Please tell me where my mistake is, and if you know what the multithreading sequence must be using CSocket or CAsyncSocket... Thank you! "Needless redundancy is the hobgoblin of software engineering." - Peter Darnell
-
The following is my code: CAsyncSocket socksrv; char buff[500]; ... UINT ClientThread(LPVOID lParam) { CSocket* s = new CSocket; s = (CSocket*) lParam; while (1) { s->Receive(buff, 500*sizeof(char), 0); if (strlen(buff)) { ::SetFocus(NULL); AfxMessageBox(buff); } } return 0; } UINT ServerThread(LPVOID lParam) { CSocket s; socksrv.Create(55599); socksrv.Listen(); while (1) { if (socksrv.Accept(s)) { AfxBeginThread(ClientThread, (LPVOID) &s); } } return 0; } void CServerDlg::OnStartServer() { AfxBeginThread(ServerThread, NULL); } When I execute the following, and try to connect the client, an exception occurs. Please tell me where my mistake is, and if you know what the multithreading sequence must be using CSocket or CAsyncSocket... Thank you! "Needless redundancy is the hobgoblin of software engineering." - Peter Darnell
Vladimir Georgiev wrote: if you know what the multithreading sequence must be using CSocket or CAsyncSocket Never use MFC socket classes in Multithreaded environment, otherwise you'll be between the devil and the deep sea. MFC not allows you to pass CSocket/CAsync objects directly from one thread to another. You have to use references for that purpose. Have a look here[^]