Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 继续昨天的例子
【标  题】:继续昨天的例子
【关键字】:
【来  源】:http://www.cppblog.com/artmouse/archive/2005/12/07/1602.html

继续昨天的例子

Your Ad Here

昨天晚上又查了很多资料,发现网上很多关于连接中断的讨论,都是说onclose没用,然后也没看到有什么解决办法的。

查资料的时候,查到一段英语
If I remember my old programming under delphi with winsock, when a
socket was closed (either by a closesocket call or an application
termination) in a host, the partner received a packet with a null
length (I'm not 100% sure of that but...).

I found in sockcore.cpp that kind of test
if (nBytes != 0 || nErrorCode != 0)
  pSocket->OnReceive(nErrorCode);
which means that if zero length received with no error the onreceived
event is not fired !

Maybe I'm wrong, but that could be the point of blocking.
To check it, it should be necessary to recompile that source file, but
I don't know exactly how, on which directory and with which
environment variables and parameter values ....


后又继续测试了昨天那个程序,发现如下现象

1,连接
2,拔网卡
3,等待10分钟
4,按下发送
5,send返回的是成功

1,连接
2,拔网卡
3,不等待
4,按下发送
5,send返回的是成功
6,等待一段时间后按发送(1-2分钟)
7,返回的是失败

看来连接中断了程序还是知道的,就是没有错误的回调函数

继续深入,在send函数的help里面看到

The successful completion of a send call does not indicate that the data was successfully delivered.
send成功完成并不代表数据已经成功送达。

If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.
如果没有缓冲存储待发送的数据,send会阻塞直到socket被设置为非阻塞模式,
On nonblocking stream-oriented sockets, the number of bytes written can be between 1 and the requested length, depending on buffer availability on both client and server machines.
在非阻塞流模式socket中,写入的字节可以是1到需要的长度,依赖于客户端和服务器的缓冲。
The select or
WSAEventSelect function can be used to determine when it is possible to send more data.
select 或 WSAEventSelect 函数可以用于决定什么时候可以继续发送数据


就这样看来,要解决这个send可能要自己动手写了。

翻了一下CCeSocket的代码,
代码是看看
IsAlive(看看是否活着),
SetNonblocking()设为非阻塞模式
SetBlocking()设为阻塞模式

BOOL CCeSocket::IsAlive()
{
    
int  iResult;                                              
    
int     iLen = sizeof(int);                       

    
if (getsockopt( m_hSocket, SOL_SOCKET, SO_TYPE, (char*)&iResult, &iLen ) == SOCKET_ERROR)
        
return (FALSE);

    
return (TRUE);
}

BOOL CCeSocket::SetNonblocking()
{
    unsigned 
long ulResult = 1;  // nonzero => non-blocking

    
if (ioctlsocket( m_hSocket, FIONBIO, &ulResult ) == SOCKET_ERROR)
    
{
        
int iErr = WSAGetLastError();
        
if (iErr != WSAENOTSOCK)
            ASSERT(FALSE);
        
return (FALSE);
    }


    
return (TRUE);
}


BOOL CCeSocket::SetBlocking()
{
    unsigned 
long ulResult = 0;  // zero => blocking

    
if (ioctlsocket( m_hSocket, FIONBIO, &ulResult ) == SOCKET_ERROR)
    
{
        
int iErr = WSAGetLastError();
        
if (iErr != WSAENOTSOCK)
            ASSERT(FALSE);
        
return (FALSE);
    }


    
return (TRUE);
}

没办法,翻出evc高级编程及其应用开发的封装,继续尝试

JavaEE 5.0的三个半方面new feature:【上一篇】
Nothing is impossible:自删除的程序:【下一篇】
【相关文章】
没有相关文章
【随机文章】
  • 自定义指定文件夹的图标
  • 关于人.....
  • 小心'溢出'陷阱
  • NULL对象指针
  • 目录,文件操作详谈2
  • 在Linux中配置网络打印机共享
  • 调用proj.4库
  • 关于SUN T3电池的问题
  • System.OutOfMemory Exception
  • ASP生成Word文档的又一方法
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.