Dear all,
Today, i tried to test ahost in wince 4.2.
But i still didn't find the error with select() which
always make nfds of read_fds to 0. and select
also return 0 cos timeout expired.
I think select() in wince 4.2 didn't run correctly, so
i make a other sample to test select(), and it
run smoothly ( you can see the code in the end )
Until now, i have no idea.
Have a nice weekend to all!
// My source code to test select()
WSAData wsaData;
int i = WSAStartup(MAKEWORD(2,2),&wsaData);
if (i == SOCKET_ERROR)
{
MessageBox(_T("socket_error"),_T("socket_error"),MB_OK);
};
SOCKET s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//check to see if we have a valid socket
if (s == INVALID_SOCKET) {
int iSocketError = WSAGetLastError ();
}
//get the host information
HOSTENT *hostServer = gethostbyname("www.yahoo.com <http://www.yahoo.com>");
if (hostServer == NULL ) {
int iSocketError1 = WSAGetLastError();
}
//set up the target device address struction
SOCKADDR_IN sinServer;
memset(&sinServer,0,sizeof(SOCKADDR_IN));
sinServer.sin_family = AF_INET;
sinServer.sin_port = htons(80);
sinServer.sin_addr = *((IN_ADDR*)hostServer->h_addr_list[0]);
//connect with a valid socket
if (connect(s,(SOCKADDR *)&sinServer,sizeof(sinServer)) == SOCKET_ERROR)
{
int iSocketError2= WSAGetLastError();
}
//send a request to the server
char cBuffer[1024] = "";
int nBytesSent = 0;
int nBytesIndex = 0;
//setup the buffer to sent
sprintf(cBuffer,"GET/HTTP/1.0\r\n\r\n");
int nBytesLeft = strlen(cBuffer);
//send the entire buffer
while(nBytesLeft>0)
{
nBytesSent = send(s,&cBuffer[nBytesIndex],nBytesLeft,0);
if (nBytesSent == SOCKET_ERROR)
{
MessageBox(_T("nBytesSent error"),_T("nBytesSent error"),MB_OK);
}
nBytesLeft -=nBytesSent;
nBytesIndex += nBytesSent;
}
timeval tv;
tv.tv_sec = 10;
tv.tv_usec = 100;
fd_set fread;
FD_ZERO(&fread);
FD_SET(s,&fread);
int ret = select(0,&fread,NULL,NULL,&tv);
// value return of ret always 1, that is good!!
//Get the reponse
TCHAR tchResponseBuffer[1024]= TEXT("\0");
char cResponseBuffer [1024] = "";
BOOL fBreak = FALSE;
int nBytesReceived = 0;
while (!fBreak)
{
nBytesReceived = recv(s,&cResponseBuffer[0],1024,0);
if (nBytesReceived == SOCKET_ERROR)
{
MessageBox(_T("nByteRecived = 0"),NULL,MB_OK);
}
//convert the data from ANSI to Unicode
mbstowcs(tchResponseBuffer,cResponseBuffer,nBytesReceived);
//show the messageBox
MessageBox(tchResponseBuffer,TEXT("Web output"),MB_OK);
if (_tcsstr(tchResponseBuffer,TEXT("\r\n\r\n")))
{
MessageBox(_T("in the end of page"),NULL,MB_OK);
}
//clear the buffer
memset(tchResponseBuffer,0,1024);
memset(cResponseBuffer,0,1024);
}
closesocket(s);
WSACleanup();
_______________________________________________
http://cool.haxx.se/mailman/listinfo/c-ares
Received on Fri Apr 29 20:12:48 2005