Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Unable to create a server

Unable to create a server

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminhelpquestion
7 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    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?

    4 T P 3 Replies Last reply
    0
    • A Anonymous

      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?

      4 Offline
      4 Offline
      4apai
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A Anonymous

        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?

        T Offline
        T Offline
        Tom Wright
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A Anonymous

          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?

          P Offline
          P Offline
          palbano
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • P palbano

            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

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            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 ... :((

            A P 2 Replies Last reply
            0
            • A Anonymous

              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 ... :((

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              hi, i'm so sorry, i've run the program in debug mode and received the following error message, Unhandled exception in MultiAPI.exe: 0xC0000005: Access Violation i wonder what this means?

              1 Reply Last reply
              0
              • A Anonymous

                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 ... :((

                P Offline
                P Offline
                palbano
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups