|
Dear all,
I have a question about Winsock.
My program is a TCP client which manipulates lots of socket connection concurrently.
One socket for each thread.
I use blocking mode at most of time, except creating a new connection to server.
I turn socket into non-blocking mode first, then use select() to check the socket before turning back.
I really don't want to waste time on uncertain blocking timeout and make sure the connection is established in the specific time.
Everything works well when most of server are reachable.
But here comes the question.
If the number of unreachable server increases(more than 5), the rest reachable server will fail on creating connection.
In this case, select() always return 0(means timeout) even though the server is originally reachable.
Here is the portion of my program to create connection:
Open(LPCTSTR addr, int port, DWORD dwTimeout)
{
_socket = 0;
SOCKET theSocket;
int nRet;
LPHOSTENT lpHostEntry;
lpHostEntry = gethostbyname(addr);
if (lpHostEntry == NULL) {
return;
}
theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (theSocket == INVALID_SOCKET) {
return;
}
unsigned long ul = 1;
nRet = ioctlsocket(theSocket, FIONBIO, (unsigned long*)&ul);
if (nRet == SOCKET_ERROR) {
closesocket(theSocket);
return;
}
SOCKADDR_IN saServer;
saServer.sin_family = AF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
saServer.sin_port = htons(port);
nRet = connect(theSocket,
(LPSOCKADDR)&saServer,
sizeof(struct sockaddr));
if (nRet == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK) {
closesocket(theSocket);
return;
}
struct timeval timeout;
fd_set r;
FD_ZERO(&r);
FD_SET(theSocket, &r);
timeout.tv_sec = dwTimeout / 1000;
timeout.tv_usec = (dwTimeout % 1000) * 1000;
nRet = select(0, 0, &r, 0, &timeout);
if (nRet <= 0 ) {
closesocket(theSocket);
return;
}
ul= 0;
nRet = ioctlsocket(theSocket, FIONBIO, (unsigned long*)&ul);
if (nRet == SOCKET_ERROR){
closesocket(theSocket);
return;
}
_socket = theSocket;
}
Is there anything wrong in this code?
Or any constraint on the number of unreachable socket select() concurrently in multithread?
|
|
|
|
|
luderjane wrote: I use blocking mode at most of time, except creating a new connection to server.
I turn socket into non-blocking mode first, then use select() to check the socket before turning back.
Just an idea, you could use a "master thread" to check for timeouts and close socket handles. This should make threads return from blocking connect() calls if necessary... and you can remove the select-timeout-code (untested).
/M
|
|
|
|
|
I wrote a simple testbed to exclude other factor in my program as follows(builds in VC2005, must depends on Ws2_32.lib):
#include <stdio.h>
#include <tchar.h>
#include <Winsock2.h>
typedef struct ServerInfo {
char sIP[32];
int iPort;
DWORD dwTimeout;
BOOL bPrintable;
} ServerInfo, *pServerInfo;
DWORD WINAPI OpenThreadProc(void* lpParam);
void BlockingOpen(pServerInfo pServer);
void NonBlockingOpen(pServerInfo pServer);
int _tmain(int argc, _TCHAR* argv[])
{
int iNumOfURServer = 0;
char sIP[32];
int iPort = 80;
DWORD dwTimeout = 5000;
printf("Number of unreachable server: ");
scanf("%d", &iNumOfURServer);
printf("Reachable server IP: ");
scanf("%s", sIP);
printf("Reachable server port: ");
scanf("%d", &iPort);
printf("Timeout(in millisecond, 0 indicates blocking mode): ");
scanf("%u", &dwTimeout);
WSADATA wsaData;
WORD version = MAKEWORD(2,2);
WSAStartup(version, &wsaData);
pServerInfo ServerArray = new ServerInfo[iNumOfURServer+1];
char sURIP[32];
for (int i = 0; i < iNumOfURServer; i++)
{
sprintf(sURIP, "192.168.1.1%02d", i);
strcpy_s(ServerArray[i].sIP, sURIP);
ServerArray[i].iPort = 3456;
ServerArray[i].dwTimeout = dwTimeout;
ServerArray[i].bPrintable = FALSE;
CreateThread(NULL, 0, OpenThreadProc, &ServerArray[i], 0, NULL);
}
strcpy_s(ServerArray[iNumOfURServer].sIP, sIP);
ServerArray[iNumOfURServer].iPort = iPort;
ServerArray[iNumOfURServer].dwTimeout = dwTimeout;
ServerArray[iNumOfURServer].bPrintable = TRUE;
CreateThread(NULL, 0, OpenThreadProc, &ServerArray[iNumOfURServer], 0, NULL);
scanf("%d", &iPort);
return 0;
}
DWORD WINAPI OpenThreadProc(void* lpParam)
{
pServerInfo pServer = (pServerInfo)lpParam;
while (true)
{
if (pServer->dwTimeout == 0)
{
BlockingOpen(pServer);
}
else
{
NonBlockingOpen(pServer);
}
Sleep(1000);
}
return 0;
}
void BlockingOpen(pServerInfo pServer)
{
SOCKET theSocket;
int nRet;
LPHOSTENT lpHostEntry;
lpHostEntry = gethostbyname(pServer->sIP);
if (lpHostEntry == NULL) {
return;
}
theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (theSocket == INVALID_SOCKET) {
return;
}
SOCKADDR_IN saServer;
saServer.sin_family = AF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
saServer.sin_port = htons(pServer->iPort);
DWORD dwStartTime = GetTickCount();
nRet = connect(theSocket,
(LPSOCKADDR)&saServer,
sizeof(struct sockaddr));
if (pServer->bPrintable)
{
if (nRet == SOCKET_ERROR)
{
printf("%s:%d blocking open failed takes %d ms\n", pServer->sIP, pServer->iPort,
GetTickCount()-dwStartTime);
}
else
{
printf("%s:%d blocking open successful takes %d ms\n", pServer->sIP, pServer->iPort,
GetTickCount()-dwStartTime);
}
}
closesocket(theSocket);
}
void NonBlockingOpen(pServerInfo pServer)
{
SOCKET theSocket;
int nRet;
LPHOSTENT lpHostEntry;
lpHostEntry = gethostbyname(pServer->sIP);
if (lpHostEntry == NULL) {
return;
}
theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (theSocket == INVALID_SOCKET) {
return;
}
unsigned long ul = 1;
nRet = ioctlsocket(theSocket, FIONBIO, (unsigned long*)&ul);
if (nRet == SOCKET_ERROR) {
closesocket(theSocket);
return;
}
SOCKADDR_IN saServer;
saServer.sin_family = AF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
saServer.sin_port = htons(pServer->iPort);
nRet = connect(theSocket,
(LPSOCKADDR)&saServer,
sizeof(struct sockaddr));
if (nRet == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK) {
closesocket(theSocket);
return;
}
struct timeval timeout;
fd_set r;
FD_ZERO(&r);
FD_SET(theSocket, &r);
timeout.tv_sec = pServer->dwTimeout / 1000;
timeout.tv_usec = (pServer->dwTimeout % 1000) * 1000;
DWORD dwStartTime = GetTickCount();
nRet = select(0, 0, &r, 0, &timeout);
if (pServer->bPrintable)
{
if ( nRet <= 0 )
{
printf("%s:%d non-blocking open failed takes %d ms\n", pServer->sIP, pServer->iPort,
GetTickCount()-dwStartTime);
}
else
{
printf("%s:%d non-blocking open successful takes %d ms\n", pServer->sIP,
pServer->iPort, GetTickCount()-dwStartTime);
}
}
closesocket(theSocket);
}
This testbed simply creates multiple thread which opens a socket repeatedly.
There is only one reachable server and the rest are unreachable.
When I use non-blocking mode(timeout = 5000) and 2 unreachable servers, the open procedure takes 0 ms at most of time.
If I gradually increase the number of unreachable server and keep the same timeout, the open procedure takes more and more time.
In the end(about 16 unreachable servers), the open procedure failed every time(except first 5 times).
Even in blocking mode(timeout = 0), the opening time consuming will increase with the number of unreachable server.
There seems to be a constraint on the socket connection in multithread.
But I cannot google any official document about it...
|
|
|
|
|
hello everybody,
Have gone through some application made in WPF and saw their painting means, drawing of controls, image etc..
They were just amazing.
One thing to be asked is how internally they are managing such fast, flicker free painting.
I tried to achieve such kind of optimized painting in Win32 or MFC applications, but to no success.
Please tell me which all points I am missing. Where I should look for concerned resources.
Thanks for your time.
|
|
|
|
|
|
Hi,
Following on from a question I asked yesterday, I am left with my document being closed and my view being reverted to default (empty) state, but being unable to open the file I was last viewing because the CDocTemplate has the file marked "yesAlreadyOpen".
The file is not open as I read the content of the file and then close it, but a I am uanble to re-view teh file again until I have opened another file and then come back to it.
Is there a flag or reset I need to apply in my OnFileClose function to clear that CDocTemplate flag?
If so, what would would I need to call?
TIA
Tony
|
|
|
|
|
I'm not totally sure of this as I am not a regular user of MFC but you might try looking at RemoveDocument() [^].
MVP 2010 - are they mad?
|
|
|
|
|
Hi again.
I just looked at the code in one of my SDI apps that does not exhibit the problem you're seeing, and I notice in the OnNewDocument function, it uses
if (!CDocument::OnNewDocument())
return FALSE;
Perhaps you can call that when you close your first document and it might reset your flag.
Hope that helps.
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
Hi,
In my applcaition the menu is default like
------------------
File. View. Help.
------------------
But i dont want this.I want like
------------------
Applicaitionname
-------------------
To get this,i delete Help menu first and the applcaiton works fine.I delete Statusbar from View,works fine.Then i delete Toolbar from view menu but it shows the assertion error in
atmfc\src\mfc\winmdi.cpp on Line 132.
How can i get my custom menu.
Even i cannot these IDR_MAINFRAME menu and using new IDR_MAINFRAME with my menu.THis also creates same assertion problem.
Pls help me.
Im using VS2008.
Anu
|
|
|
|
|
If you are removing resources then you may have to remove some parts of the source code as well which might be using these deleted resources. May be you can debug and check the call stack to see which call leads to the assertion.
|
|
|
|
|
I'm going to guess that your application is an MFC MDI app. If that's the case, the MFC framework requires that the main menu MUST contain a minimum of 2 items (Normally File and View).
If you debug your application you should get a Debug Assertion. Click on the retry button and it will take you to the line that caused the problem. In my test case, it's on line 132 of winmdi.cpp (MFC source file). Look at the surrounding code and you'll see
int iMenu = pMenu->GetMenuItemCount() - 2;
ASSERT(iMenu >= 0);
In the comments, it tells you how to handle this - override OnCreateClient.
Hope that helps.
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
How to deselect the tree item when clicking outside the tree node (i need to remove focusbox). I used the below code but i failed.
void CEditableView::OnLButtonDown(UINT nFlags, CPoint point)
{
TVHITTESTINFO tvInfo;
CPoint pt;
HTREEITEM m_pOldSel;
GetCursorPos(&pt);
m_treeCtrl.ScreenToClient(&pt);
tvInfo.pt = pt;
UINT uFlags = 1;
UINT flags;
HTREEITEM htItem = m_treeCtrl.HitTest(pt);
if ((htItem != NULL))
{
m_pOldSel = m_treeCtrl.GetSelectedItem();
m_treeCtrl.Select(htItem, TVGN_DROPHILITE);
m_treeCtrl.GetCheck(htItem);
}
else
{
m_treeCtrl.Select(htItem, TVGN_DROPHILITE);
}
CView::OnLButtonDown(nFlags, point);
}
|
|
|
|
|
ratheeshnair123 wrote: else
{
m_treeCtrl.Select(htItem, TVGN_DROPHILITE);
}
Why would you do this if htItem is NULL ?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Hi...
I have to some static library files.
I have added the library name in project setting.
but the library files are not working...
any help will be appriciated...
Thanks...
G.Paulraj
|
|
|
|
|
Paulraj G wrote: I have added the library name in project setting.
You should:
- Add the
.lib files as linker input (additional dependecies). - Make
Visual Studio aware of the folder(s) wherein such library files are (add the folders in the VC++ directories list). - Include the appropriate headers in your source files.
Paulraj G wrote: but the library files are not working...
What (the fresh hell) does it mean? Could you, please, be more specific?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
tsk tsk, you don't know what "files not working" mean? It means, of course, they are playing cards or drinking cooffe insteads of answering the calls of the process and doing their job. Imagine it... they are so lazy that don't even give any error message.
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Those library files, always reading books when they should be working...
|
|
|
|
|
Hi...
Thanks for both of your reply.
M.D.V.,
I am not that kind of person. Due to some network problem, Internet connection was cancelled. thats why the delay...
Sorry for the trouble...
G.Paulraj
|
|
|
|
|
What kind of person?
For what I understood, he wrote about lazy libraries...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Mmmm... so?
I gave you an answer to your question and then I made a joke with the start point of one CPallini's comment (please pay attention to the "tree design" to see which message hangs on which other message and on the icons of "message type"). I never said anything about you.
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
in grosso modo:
Once per library:
1) Install the library. Unzip it to a directory or install it via a package manager.
2) Tell the compiler where to look for the header file(s) for the library.
3) Tell the linker where to look for the library file(s) for the library.
Once per project:
5) Tell the linker which static or import library files to link.
6) #include the library's header file(s) in your program.
7) Make sure the program know where to find any dynamic libraries being used.
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Hello Sirs,
How to find the compression type of jpeg, when we give one sample input jpeg file the output result is this image contains (ex.) xxxx compression type. How to identify it .?
please replay
Failure is Success If we learn from it!!
|
|
|
|
|
Game-point wrote: How to find the compression type of jpeg, when we give one sample input jpeg file the output result is this image contains (ex.) xxxx compression type.
JPEG images use JPEG Compression. May be you should hardcode the output?
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Hello all,
I am trying to figure out how to make an adaptive program, which asks questions and if you get it wrong, it will ask you another question in that area.
I know this will utilize arrays but I am just asking for some guidence on if I should use looping statements or write the whole thing out.
Thoughts on where to start?
"EDIT: The source is on the 9'th reply to this thread. Please read it to help me out."
V/R
Rob
modified on Thursday, January 28, 2010 10:25 AM
|
|
|
|
|