找回密码
 加入
搜索
查看: 7844|回复: 18

[网络通信] TCPConnect 函数导致Word文档打开速度奇慢如何解决?

 火.. [复制链接]
发表于 2011-11-21 14:18:48 | 显示全部楼层 |阅读模式
服务端与客户端通讯简单代码:
TCPStartup()
$serIP = "123.123.123.123";创建一个不存在的IP,因为并不是每次与服务端都能连接上
$Customerlsn = TCPListen(@IPAddress1, 8843)
While 1
        $Indextext = TCPAccept($Customerlsn)
        $text = TCPRecv($Indextext, 2048)
        If $text <> "" Then
                MsgBox(0,1,"收到信息.")
        EndIf
        Sleep(1000)
        $con = TCPConnect($serIP, 8842)
        If $con <> -1 Then TCPSend($con, "发送信息.")        
WEnd
运行代码后,TCPConnect函数在连接过程中,某些程序会出现假死状态,特别是Word、Excel文档,打开速度不是一般的慢!只有等TCPConnect函数连接结束后Word、Excel文档才能打开!如何解决呢?
 楼主| 发表于 2011-11-21 16:48:10 | 显示全部楼层
没有人发现吗?
发表于 2011-11-21 19:42:36 | 显示全部楼层
有这个问题,暂时无解。。
发表于 2011-11-21 21:15:10 | 显示全部楼层
楼主为什么要用不存在的IP?当然会变慢了
 楼主| 发表于 2011-11-21 23:32:46 | 显示全部楼层
回复 4# xms77


    用来测试,因为并不是每次客户端与服务端都能连接上。
发表于 2011-11-22 00:05:46 | 显示全部楼层
au3阻塞的话会导致系统某些方面卡死。有时候连鼠标也会卡。应该是本身架构的问题。
发表于 2011-11-22 00:08:44 | 显示全部楼层
回复 4# xms77

什么叫当然会变慢?
一个au3的程序在不占用CPU资源,代码中不涉及能影响其他程序的语句时能影响到其他程序,这叫理所当然?
 楼主| 发表于 2011-11-22 07:40:22 | 显示全部楼层
其实不止TCPConnect 函数,其它如InetGet、InetGetSize等函数都会出现此情况。
发表于 2011-11-22 12:26:01 | 显示全部楼层
回复 7# republican
我认为会的,我如果连接一个不存在的FTP服务器,资源管理去会长时间停止响应,一样的道理,tcpconnect连接不上也可能出现类似情况
 楼主| 发表于 2011-11-24 10:47:03 | 显示全部楼层
http://www.autoitx.com/forum.php ... 3&highlight=tcp
这个帖子中,4#有方法,但本人不会用,谁知道吗?贴下代码。
发表于 2011-11-27 01:18:43 | 显示全部楼层
au3阻塞的话会导致系统某些方面卡死。有时候连鼠标也会卡。应该是本身架构的问题。
republican 发表于 2011-11-22 00:05

举个例子看看!
发表于 2011-11-27 01:22:35 | 显示全部楼层
本帖最后由 ceoguang 于 2011-11-27 01:35 编辑
有这个问题,暂时无解。。
itljl 发表于 2011-11-21 19:42

有可能无解吗?
只是高手们不肯出招而已,设置为非阻塞方式连接就会解决你的问题.
看代码!不解释

#AutoIt3Wrapper_UseX64=n
Global Const $SOCKET_ERROR = -1
Global Const $WSADESCRIPTION_LEN = 256
Global Const $WSASYS_STATUS_LEN = 128
Global Const $FIONBIO = 2147772030
Global Const $SD_RECEIVE = 0, $SD_SEND = 1, $SD_BOTH = 2
Global Const $AF_UNSPEC = 0, $AF_INET = 2, $AF_IPX = 6, $AF_APPLETALK = 16, $AF_NETBIOS = 17, $AF_INET6 = 23, $AF_IRDA = 26, $AF_BTH = 32
Global Const $SOCK_STREAM = 1, $SOCK_DGRAM = 2, $SOCK_RAW = 3, $SOCK_RDM = 4, $SOCK_SEQPACKET = 5
Global Const $IPPROTO_ICMP = 1, $IPPROTO_IGMP = 2, $BTHPROTO_RFCOMM = 3, $IPPROTO_TCP = 6, $IPPROTO_UDP = 17, $IPPROTO_ICMPV6 = 58, $IPPROTO_RM = 113

Global Const $tagMIB_IFROW = _
                'WCHAR wszName[256];' & _
                'DWORD dwIndex;' & _
                'DWORD dwType;' & _
                'DWORD dwMtu;' & _
                'DWORD dwSpeed;' & _
                'DWORD dwPhysAddrLen;' & _
                'BYTE  bPhysAddr[8];' & _
                'DWORD dwAdminStatus;' & _
                'DWORD dwOperStatus;' & _
                'DWORD dwLastChange;' & _
                'DWORD dwInOctets;' & _
                'DWORD dwInUcastPkts;' & _
                'DWORD dwInNUcastPkts;' & _
                'DWORD dwInDiscards;' & _
                'DWORD dwInErrors;' & _
                'DWORD dwInUnknownProtos;' & _
                'DWORD dwOutOctets;' & _
                'DWORD dwOutUcastPkts;' & _
                'DWORD dwOutNUcastPkts;' & _
                'DWORD dwOutDiscards;' & _
                'DWORD dwOutErrors;' & _
                'DWORD dwOutQLen;' & _
                'DWORD dwDescrLen;' & _
                'CHAR  bDescr[256];'

Global Const $tagSockaddr_in = 'ushort family;ushort port;long address;char zero[8];'
Global Const $tagWSADATA = 'ushort wVersion;' & _
                'ushort wHighVersion;' & _
                'char szDescription[' & $WSADESCRIPTION_LEN + 1 & '];' & _
                'char szSystemStatus[' & $WSASYS_STATUS_LEN + 1 & '];' & _
                'ushort iMaxSockets;' & _
                'ushort iMaxUdpDg;' & _
                'ptr lpVendorInfo;'
Global Const $tagtimeval = 'int tv_sec;int tv_usec;'
Global Const $tagFD_SET = 'uint;uint[64];'
Global $Ws2_32_dll = -1

Local $mSocket, $iResult, $tSockaddr_in, $tTime, $tFD_SET
Local $iTime

WSAStartup()
If @error Then Exit (MsgBox(16, '错误', '网络初始化失败'))
$mSocket = socket()
If $mSocket = -1 Then Exit (MsgBox(16, '错误', '套字节创建失败'))
$iResult = ioctlsocket($mSocket, $FIONBIO, 1);设置为非阻塞模式
If $iResult <> 0 Then MsgBox(16, '错误', '设置非阻塞模式失败')
$tSockaddr_in = DllStructCreate($tagSockaddr_in)
DllStructSetData($tSockaddr_in, 1, $AF_INET)
DllStructSetData($tSockaddr_in, 2, htons(3456))
DllStructSetData($tSockaddr_in, 3, inet_addr('192.168.188.2'))
connect($mSocket, DllStructGetPtr($tSockaddr_in), DllStructGetSize($tSockaddr_in));此处WSAGetLastError()会产生10035错误,表示一个非阻塞的连接正在进行.这是一个正常的错误,不必理会
$tTime = DllStructCreate($tagtimeval)
DllStructSetData($tTime, 1, 5);5秒 ==============================>>>>单位秒
;DllStructSetData($tTime, 2, 0)0  =============================>>>>>单位微秒
$tFD_SET = DllStructCreate($tagFD_SET)
$iTime = TimerInit()
DllStructSetData($tFD_SET, 1, 1)
DllStructSetData($tFD_SET, 2, $mSocket, 1)
$iResult = _select(0, DllStructGetPtr($tFD_SET), 0, DllStructGetPtr($tTime))
If $iResult <= 0 Then Exit(MsgBox(0, '', '连接超时,程序退出'))
MsgBox(64,'连接成功','耗时:' & TimerDiff($iTime))
ioctlsocket($mSocket, $FIONBIO, 1);设置回阻塞模式

Func MAKEWORD($LoByte, $HiByte)
        Return BitOR($LoByte, 0x100 * $HiByte)
EndFunc   ;==>MAKEWORD

Func MakeLong($LoWord, $HiWord)
        Return BitOR($HiWord * 0x10000, BitAND($LoWord, 0xFFFF))
EndFunc   ;==>MakeLong

Func HiWord($Long)
        Return BitShift($Long, 16)
EndFunc   ;==>HiWord

Func WSAGetLastError()
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $Ret = DllCall($Ws2_32_dll, 'int', 'WSAGetLastError')
        If @error Then SetError(@error)
        Return $Ret[0]
EndFunc   ;==>WSAGetLastError


Func ioctlsocket($hSocket, $iCmd, $pArgp)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $Ret = DllCall($Ws2_32_dll, 'int', 'ioctlsocket', 'uint', $hSocket, 'long', $iCmd, 'ulong*', $pArgp)
        If @error Then Return SetError(@error, 0, -1)
        Return $Ret[0]
EndFunc   ;==>ioctlsocket

Func _select($preadfds, $pWritefds, $pexceptfds, $pTimeout)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $aResult = DllCall($Ws2_32_dll, 'int', 'select', 'int', 0, 'ptr', $preadfds, 'ptr', $pWritefds, 'ptr', $pexceptfds, 'ptr', $pTimeout)
        If @error Then Return SetError(@error)
        Return $aResult[0]
EndFunc   ;==>_select

Func WSAStartup()
        $Ws2_32_dll = DllOpen('Ws2_32.dll')
        If $Ws2_32_dll = -1 Then
                Return SetError(-2, 0, 0)
        EndIf
        
        Local $WSADATA = DllStructCreate($tagWSADATA), $Ret
        $Ret = DllCall($Ws2_32_dll, 'int', 'WSAStartup', 'short', MAKEWORD(2, 2), 'ptr', DllStructGetPtr($WSADATA))
        Return SetError($Ret[0], $WSADATA = 0, $Ret[0] = 0)
EndFunc   ;==>WSAStartup

Func socket($iAddressFamily = $AF_INET, $iType = $SOCK_STREAM, $iProtocol = $IPPROTO_TCP)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $hSocket = DllCall($Ws2_32_dll, 'uint', 'socket', 'int', $iAddressFamily, 'int', $iType, 'int', $iProtocol)
        If @error Then Return SetError(@error, 1, -1)
        Return SetError(WSAGetLastError(), 0, $hSocket[0])
EndFunc   ;==>socket

Func connect($hSocket, $lpsockaddr, $iLen)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $aResult = DllCall($Ws2_32_dll, 'int', 'connect', 'uint', $hSocket, 'ptr', $lpsockaddr, 'int', $iLen)
        If @error Then Return SetError(@error, 1, -1)
        Return SetError(WSAGetLastError(), 0, $aResult[0])
EndFunc   ;==>connect

Func bind($hSocket, $lpsockaddr, $iLen)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $aResult = DllCall($Ws2_32_dll, 'int', 'bind', 'uint', $hSocket, 'ptr', $lpsockaddr, 'int', $iLen)
        If @error Then Return SetError(@error, 1, False)
        Return SetError(WSAGetLastError(), 0, $aResult[0])
EndFunc   ;==>bind

Func listen($hSocket, $ibacklog = 1)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $aResult = DllCall($Ws2_32_dll, 'int', 'listen', 'uint', $hSocket, 'int', $ibacklog)
        If @error Then Return SetError(@error, 1, False)
        Return SetError(WSAGetLastError(), 0, $aResult[0])
EndFunc   ;==>listen

Func accept($hSocket)
        Local $aResult, $tBuffer = DllStructCreate($tagSockaddr_in)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        $aResult = DllCall($Ws2_32_dll, 'int', 'accept', 'uint', $hSocket, 'ptr', DllStructGetPtr($tBuffer), 'int*', DllStructGetSize($tBuffer))
        If @error Then Return SetError(-1, 0, -1)
        Return SetError(WSAGetLastError(), 1, $aResult[0])
EndFunc   ;==>accept

Func _send($hSocket, $lpBuf, $iLen, $iFlags = 0)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $aResult = DllCall($Ws2_32_dll, 'int', 'send', 'uint', $hSocket, 'ptr', $lpBuf, 'int', $iLen, 'int', $iFlags)
        If @error Then Return SetError(-1, 0, @error)
        Return SetError(WSAGetLastError(), 1, $aResult[0])
EndFunc   ;==>_send

Func recv($hSocket, $lpBuf, $iLen, $iFlags = 0)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $aResult = DllCall($Ws2_32_dll, 'int', 'recv', 'uint', $hSocket, 'ptr', $lpBuf, 'int', $iLen, 'int', $iFlags)
        If @error Then Return SetError(-1, 0, @error)
        Return SetError(WSAGetLastError(), 1, $aResult[0])
EndFunc   ;==>recv


Func WSACleanup()
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $aResult = DllCall($Ws2_32_dll, 'int', 'WSACleanup')
        If @error Then Return SetError(-1, 0, @error)
        Return SetError(WSAGetLastError(), 1, $aResult[0])
EndFunc   ;==>WSACleanup

Func closesocket($hSocket)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $aResult = DllCall($Ws2_32_dll, 'int', 'closesocket', 'uint', $hSocket)
        If @error Then Return SetError(-1, 0, @error)
        Return SetError(WSAGetLastError(), 1, $aResult[0])
EndFunc   ;==>closesocket

Func _TCPShutdown($hSocket, $iHow = $SD_BOTH)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $aResult = DllCall($Ws2_32_dll, "int", "shutdown", "uint", $hSocket, "int", $iHow)
        If @error Then Return SetError(1, @error, False)
        If $Ws2_32_dll[0] <> 0 Then Return SetError(2, WSAGetLastError(), False)
        Return True
EndFunc   ;==>_TCPShutdown

Func htons($iPort)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $aResult = DllCall($Ws2_32_dll, 'ushort', 'htons', 'ushort', $iPort)
        If @error Then Return SetError(-1, 0, @error)
        Return SetError(WSAGetLastError(), 1, $aResult[0])
EndFunc   ;==>htons

Func htonl($hostlong)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $aResult = DllCall($Ws2_32_dll, 'ulong', 'htonl', 'ulong', $hostlong)
        If @error Then Return SetError(-1, 0, @error)
        Return SetError(WSAGetLastError(), 1, $aResult[0])
EndFunc   ;==>htonl

Func inet_addr($sIp)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $aResult = DllCall($Ws2_32_dll, 'uint', 'inet_addr', 'str', $sIp)
        If @error Then Return SetError(-1, 0, @error)
        Return SetError(@error, @extended, $aResult[0])
EndFunc   ;==>inet_addr

Func inet_ntoa($iLong)
        If $Ws2_32_dll = -1 Then $Ws2_32_dll = DllOpen('Ws2_32.dll')
        Local $aResult = DllCall($Ws2_32_dll, 'str', 'inet_ntoa', 'int', $iLong)
        If @error Then Return SetError(-1, 0, @error)
        Return SetError(@error, @extended, $aResult[0])
EndFunc   ;==>inet_ntoa

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2011-11-27 01:27:11 | 显示全部楼层
上面的代码不知道为什么居然不支持X64,而C++即使编译为X64却也表示毫无鸭莉,求解释!!!
所以,如果要部署使用,请不要注释掉
#AutoIt3Wrapper_UseX64=n
,以免在X64上select模型设置失败.
因为经测,在X64上select会返回WSAENOTSOCK错误
发表于 2011-11-29 06:25:26 | 显示全部楼层
回复 11# ceoguang

au3后台联网,然后玩魔兽就会发现鼠标经常被block掉,与阻塞时间有关。
发表于 2011-11-29 12:25:57 | 显示全部楼层
楼上的上代码来测试下
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-4 01:35 , Processed in 0.088768 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表