Unable to create a server
-
hi, i was creating a server from CSocket but whenever the client connects to the server, my server dialog box will automatically close itself. i wonder of there is anything wrong with my code? i have a mysocket class, below is part of the code: void mysocket::OnAccept(int nErrorCode) { if ( nErrorCode == 0 ) ( ( CMultiAPIDlg* ) m_pWnd ) -> onaccept( ) ; CSocket::OnAccept(nErrorCode); } in my code for the dialog box, when user press connect, the following function is evoked: void CMultiAPIDlg::OnBConnect() { m_connectsocket.Create ( 0 ) ; m_connectsocket.Connect ( m_server, m_port) ; } and the onaccept function is: void CMultiAPIDlg::onaccept() { AfxMessageBox("Connected"); m_listensocket.Accept ( m_connectsocket ) ; } somehow, when the client connects to the server, the server dialog box will automatically close itself... can anyone help?
-
hi, i was creating a server from CSocket but whenever the client connects to the server, my server dialog box will automatically close itself. i wonder of there is anything wrong with my code? i have a mysocket class, below is part of the code: void mysocket::OnAccept(int nErrorCode) { if ( nErrorCode == 0 ) ( ( CMultiAPIDlg* ) m_pWnd ) -> onaccept( ) ; CSocket::OnAccept(nErrorCode); } in my code for the dialog box, when user press connect, the following function is evoked: void CMultiAPIDlg::OnBConnect() { m_connectsocket.Create ( 0 ) ; m_connectsocket.Connect ( m_server, m_port) ; } and the onaccept function is: void CMultiAPIDlg::onaccept() { AfxMessageBox("Connected"); m_listensocket.Accept ( m_connectsocket ) ; } somehow, when the client connects to the server, the server dialog box will automatically close itself... can anyone help?
honestly, i didnt work with sockets via MFC, but in DOS serverside app this looks like following call-sequence: 1. bind 2. listen 3. accept 4. recv/send i think it wouldn't differ enough with an MFC algorythm... 4apai There're no impossible tasks. There're tasks that required infinite period of execution time.
-
hi, i was creating a server from CSocket but whenever the client connects to the server, my server dialog box will automatically close itself. i wonder of there is anything wrong with my code? i have a mysocket class, below is part of the code: void mysocket::OnAccept(int nErrorCode) { if ( nErrorCode == 0 ) ( ( CMultiAPIDlg* ) m_pWnd ) -> onaccept( ) ; CSocket::OnAccept(nErrorCode); } in my code for the dialog box, when user press connect, the following function is evoked: void CMultiAPIDlg::OnBConnect() { m_connectsocket.Create ( 0 ) ; m_connectsocket.Connect ( m_server, m_port) ; } and the onaccept function is: void CMultiAPIDlg::onaccept() { AfxMessageBox("Connected"); m_listensocket.Accept ( m_connectsocket ) ; } somehow, when the client connects to the server, the server dialog box will automatically close itself... can anyone help?
Okay I'm going to make an attempt to answer this. Anonymous wrote: void CMultiAPIDlg::OnBConnect() { m_connectsocket.Create ( 0 ) ; m_connectsocket.Connect ( m_server, m_port) ; } This should be a create then a listen. Like this
void CMultiAPIDlg::OnBConnect() { m_listensocket.Create ( m_port ) ; m_listensocket.Listen ( ) ; }
The Create is where you specifiy what port you want your server listening on. Next you must listen for a connection. Also to make it easier switch your class reference around to reflect your app. For servers they listen, for clients they connect. So in the code above I switched the references around. Now just accept your connection just like you have it. One last suggestion in your mysocket class, in the onconnect, I would do something like this:if (nErrorCode != 0) { switch( nErrorCode ) { case WSAEADDRINUSE: AfxMessageBox("The specified address is already in use.\n"); break; case WSAEADDRNOTAVAIL: AfxMessageBox("The specified address is not available from the local machine.\n"); break; case WSAEAFNOSUPPORT: AfxMessageBox("Addresses in the specified family cannot be used with this socket.\n"); break; case WSAECONNREFUSED: AfxMessageBox("The attempt to connect was forcefully rejected.\n"); break; case WSAEDESTADDRREQ: AfxMessageBox("A destination address is required.\n"); break; case WSAEFAULT: AfxMessageBox("The lpSockAddrLen argument is incorrect.\n"); break; case WSAEINVAL: AfxMessageBox("The socket is already bound to an address.\n"); break; case WSAEISCONN: AfxMessageBox("The socket is already connected.\n"); break; case WSAEMFILE: AfxMessageBox("No more file descriptors are available.\n"); break; case WSAENETUNREACH: AfxMessageBox("The network cannot be reached from this host at this time.\n"); break; case WSAENOBUFS: AfxMessageBox("No buffer space is available. The socket cannot be connected.\n"); break; case WSAENOTCONN: AfxMessageBox("The socket is not connected.\n"); break; case WSAENOTSOCK: AfxMessageBox("The descriptor is a file, not a socket.\n"); break; case WSAETIMEDOUT: AfxMessageBox("The attempt to connect timed out without establishing a connection. \n"); break; default: TCHAR szError[256]; wsprintf(szError, "OnConnect error: %d", nErrorCode); AfxMessageBox(szError); break; }
That way you k -
hi, i was creating a server from CSocket but whenever the client connects to the server, my server dialog box will automatically close itself. i wonder of there is anything wrong with my code? i have a mysocket class, below is part of the code: void mysocket::OnAccept(int nErrorCode) { if ( nErrorCode == 0 ) ( ( CMultiAPIDlg* ) m_pWnd ) -> onaccept( ) ; CSocket::OnAccept(nErrorCode); } in my code for the dialog box, when user press connect, the following function is evoked: void CMultiAPIDlg::OnBConnect() { m_connectsocket.Create ( 0 ) ; m_connectsocket.Connect ( m_server, m_port) ; } and the onaccept function is: void CMultiAPIDlg::onaccept() { AfxMessageBox("Connected"); m_listensocket.Accept ( m_connectsocket ) ; } somehow, when the client connects to the server, the server dialog box will automatically close itself... can anyone help?
You get no error messages? What about in debug mode do you get an unhandled exception or assertion failure? Anonymous wrote: AfxMessageBox("Connected"); Does that happen before the dialog closes? Is m_listensocket.Listen() called in a worker thread? If so then, m_pWnd may not be valid in the mysock::OnAccept() member. MFC Window objects should not be passed accross threads. You can find Knowledgebase articles on this subject on MSDN. At runtime do you have two instances of CMultiAPIDlg, one running as server and the second as the client?
-- signature under construction --
-pete
-
You get no error messages? What about in debug mode do you get an unhandled exception or assertion failure? Anonymous wrote: AfxMessageBox("Connected"); Does that happen before the dialog closes? Is m_listensocket.Listen() called in a worker thread? If so then, m_pWnd may not be valid in the mysock::OnAccept() member. MFC Window objects should not be passed accross threads. You can find Knowledgebase articles on this subject on MSDN. At runtime do you have two instances of CMultiAPIDlg, one running as server and the second as the client?
-- signature under construction --
-pete
hi! i'm so sorry i've wrongly quoted the client section of my code. the actual code of my server is void CMultiAPIDlg::OnBConnect() { m_listensocket.Create ( m_port ); m_listensocket.Listen( ) ; AfxMessageBox("Listening"); } when the server is connected, the message "Listening" appears. hence, there's no problem with this part. but when the client trys to connect, the following code was not evoked at all since i did not get the "Connected" message. the dialog box simply closes... void mysocket::OnAccept(int nErrorCode) { AfxMessageBox("Connected"); if ( nErrorCode == 0 ) ( ( CMultiAPIDlg* ) m_pWnd ) -> onaccept( ) ; CSocket::OnAccept(nErrorCode); } and i do not have 2 instance of CMultiAPIDlg running cos i've another program named SOCK_CIENT working as a client ... :((
-
hi! i'm so sorry i've wrongly quoted the client section of my code. the actual code of my server is void CMultiAPIDlg::OnBConnect() { m_listensocket.Create ( m_port ); m_listensocket.Listen( ) ; AfxMessageBox("Listening"); } when the server is connected, the message "Listening" appears. hence, there's no problem with this part. but when the client trys to connect, the following code was not evoked at all since i did not get the "Connected" message. the dialog box simply closes... void mysocket::OnAccept(int nErrorCode) { AfxMessageBox("Connected"); if ( nErrorCode == 0 ) ( ( CMultiAPIDlg* ) m_pWnd ) -> onaccept( ) ; CSocket::OnAccept(nErrorCode); } and i do not have 2 instance of CMultiAPIDlg running cos i've another program named SOCK_CIENT working as a client ... :((
-
hi! i'm so sorry i've wrongly quoted the client section of my code. the actual code of my server is void CMultiAPIDlg::OnBConnect() { m_listensocket.Create ( m_port ); m_listensocket.Listen( ) ; AfxMessageBox("Listening"); } when the server is connected, the message "Listening" appears. hence, there's no problem with this part. but when the client trys to connect, the following code was not evoked at all since i did not get the "Connected" message. the dialog box simply closes... void mysocket::OnAccept(int nErrorCode) { AfxMessageBox("Connected"); if ( nErrorCode == 0 ) ( ( CMultiAPIDlg* ) m_pWnd ) -> onaccept( ) ; CSocket::OnAccept(nErrorCode); } and i do not have 2 instance of CMultiAPIDlg running cos i've another program named SOCK_CIENT working as a client ... :((
I can't find anything in what you posted to indicate a problem. Also your description of the behavior does not even indicate that the socket has anything to do with the dialog closing. I can't even think of a theory of what could cause the dialog to close without at least firing an assertion. You might try using Spy to watch the messages arriving in the dialog that closes to get some indication of why/how it is closing. Also Debugging and Tracing techniques could prove helpful. Have you ever done a tutorial/sample socket project? I believe MSDN has something like a chat or something that uses CAsyncSocket or CSocket.
-- signature under construction --
-pete