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. Socket Communication - Server with 2 different clients

Socket Communication - Server with 2 different clients

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++sysadmin
13 Posts 4 Posters 0 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.
  • M Offline
    M Offline
    manoharbalu
    wrote on last edited by
    #1

    I have a client and server MFC applications that uses CSocket. The Server exchanges data with the client 1 through port 1500. I want to create a new client application entirely different from client 1 which needs to connect to the server but needs entirely different data than that of client 1 which already connects to the Server in Port 1500. Please suggest me some technical details in terms of port and way of establishing a unique connection with the server. In case If I connect the 2 clients to the same port where the server is listening, how can I know from the Server code that application (client 1) is requesting a connection and client 2 is requesting a connection. Please let me know how can I distinguish applications(clients) from Server side?

    L M 2 Replies Last reply
    0
    • M manoharbalu

      I have a client and server MFC applications that uses CSocket. The Server exchanges data with the client 1 through port 1500. I want to create a new client application entirely different from client 1 which needs to connect to the server but needs entirely different data than that of client 1 which already connects to the Server in Port 1500. Please suggest me some technical details in terms of port and way of establishing a unique connection with the server. In case If I connect the 2 clients to the same port where the server is listening, how can I know from the Server code that application (client 1) is requesting a connection and client 2 is requesting a connection. Please let me know how can I distinguish applications(clients) from Server side?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Please don't use bold font in your questions, it is considered rude. Also we are all perfectly capable of reading a clearly written question. As to your problem, there is really nothing much to do other than designing a simple protocol for your clients: On connect the server starts a new thread which will talk to that client. The client then sends a request which indicates which data it requires The server gets the data and sends to the client.

      Veni, vidi, abiit domum

      M K 2 Replies Last reply
      0
      • L Lost User

        Please don't use bold font in your questions, it is considered rude. Also we are all perfectly capable of reading a clearly written question. As to your problem, there is really nothing much to do other than designing a simple protocol for your clients: On connect the server starts a new thread which will talk to that client. The client then sends a request which indicates which data it requires The server gets the data and sends to the client.

        Veni, vidi, abiit domum

        M Offline
        M Offline
        manoharbalu
        wrote on last edited by
        #3

        First of all, I apologize for the inconvenience regarding my font usage and I assure that I won't continue that henceforth. I want to add something to my above question. 1. The Server and client_1 are already developed applications which communicate flawlessly. 2. There may be a multiple client instances of client_1 that may run and get connected to the server and exchange data. So if any of the data changes in the server, the same gets reflected in all of the client instances of client_1. 3. Now I want to add on a new client_2 entirely different from client_1. So any number of client_2 instances may run and connect to the same server to exchange some other set of data. 4. The Server is listening in Port 1500 for eg. Suppose it gets a client request, now how am I from the server side going to recognize the client before calling accept() so that I could route the client accordingly to exchange data. For Eg: If I know that it is from client_1 I will call SendDataClient1() in a thread. or I will call SendDataClient2() if I know that it is from client_2 Please suggest me with some code to find out a way to differentiate the client_1 and client_2 for the server

        L 1 Reply Last reply
        0
        • M manoharbalu

          I have a client and server MFC applications that uses CSocket. The Server exchanges data with the client 1 through port 1500. I want to create a new client application entirely different from client 1 which needs to connect to the server but needs entirely different data than that of client 1 which already connects to the Server in Port 1500. Please suggest me some technical details in terms of port and way of establishing a unique connection with the server. In case If I connect the 2 clients to the same port where the server is listening, how can I know from the Server code that application (client 1) is requesting a connection and client 2 is requesting a connection. Please let me know how can I distinguish applications(clients) from Server side?

          M Offline
          M Offline
          Malli_S
          wrote on last edited by
          #4

          You need not to anything different at client side. The required capability needs to be implemented at server side. Your server should be able to server multiple request/connections. In your case, when server accepts the first connection, it should spawn the thread to handle the requests from the connected client and the main thread should continue listening of the socket for any new connection. Sample 1.[^] Sample 2.[^]

          [Delegates]      [Virtual Desktop]      [Tray Me !]
          -Malli...! :rose:****

          1 Reply Last reply
          0
          • M manoharbalu

            First of all, I apologize for the inconvenience regarding my font usage and I assure that I won't continue that henceforth. I want to add something to my above question. 1. The Server and client_1 are already developed applications which communicate flawlessly. 2. There may be a multiple client instances of client_1 that may run and get connected to the server and exchange data. So if any of the data changes in the server, the same gets reflected in all of the client instances of client_1. 3. Now I want to add on a new client_2 entirely different from client_1. So any number of client_2 instances may run and connect to the same server to exchange some other set of data. 4. The Server is listening in Port 1500 for eg. Suppose it gets a client request, now how am I from the server side going to recognize the client before calling accept() so that I could route the client accordingly to exchange data. For Eg: If I know that it is from client_1 I will call SendDataClient1() in a thread. or I will call SendDataClient2() if I know that it is from client_2 Please suggest me with some code to find out a way to differentiate the client_1 and client_2 for the server

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            The only way to distinguish, would be to have a listener on a different port. So, client1 types connect to 1500, and client2 types connect to 1501 (for example).

            Veni, vidi, abiit domum

            M 1 Reply Last reply
            0
            • L Lost User

              Please don't use bold font in your questions, it is considered rude. Also we are all perfectly capable of reading a clearly written question. As to your problem, there is really nothing much to do other than designing a simple protocol for your clients: On connect the server starts a new thread which will talk to that client. The client then sends a request which indicates which data it requires The server gets the data and sends to the client.

              Veni, vidi, abiit domum

              K Offline
              K Offline
              koll Zhu
              wrote on last edited by
              #6

              i'd like to known how to work with threads when there'r many clients

              L 1 Reply Last reply
              0
              • L Lost User

                The only way to distinguish, would be to have a listener on a different port. So, client1 types connect to 1500, and client2 types connect to 1501 (for example).

                Veni, vidi, abiit domum

                M Offline
                M Offline
                manoharbalu
                wrote on last edited by
                #7

                This is my sample code I have created 2 Socket objects derived from CListenSocket which in turn is derived from CSocket. The 2 sockets listen in 2 different ports as you said. When a client request comes in the CListenSocket, the AcceptClients() in the CMainframe is called. A Client socket pSocket is created. The new client is accepted in which ever port the client tries to connect. In the below eg. the client tries to connect in port no 1500. But when I debug the code in the function CMainFrame::AcceptClients(), it gets inside the condition if (m_pSocket->Accept()) and gets lost. whereas it is supposed to get inside the condition if (m_pSVRLisnSocket->Accept(*pSocket)) Anyone, Please clarify this and suggest me a method to find out a way to distinguish the connection from 2 different ports 1499 and 1500. Server side code BOOL CMainFrame::StartServer() { PortNo = 1499; if(!m_pSocket) { m_pSocket = new CListenSocket(this); if ( m_pSocket->Create(PortNo) ) m_pSocket->Listen(); else AfxMessageBox("Socket creation failed. Failed to communicate with operator station"); } PortNo = 1500; if(!m_pSVRLisnSocket) //CListenSocket* m_pSVRLisnSocket { m_pSVRLisnSocket = new CListenSocket(this); if( m_pSVRLisnSocket->Create(PortNo) ) //sandeep1 m_pSVRLisnSocket->Listen(); else AfxMessageBox("Socket creation failed. Failed to communicate with operator station"); } } //Server Listen Socket class void CListenSocket::OnAccept(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class CSocket::OnAccept(nErrorCode); m_pFrm->AcceptClients(); //CMainFrame* m_pFrm; } void CMainFrame::AcceptClients() { COprSocket* pSocket = new COprSocket(this,cId); if (m_pSocket->Accept(*pSocket)) //CListenSocket* m_pSocket { CString curOpr; UINT i; pSocket->GetPeerName(curOpr,i); SendPrelimData(pSocket); } else delete pSocket; ///////////////////////////////////////////////////////////////////////////////////// if (m_pSVRLisnSocket->Accept(*pSocket)) //CListenSocket* m_pSVRLisnSocket { CString sCltIP; UINT iPort; pSocket->GetPeerName(sCltIP,iPort); if (iPort == 1500) { AfxMessageBox("INS CLT connected."); } } else delete pSocket; ///////////////////////////////////////////////////////////////////////////////////// } Client Side code BOOL CMainFrame::ConnectSo

                L 1 Reply Last reply
                0
                • K koll Zhu

                  i'd like to known how to work with threads when there'r many clients

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Search Google for "multithreaded c++", and read through some of the samples and articles.

                  Veni, vidi, abiit domum

                  1 Reply Last reply
                  0
                  • M manoharbalu

                    This is my sample code I have created 2 Socket objects derived from CListenSocket which in turn is derived from CSocket. The 2 sockets listen in 2 different ports as you said. When a client request comes in the CListenSocket, the AcceptClients() in the CMainframe is called. A Client socket pSocket is created. The new client is accepted in which ever port the client tries to connect. In the below eg. the client tries to connect in port no 1500. But when I debug the code in the function CMainFrame::AcceptClients(), it gets inside the condition if (m_pSocket->Accept()) and gets lost. whereas it is supposed to get inside the condition if (m_pSVRLisnSocket->Accept(*pSocket)) Anyone, Please clarify this and suggest me a method to find out a way to distinguish the connection from 2 different ports 1499 and 1500. Server side code BOOL CMainFrame::StartServer() { PortNo = 1499; if(!m_pSocket) { m_pSocket = new CListenSocket(this); if ( m_pSocket->Create(PortNo) ) m_pSocket->Listen(); else AfxMessageBox("Socket creation failed. Failed to communicate with operator station"); } PortNo = 1500; if(!m_pSVRLisnSocket) //CListenSocket* m_pSVRLisnSocket { m_pSVRLisnSocket = new CListenSocket(this); if( m_pSVRLisnSocket->Create(PortNo) ) //sandeep1 m_pSVRLisnSocket->Listen(); else AfxMessageBox("Socket creation failed. Failed to communicate with operator station"); } } //Server Listen Socket class void CListenSocket::OnAccept(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class CSocket::OnAccept(nErrorCode); m_pFrm->AcceptClients(); //CMainFrame* m_pFrm; } void CMainFrame::AcceptClients() { COprSocket* pSocket = new COprSocket(this,cId); if (m_pSocket->Accept(*pSocket)) //CListenSocket* m_pSocket { CString curOpr; UINT i; pSocket->GetPeerName(curOpr,i); SendPrelimData(pSocket); } else delete pSocket; ///////////////////////////////////////////////////////////////////////////////////// if (m_pSVRLisnSocket->Accept(*pSocket)) //CListenSocket* m_pSVRLisnSocket { CString sCltIP; UINT iPort; pSocket->GetPeerName(sCltIP,iPort); if (iPort == 1500) { AfxMessageBox("INS CLT connected."); } } else delete pSocket; ///////////////////////////////////////////////////////////////////////////////////// } Client Side code BOOL CMainFrame::ConnectSo

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    You should be able to distinguish which port it is from your object's properties, i.e. PortNo.

                    Veni, vidi, abiit domum

                    M 1 Reply Last reply
                    0
                    • L Lost User

                      You should be able to distinguish which port it is from your object's properties, i.e. PortNo.

                      Veni, vidi, abiit domum

                      M Offline
                      M Offline
                      manoharbalu
                      wrote on last edited by
                      #10

                      In my eg. when you see the client code, the client tries to connect in port no 1500. But In the Server side code when I debug the code after the client request, it gets in the function CMainFrame::AcceptClients() and it gets inside the condition if (m_pSocket->Accept()) and gets lost. whereas it is supposed to get inside the condition if (m_pSVRLisnSocket->Accept(*pSocket)) because pSVRLisnSocket object is created to listen in port 1500. If it happens i can assume that this socket is for port 1500 and proceed further to send the data meant for it. Why is that, its getting inside the first condition Accept())> even though this socket m_pSocket is listening in 1499

                      L 1 Reply Last reply
                      0
                      • M manoharbalu

                        In my eg. when you see the client code, the client tries to connect in port no 1500. But In the Server side code when I debug the code after the client request, it gets in the function CMainFrame::AcceptClients() and it gets inside the condition if (m_pSocket->Accept()) and gets lost. whereas it is supposed to get inside the condition if (m_pSVRLisnSocket->Accept(*pSocket)) because pSVRLisnSocket object is created to listen in port 1500. If it happens i can assume that this socket is for port 1500 and proceed further to send the data meant for it. Why is that, its getting inside the first condition Accept())> even though this socket m_pSocket is listening in 1499

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        You should be using separate objects (one for each port number) to listen on, so you can easily distinguish which object (and port) gets the connection. You cannot assume anything.

                        Veni, vidi, abiit domum

                        M 1 Reply Last reply
                        0
                        • L Lost User

                          You should be using separate objects (one for each port number) to listen on, so you can easily distinguish which object (and port) gets the connection. You cannot assume anything.

                          Veni, vidi, abiit domum

                          M Offline
                          M Offline
                          manoharbalu
                          wrote on last edited by
                          #12

                          I am agree that I am using separate objects (one for each port number) to listen on. But the problem is when the client requests a connection in object_2 at port number_2 (both object_1 and Object_2 are objects of class CListenSocket) it triggers the event CListenSocket::OnAccept(int nErrorCode) which in turn calls m_pFrm->AcceptClients() and inside the function I am calling both (m_pSocket->Accept(*pSocket)) and (m_pSVRLisnSocket->Accept(*pSocket)). What happens is it gets inside m_pSocket->Accept which in turn calls CSocket::Accept() and strucks inside the below loop inside the function while my application proceeds without connecting to the client while (!CAsyncSocket::Accept(rConnectedSocket, lpSockAddr, lpSockAddrLen)) { if (GetLastError() == WSAEWOULDBLOCK) { if (!PumpMessages(FD_ACCEPT)) return FALSE; } else return FALSE; } I thought that it will not take (m_pSocket->Accept(*pSocket)) but will call (m_pSVRLisnSocket->Accept(*pSocket)) since this object_2 (m_pSVRLisnSocket) is getting the client request. Please help me to find out what is causing the problem and how to resolve it.

                          L 1 Reply Last reply
                          0
                          • M manoharbalu

                            I am agree that I am using separate objects (one for each port number) to listen on. But the problem is when the client requests a connection in object_2 at port number_2 (both object_1 and Object_2 are objects of class CListenSocket) it triggers the event CListenSocket::OnAccept(int nErrorCode) which in turn calls m_pFrm->AcceptClients() and inside the function I am calling both (m_pSocket->Accept(*pSocket)) and (m_pSVRLisnSocket->Accept(*pSocket)). What happens is it gets inside m_pSocket->Accept which in turn calls CSocket::Accept() and strucks inside the below loop inside the function while my application proceeds without connecting to the client while (!CAsyncSocket::Accept(rConnectedSocket, lpSockAddr, lpSockAddrLen)) { if (GetLastError() == WSAEWOULDBLOCK) { if (!PumpMessages(FD_ACCEPT)) return FALSE; } else return FALSE; } I thought that it will not take (m_pSocket->Accept(*pSocket)) but will call (m_pSVRLisnSocket->Accept(*pSocket)) since this object_2 (m_pSVRLisnSocket) is getting the client request. Please help me to find out what is causing the problem and how to resolve it.

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #13

                            Sorry, but I cannot follow that. I am not familiar with these MFC constructs, but would assume that when you get a connection there is some way to identifiy which port it occurred on.

                            Veni, vidi, abiit domum

                            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