我在使用winsock时,使用setsockopt来设置发送和接收的超时。
程序在xp下就可以设置成功,在2000下却发生错误。
请问是为什么?
怎么处理?
在线等候
谢谢

解决方案 »

  1.   

    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
      

  2.   

    saucer(思归) :谢谢您的资料。
    我在开始时已经使用了
    WORD wVersionRequested = MAKEWORD( 2, 2 );
    WSAStartup(wVersionRequested,&ws);
    并且是成功的。
    这不代表我已经在使用winsock 2.0吗?
    另外,我如何得到winsock 2.0?谢谢
      

  3.   

    I guess that should do, also check
     c:\winnt\system32\ws2_32.dll
      

  4.   

    saucer:
    您好。
    谢谢您的提议。
    我现在的设置是这样
    #include <winsock2.h>
    link里包含WS2_32.LIB
    可是,还是不行。
    您看还需要设置什么?
    谢谢了。
      

  5.   

    you said "在2000下却发生错误"? what kind of errors did you get? after you used winsock2, what errors did you get?
      

  6.   

    saucer(思归):
    您好。
    发生的错误是10042号,ms的解释是:
    协议选项错无。表明在getsockopt或setsockopt调用中,指定的套接字选项或级别不明、未获支持或者无效。
    用了winsock2后,错误依然。
      

  7.   

    不是吧,我在win2k中也像你一样,通过setopt来设置超时,但一点儿问题都没有
      

  8.   

    Semigod() :
    您好。
    您的2000是什么版本?
    有什么特殊设置?
    谢谢
      

  9.   

    ************************************
    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
      

  10.   

    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;

    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;
    } 到这里就开始出错。您帮忙看看.
      

  11.   

    try changing
    IPPROTO_TCP
    ===>
    SOL_SOCKET
      

  12.   

    saucer(思归):
    您好。
    改成SOL_SOCKET后,能够成功,但对超时现象没有改变。
    您看怎么办?谢谢
      

  13.   

    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>
      

  14.   

    saucer(思归):
    您好,是设置成SOL_SOCKET吗?
    设置成这样一切都正常,可是到了一些网站那延时就会出现。
    会等很长时间。
      

  15.   

    I suggest you to use asynchronous socket
      

  16.   

    saucer(思归):
    您好。
    我使用的就是我上面给您写出的代码。
    我实在是想不通有什么问题了。
    您能把您的测试代码给我看看吗?
    谢谢
      

  17.   

    change
    iValue=1;
    ===>
    iValue=1000;
      

  18.   

    saucer(思归) :
    不行。
    您能把您的测试程序发给我吗?
    [email protected]谢谢
      

  19.   

    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;
    }

    if(setsockopt(Socket,SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout))==SOCKET_ERROR)
    {
      closesocket(Socket);
      PRINTERROR("cannot set SO_SNDTIMEO timeout"); 
      return;
    }
      

  20.   

    tell me a URL you have timeout problem with, and let me try it to make sure my code works in those situations
      

  21.   

    saucer(思归) :
    您好。
    其实我的程序是要循环访问一组url。
    这样就经常会出现在一些站点上停止不动。
    所以想使用超时设置,可是总是出问题。您能告诉我您的某种联系方法吗?
    我好当面请教。谢谢
      

  22.   

    use asynchronous and non-blocking sockets
      

  23.   

    saucer(思归):
    能给出一个简单的例子吗?谢谢
      

  24.   

    加上下面两句试试:
    u_long mode = 0;
    ioctlsocket(s, FIONBIO, &mode); // set to blocking mode