|
Hi,
thanks for Answer, you right, i solve it in this way!
I didn know that i can check "the required size"
Now:
<br />
srg.QueryBinaryValue(_T("key"), NULL, &bytes);<br />
int *iArr = new int[bytes];<br />
srg.QueryBinaryValue(_T("key"), (BYTE**)&iArr[0], &bytes);<br />
Now i now how big is the needed size, it is saved in "bytes".
And i have all Data from the registry.
Best regards
bosfan
|
|
|
|
|
int *iArr = new int[bytes];
Your buffer will be 4 (or even 8) times bigger than necessary; you should use:
BYTE *iArr = new BYTE[bytes];
Veni, vidi, abiit domum
|
|
|
|
|
You right again, i change this, but i think if i do this in this way i can call the function
without cast to byte, directly?
Something like this:
<br />
regkey.QueryBinaryData(_T("key"), &iArr[0], &bytes);
i try this and this works to, is there any thing wrong now?
bosfan
|
|
|
|
|
I cannot find a method called QueryBinaryData so I presume you are referring to CRegKey::QueryBinaryValue [^]. You could also use the simpler syntax for your buffer address thus:
regkey.QueryBinaryValue(_T("key"), iArr, &bytes);
The compiler may also still insist that you cast the buffer address to a void* .
Veni, vidi, abiit domum
|
|
|
|
|
Oh no, yes of course i mean QueryBinaryValue my mistake (.
One question now, is there any difference in Unicode Version, my App is in Unicode?
Regards
bosfan
|
|
|
|
|
If the data you are extracting is binary, then no, there is no difference.
Veni, vidi, abiit domum
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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::ConnectSocket(LPCTSTR lpszAddress, UINT nPort)
{
m_pCLTSocket = new COprSocket(this); //COprSocket *m_pCLTSocket
if (!m_pCLTSocket->Create())
{
delete m_pCLTSocket;
m_pCLTSocket = NULL;
return FALSE;
}
if(!m_pCLTSocket->Connect(lpszAddress, 1500))
{
delete m_pCLTSocket;
m_pCLTSocket = NULL;
return FALSE;
}
}
|
|
|
|
|
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 <if (m_psocket-="">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.
|
|
|
|
|
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
|
|
|
|
|
i'd like to known how to work with threads when there'r many clients
|
|
|
|
|
Search Google for "multithreaded c++", and read through some of the samples and articles.
Veni, vidi, abiit domum
|
|
|
|
|
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.[^]
|
|
|
|
|
Server Side:
m_pSocket->Create(1500);
m_pSocket->Listen();
m_pSocket->Accept(*pSocket);
pSocket->GetPeerName(sIP,iPort);
GetPeerName
Client Side:
m_pCLTSocket->Create();
m_pCLTSocket->Connect(lpszAddress, 1500));
I checked in the debugger the value of iPort which shows 4774. I am listening at port 1500 and connecting the client at port 1500.
Then what is the port number that is returned by GetPeerName(sIP,iPort) function?
|
|
|
|
|
This is the address of the socket at the client end, which will be allocated by the system when it calls Connect . Port 1500 is the port number at the server end.
Veni, vidi, abiit domum
|
|
|
|
|
Thanks a lot for the clarification.
|
|
|
|
|
in the function of undating,i need to obtain the version number which is getted from web server and set it to my software. could anyone show me how to set the version in code.
thanks!
|
|
|
|
|
Is this a C++ question, and if so what version are you referring to?
Veni, vidi, abiit domum
|
|
|
|
|
thank u,my meaning is replacing the version of my .exe with the one from server.
|
|
|
|