|
I have come across this before.
First off, I beleive the parameter to listen() is the queue size, so it is valid to be handling 1 call and having one in the queue.
Is there any reason you can only have one client? Typical pattern would be to have a server thread for each client.
One way I have seen your problem solved is to close the accepting socket straight after the first call to accept. The 2nd client will the get a connection refused error. When you server has finished with the 1st client, it can then re-open the accepting socket.
HTH Rob.
|
|
|
|
|
Thank you, Bob, (why your signature is Rob though? )
I tried to shutdown and close the new connection right after accept(). But the client will not receive any feedback from its connect(). But it couldn't send or receive anything either, since there is no connectin at all. Only he (client) doesn't know it. Maybe I should implement some software handshake here to confirm the connection? But there should be some implemented method I could use..
The reason I want only one connection is that the server is running a real-time application, and it should not be disturbed by too many clients, afraid that it would slow down the server's performance. After all, the client's functionality is to record down the server's status for off-line analysis, so one client is enough. I did a version which can accept up to 10 connection, but my collegue said it's a waste of effort.
|
|
|
|
|
You need to close the accepting socket.
eg pseudocode...
SOCKET s1 = socket()
SOCKET s2 = accept(s1,...)
// Now client one is connected to s2
closesocket(s1)
// Now no more clients can connect
// you need to re-open s1 should the s2 connection fail for any reason.
Bob/Rob are both short forms of Robert. At work I am Bob, at Home Rob. I get confused as well sometimes!!
|
|
|
|
|
Bob Groves wrote:
Bob/Rob are both short forms of Robert.
I see. there are so many short forms of English name.
Yeah, I did the same thing as you suggested. The code is :
SOCKET listenSocket;
listen(listenSocket, 1);
while(1)
{
BuildSelectList();
select(HighSocket+1, &ReadSet, NULL, NULL, &timeout);
if ( FD_ISSET(ListenSocket, &ReadSet) )
HandleNewConnection();
else if ( FD_ISSET(ClientSocket, &ReadSet) )
ReadSocket(ClientSocket);
}
in HandleNewConnection():
int newSd;
if ( ClientSocket != 0 )
{
newSd = accept(ListenSocket, NULL, NULL);
shutdown(newSd, 2);
close(newSd);
return;
}
although I closed the second connection, the client running on Windows 2000 didn't receive any error message from its call of connect(). what should I do?
|
|
|
|
|
I've never used the FD_??? macros, but I just tried the following in the 10 minutes before I went home, and it works OK.
I tested it by running "telnet 127.0.0.1 1001" from the DOS prompt twice. The 1st connects, the 2nd doesn't.
I hope it works for you too.
Gotta run now.....
Bob.
#include <winsock.h>
void StartupWinSock()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 0);
err = ::WSAStartup(wVersionRequested, &wsaData);
}
void CSimplesockDlg::OnButton1()
{
StartupWinSock();
SOCKET s1 = socket(PF_INET, SOCK_STREAM, 0);
if (s1 == INVALID_SOCKET)
{
AfxMessageBox("error creating socket");
return;
}
struct sockaddr_in serv_addr;
memset((char *)&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(1001);
if (bind(s1, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
AfxMessageBox("error binding socket");
return;
}
if (listen(s1, 1) == SOCKET_ERROR)
{
AfxMessageBox("error listening on socket");
return;
}
SOCKET s2 = accept(s1, 0, 0);
if (s2 == INVALID_SOCKET)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
AfxMessageBox("error accepting socket");
return;
}
}
closesocket(s1);
// DO stuff with s2
// No new clients should be able to connect
// The Sleep is just so that my simple server does not immediately close down, in real life, you would do you code here.
Sleep(60000);
}
|
|
|
|
|
I tried this. But what would you do after the connection between server and the first client drop? How do you make s1 listen again? Would it be a must to go through the cycle of
socket, bind, listen?
TIA!
|
|
|
|
|
Yes, you would have to go through that cycle.
Have a look at MFC'c CSocket, or wrap up the code in your own class
to make this easier.
Regards,
Bob.
|
|
|
|
|
I see. Thank you Bob. Have a nice day!
|
|
|
|
|
Hi
I've just started to build an lyric application that can query and update the "lyric" database. I'm using the object-oriented approach for this. However i'm having some difficult to interact with the database(just say an access file). Which header file should i include? Are these header available in the standard STL library or in the MFC ? Or if there are not, how do i get those.
Thanks in advace
Cheers.
|
|
|
|
|
STL is a standard library, it does not cover database access. MFC has a umber of ways to access a database, such as ODBC. The best bet is ADO, which is a COM object. Look up ADO on msdn.microsoft.com for more info.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
"I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?!
- Jon Hulatt, 22/3/2002
|
|
|
|
|
Cool! I did do a search on ADO in my MSDN library and i have found huge useful articles about the above issue.
Thank Chris for your help!
|
|
|
|
|
Howdy,
I can create a CEdit with the appropriate style and align text how i want it (ES_LEFT, ES_CENTER, ES_RIGHT). However, I want to then be able to modify the alignment. I tried using ModifyStyle() to do this but it doesnt seem to work.
Any suggestions?
Thanks,
Alex
|
|
|
|
|
Hi!
I am struck at this for a while now. Hopefully sombody will be able to help me.
I have a custom drawn tree ctrl, where I have increased the height of just one item. The vertical scroll bar however does not recognize this increase in height and doesn't scroll properly. I have tried SetScrollRange(), SetScrollInfo() to change the max limit and position of the scroll, but to no effect. I might not have used these functions correctly.
Can anybody please tell me how I can change the scroll pos and range of the vertical scroll bar?
Thanks,
NC
|
|
|
|
|
When I use ICaptureGraphBuilder2::FindInterface() method
to find a specific filter (say IAMVideoCompression).It
give two messageboxes that "Please wait until preroll is
completed!" and ASSERT Failed ((HRESULT)BreakConnect())
>=0) At line 2005
of ..\DXSDK\Samples\Multimedia\DirectShow\BaseClasses\amfi
lter.cpp),but my sourcefilter doesn't need preroll and i
had not add any prerolling filter to the graph.The
reprentative code like :
IAMVideoCompression *pVC=NULL;
if(FAILED(m_pCapture->FindInterface
(&PIN_CATEGORY_CAPTURE,&MEDIATYPE_Video,m_pSF7146,IID_IAMV
ideoCompression,(void**)&pVC)))
{
return S_FALSE;
}
My work environment is DirectX8.1SDK + VC++6.0 + WinXP.
Please help me to release these errors to get my work
continue,thank you advaced!
Scratch
|
|
|
|
|
How to get sytem color: window text color, window bkcolor...
I'm amumu, and you?
|
|
|
|
|
run help index
start to type
getsys
you will see GetSysColor()
t!
|
|
|
|
|
Thankyou
I'm amumu, and you?
|
|
|
|
|
how can get the CPU unique id?
Thanks
|
|
|
|
|
Try GetSystemInfo().
Scratch
|
|
|
|
|
|
I'm trying to use:
ShellExecute(hwnd, NULL, "C:\\Program\\MyApp.EXE", NULL, NULL, SW_SHOW);
to launch a program after a button is clicked on the dialog box but I keep getting the error Undeclared identifier on hwnd, could someone please please help me.
Clara
|
|
|
|
|
you should try to use NULL or this->HWnd instead of hwnd which is not defined
|
|
|
|
|
Undeclared Identifier means exactly what it says... "hwnd" has not been declared. I suspect that either your forgot to have a line like this somewhere:
HANDLE hwnd;
or, you've declared it but are using the wrong case. Remember, hWnd is different to hwnd in C/C++.
If you're using MFC, then it's probably appropriate to call ShellExecute like this:
ShellExecute(this->m_hWnd, NULL, "C:\\Program\\MyApp.EXE", NULL, NULL, SW_SHOW);
Jon
Sorry to dissapoint you all with my lack of a witty or poignant signature.
|
|
|
|
|
the 'hwnd' parameter is that window that calls this function, or to say: Owns it!
You can decide which window that will own it. Pass your parent window to that.
Understand? Perhaps not, because my explanations in English doesn't have such good quality!
------------------------------------
Rickard Andersson, Suza Computing
ICQ#: 50302279
I'm from the winter country SWEDEN!
------------------------------------
|
|
|
|
|
GetHtmlDocument() returns NULL when I call it from OnInitialUpdate() in my CHtmlView derived class.
But later when I call it from a menu command handler function it works fine.
Why is this? Is this intended behaviour?
Nish
If I am awake and my eyes are closed, it does not necessarily mean that I am thinking of naked women.
|
|
|
|
|