Socket Communication - Server with 2 different clients
-
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?
-
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?
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
-
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
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
-
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?
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:**** -
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
-
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
-
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
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
-
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
-
You should be able to distinguish which port it is from your object's properties, i.e.
PortNo
.Veni, vidi, abiit domum
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
-
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
-
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
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.
-
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.