check the winsock version you are using, it should work under winsock2.0, seeSocket Options for Windows NT and Windows 95/98/Me http://msdn.microsoft.com/library/en-us/winsock/winsock/socket_options_for_windows_nt_and_windows_95_98_me_2.asp?frame=true How can I change the timeout for a Winsock function? http://tangentsoft.net/wskfaq/newbie.html
************************************ WSAENOPROTOOPT (10042) Bad protocol option. An unknown, invalid or unsupported option or level was specified in a getsockopt() or setsockopt() call. ************************************please show your relevant code
saucer(思归) :您好大概的代码如下,主要目的是访问网站来获取页面 WSADATA ws; WORD wVersionRequested; wVersionRequested = MAKEWORD( 2, 2 ); int r=WSAStartup(wVersionRequested,&ws); if(r != 0) { WSACleanup(); AfxMessageBox("failed for some reason\n"); return ; } CString strUrl=_T("http://www.csdn.net/develop"); DWORD dwServiceType; CString _strServer,_strObject; INTERNET_PORT nPort;
SOCKET s; struct sockaddr_in addr; int iResult; char *strMessage; int iDataLength=0; int idx=0; CString content; char strBuffer[65535]; int iValue, iLen=sizeof(iValue); iValue=1;
I didn't get any timeout error, here is what I got:HTTP/1.1 302 Object Moved Location: http://211.157.102.21/develop/ Server: Microsoft-IIS/5.0 Content-Type: text/html Content-Length: 153<head><title>Document Moved</title></head> <body><h1>Object Moved</h1>This document may be found <a HREF="http://211.157.102.21/develop/">here</a></body>
here is the code I used:download GetHTTP.zip from http://www.sockaddr.com/ExampleSourceCode.htmland I modified the following lines:#include <winsock2.h> ..... WORD wVersionRequested = MAKEWORD(2,2); .... int timeout = 1000; if(setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout))==SOCKET_ERROR) { closesocket(Socket); PRINTERROR("cannot set SO_RCVTIMEO timeout"); return; }
http://msdn.microsoft.com/library/en-us/winsock/winsock/socket_options_for_windows_nt_and_windows_95_98_me_2.asp?frame=true
How can I change the timeout for a Winsock function?
http://tangentsoft.net/wskfaq/newbie.html
我在开始时已经使用了
WORD wVersionRequested = MAKEWORD( 2, 2 );
WSAStartup(wVersionRequested,&ws);
并且是成功的。
这不代表我已经在使用winsock 2.0吗?
另外,我如何得到winsock 2.0?谢谢
c:\winnt\system32\ws2_32.dll
您好。
谢谢您的提议。
我现在的设置是这样
#include <winsock2.h>
link里包含WS2_32.LIB
可是,还是不行。
您看还需要设置什么?
谢谢了。
您好。
发生的错误是10042号,ms的解释是:
协议选项错无。表明在getsockopt或setsockopt调用中,指定的套接字选项或级别不明、未获支持或者无效。
用了winsock2后,错误依然。
您好。
您的2000是什么版本?
有什么特殊设置?
谢谢
WSAENOPROTOOPT (10042)
Bad protocol option. An unknown, invalid or unsupported option or level was specified in a getsockopt() or setsockopt() call.
************************************please show your relevant code
WSADATA ws;
WORD wVersionRequested;
wVersionRequested = MAKEWORD( 2, 2 );
int r=WSAStartup(wVersionRequested,&ws);
if(r != 0)
{
WSACleanup();
AfxMessageBox("failed for some reason\n");
return ;
}
CString strUrl=_T("http://www.csdn.net/develop");
DWORD dwServiceType;
CString _strServer,_strObject;
INTERNET_PORT nPort;
SOCKET s;
struct sockaddr_in addr;
int iResult;
char *strMessage;
int iDataLength=0;
int idx=0;
CString content;
char strBuffer[65535];
int iValue, iLen=sizeof(iValue);
iValue=1;
if(!AfxParseURL(strUrl,dwServiceType,
_strServer,_strObject,nPort))
{
AfxMessageBox("");
return;
}
s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(setsockopt(s, IPPROTO_TCP, SO_RCVTIMEO, (char *)&iValue, iLen)==SOCKET_ERROR)
{
//接收超时
closesocket(s);
AfxMessageBox("");
return;
}
if(setsockopt(s, IPPROTO_TCP, SO_SNDTIMEO, (char *)&iValue, iLen)==SOCKET_ERROR)
{
//发送超时
closesocket(s);
AfxMessageBox("");
return;
} 到这里就开始出错。您帮忙看看.
IPPROTO_TCP
===>
SOL_SOCKET
您好。
改成SOL_SOCKET后,能够成功,但对超时现象没有改变。
您看怎么办?谢谢
Location: http://211.157.102.21/develop/
Server: Microsoft-IIS/5.0
Content-Type: text/html
Content-Length: 153<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="http://211.157.102.21/develop/">here</a></body>
您好,是设置成SOL_SOCKET吗?
设置成这样一切都正常,可是到了一些网站那延时就会出现。
会等很长时间。
您好。
我使用的就是我上面给您写出的代码。
我实在是想不通有什么问题了。
您能把您的测试代码给我看看吗?
谢谢
iValue=1;
===>
iValue=1000;
不行。
您能把您的测试程序发给我吗?
[email protected]谢谢
http://www.sockaddr.com/ExampleSourceCode.htmland I modified the following lines:#include <winsock2.h>
.....
WORD wVersionRequested = MAKEWORD(2,2);
....
int timeout = 1000;
if(setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout))==SOCKET_ERROR)
{
closesocket(Socket);
PRINTERROR("cannot set SO_RCVTIMEO timeout");
return;
}
if(setsockopt(Socket,SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout))==SOCKET_ERROR)
{
closesocket(Socket);
PRINTERROR("cannot set SO_SNDTIMEO timeout");
return;
}
您好。
其实我的程序是要循环访问一组url。
这样就经常会出现在一些站点上停止不动。
所以想使用超时设置,可是总是出问题。您能告诉我您的某种联系方法吗?
我好当面请教。谢谢
能给出一个简单的例子吗?谢谢
u_long mode = 0;
ioctlsocket(s, FIONBIO, &mode); // set to blocking mode