TCPSend & TCPRecv 中文汉字字符串[已解决]
本帖最后由 love5173 于 2011-5-5 17:54 编辑在帮助文件里这两个函数下面都有例子,我以这两个例子分别放在我两台机器上测试,发送中文字符串的时候只能接受到一半;
根据P版说的:调用TCPSend发送数据时,AutoIt实际会在其内部 调用Ws2_32.send函数,如果发送的数据是文本字符串,那么在调用Ws2_32.send函数之前,AutoIt会先计算字符串的长度,注意这里的“字符串长度”,AutoIt内部会计算成字符数量,而不是字符串所占用的字节长度,而Ws2_32.send函数需要的是“字节长度”。比如发送“你好”,这个字符串所占用的字节数量是4(因为1个全角字符占用2个字节),可是AutoIt内部却将2(字符数量)传递给了send函数,所以实际被 复制进发送缓存区中的只有前2个字节,也就是只有“你”。
解决方法如楼主1#说的,发送前将文本转换为二进制,接收后再次转换。或者,自己调用系统函数。
我在两边都加了2进制字符转换,转换后字符也能显示,但是还是只显示一半的汉字。请问是什么原因,有没有熟悉这块的给个简单的例子!不胜感激! 以前我曾做过这类程序.是用_StringEncrypt解决的这问题.
代码有些乱,你凑和着看看相关的部分吧.
;消息发送端
#Region AutoIt3Wrapper 预编译参数(常用参数)
#AutoIt3Wrapper_UseAnsi=N ;编码
#AutoIt3Wrapper_Icon= ;图标
#AutoIt3Wrapper_OutFile= ;输出文件名
#AutoIt3Wrapper_OutFile_Type=exe ;文件类型
#AutoIt3Wrapper_Compression=4 ;压缩等级
#AutoIt3Wrapper_UseUpx=y ;使用压缩
#AutoIt3Wrapper_Res_Comment= ;注释
#AutoIt3Wrapper_Res_Description= ;详细信息
#AutoIt3Wrapper_Res_Fileversion=0.1.0.0000 Alpha ;文件版本
#AutoIt3Wrapper_Res_FileVersion_AutoIncrement= ;自动更新版本
#AutoIt3Wrapper_Res_LegalCopyright=eSoul ;版权
#AutoIt3Wrapper_Change2CUI=N ;修改输出的程序为CUI(控制台程序)
;#AutoIt3Wrapper_Res_Field=AutoIt Version|%AutoItVer% ;自定义资源段
;#AutoIt3Wrapper_Run_Tidy= ;脚本整理
;#AutoIt3Wrapper_Run_Obfuscator= ;代码迷惑
;#AutoIt3Wrapper_Run_AU3Check= ;语法检查
;#AutoIt3Wrapper_Run_Before= ;运行前
;#AutoIt3Wrapper_Run_After= ;运行后
#EndRegion AutoIt3Wrapper 预编译参数设置完成
#cs ____________________________________
Au3 版本: AutoIt v3.3.6.1
软件语言: 简体中文
执行环境: WinXP
创建日期: 2010-07-18
修改日期:
脚本作者: eSoul
Email: Easied@QQ.com
QQ/TM: 19519549
脚本版本: 0.1.1.0718
脚本功能:
#ce _______________脚本开始_________________
#include <Date.au3>
#NoTrayIcon
FileChangeDir(@ScriptDir)
;==============================================
; 启动控制
;==============================================
fn_RepeatedStartControl("悄悄话检测系统-DrClient")
;==============================================
; 参数预设
;==============================================
$TargetIP = "192.168.0.127"
$nPort = 18666
$sComputerName = "DrClient" & StringRight(@ComputerName, 2)
$sPromptString = "有人联系您,请尽快回复!"
While 1
If fn_DrCheckIsCalled() = 1 Then
;==============================================
; 启动传输控制协议服务
;==============================================
TCPStartUp()
;==============================================
; 连接到监听 "套接字"
;==============================================
$ConnectedSocket = TCPConnect($TargetIP, $nPort)
If @error Then
MsgBox(4112, "差错", "TCPConnect 以 WSA 差错失败: " & @error)
Else
;==============================================
; 发送数据
;==============================================
$sPromptTime = StringRight("0000"&@YEAR, 4)&"-"&StringRight("00"&@MON, 2)&"-"&StringRight("00"&@MDAY, 2)&"|"&StringRight("00"&@HOUR, 2)&":"&StringRight("00"&@MIN, 2)&":"&StringRight("00"&@SEC, 2)
$sReturnString = $sComputerName &"|"& $sPromptTime &"|"& $sPromptString
$sReturnString = _StringEncrypt(1, $sReturnString, "123", 1)
$n = TCPSend($ConnectedSocket, $sReturnString)
;==============================================
; 关闭监听 "套接字"连接
;==============================================
TCPCloseSocket($ConnectedSocket)
Sleep(27*1000)
EndIf
EndIf
Sleep(3000)
WEnd
Exit
Func fn_DrCheckIsCalled()
$IsCalled = 0
For $i = 346 To 481 Step 15
If PixelGetColor(24, $i) = 3269425 Then
$IsCalled = 1
ExitLoop
EndIf
Next
Return($IsCalled)
EndFunc ;==>fn_DrCheckIsCalled
; #FUNCTION# ====================================================================================================================
; Name...........: _StringEncrypt
; Description ...: An RC4 based string encryption function.
; Syntax.........: _StringEncrypt($i_Encrypt, $s_EncryptText, $s_EncryptPassword[, $i_EncryptLevel = 1])
; Parameters ....: $i_Encrypt - 1 to encrypt, 0 to decrypt.
; $s_EncryptText - Text to encrypt/decrypt.
; $s_EncryptPassword - Password to encrypt/decrypt with.
; $i_EncryptLevel - Optional: Level to encrypt/decrypt. Default = 1
; $i_Flag - Optional: Changes how the string is stored as binary:
; flag = 1 (default), binary data is ANSI
; flag = 2, binary data is UTF16 Little Endian
; flag = 3, binary data is UTF16 Big Endian
; flag = 4, binary data is UTF8
; Return values .: Success - The Encrypted/Decrypted string.
; Failure - Blank string and @error = 1
; Author ........: Wes Wolfe-Wolvereness <Weswolf at aol dot com>
; Modified.......:
; Remarks .......: WARNING: This function has an extreme timespan if the encryption level or encrypted string are too large!
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _StringEncrypt($i_Encrypt, $s_EncryptText, $s_EncryptPassword, $i_EncryptLevel = 1, $i_Flag = 1)
If $i_Encrypt <> 0 And $i_Encrypt <> 1 Then
SetError(1)
Return ''
ElseIf $s_EncryptText = '' Or $s_EncryptPassword = '' Then
SetError(1)
Return ''
Else
If Number($i_EncryptLevel) <= 0 Or Int($i_EncryptLevel) <> $i_EncryptLevel Then $i_EncryptLevel = 1
Local $b_EncryptText
Local $b_EncryptPassword
Local $v_EncryptModified
Local $i_EncryptCountH
Local $i_EncryptCountG
Local $v_EncryptSwap
Local $av_EncryptBox
Local $i_EncryptCountA
Local $i_EncryptCountB
Local $i_EncryptCountC
Local $i_EncryptCountD
Local $i_EncryptCountE
Local $v_EncryptCipher
Local $v_EncryptCipherBy
$b_EncryptText = StringToBinary($s_EncryptText, $i_Flag)
$b_EncryptPassword = StringToBinary($s_EncryptPassword, $i_Flag)
If $i_Encrypt = 1 Then
For $i_EncryptCountF = 0 To $i_EncryptLevel Step 1
$i_EncryptCountG = ''
$i_EncryptCountH = ''
$v_EncryptModified = Binary('')
For $i_EncryptCountG = 1 To BinaryLen($b_EncryptText)
If $i_EncryptCountH = BinaryLen($b_EncryptPassword) Then
$i_EncryptCountH = 1
Else
$i_EncryptCountH += 1
EndIf
$v_EncryptModified = Binary($v_EncryptModified) & Binary('0x' & Hex(BitXOR(BinaryMid($b_EncryptText, $i_EncryptCountG, 1), BinaryMid($b_EncryptPassword, $i_EncryptCountH, 1), 255),2))
Next
$b_EncryptText = $v_EncryptModified
$i_EncryptCountA = ''
$i_EncryptCountB = 0
$i_EncryptCountC = ''
$i_EncryptCountD = ''
$i_EncryptCountE = ''
$v_EncryptCipherBy = ''
$v_EncryptCipher = ''
$v_EncryptSwap = ''
$av_EncryptBox = ''
Local $av_EncryptBox
For $i_EncryptCountA = 0 To 255
$av_EncryptBox[$i_EncryptCountA] = Dec(StringTrimLeft(BinaryMid($b_EncryptPassword, Mod($i_EncryptCountA, BinaryLen($b_EncryptPassword)) + 1, 1),2))
$av_EncryptBox[$i_EncryptCountA] = $i_EncryptCountA
Next
For $i_EncryptCountA = 0 To 255
$i_EncryptCountB = Mod(($i_EncryptCountB + $av_EncryptBox[$i_EncryptCountA] + $av_EncryptBox[$i_EncryptCountA]), 256)
$v_EncryptSwap = $av_EncryptBox[$i_EncryptCountA]
$av_EncryptBox[$i_EncryptCountA] = $av_EncryptBox[$i_EncryptCountB]
$av_EncryptBox[$i_EncryptCountB] = $v_EncryptSwap
Next
For $i_EncryptCountA = 1 To BinaryLen($b_EncryptText)
$i_EncryptCountC = Mod(($i_EncryptCountC + 1), 256)
$i_EncryptCountD = Mod(($i_EncryptCountD + $av_EncryptBox[$i_EncryptCountC]), 256)
$i_EncryptCountE = $av_EncryptBox + $av_EncryptBox[$i_EncryptCountD]), 256) ]
$v_EncryptCipherBy = BitXOR(BinaryMid($b_EncryptText, $i_EncryptCountA, 1), $i_EncryptCountE)
$v_EncryptCipher = Binary($v_EncryptCipher) & Binary('0x' & Hex($v_EncryptCipherBy, 2))
Next
$b_EncryptText = StringTrimLeft($v_EncryptCipher,2)
Next
Else
$b_EncryptText = $s_EncryptText
For $i_EncryptCountF = 0 To $i_EncryptLevel Step 1
$b_EncryptText = Binary('0x' & $b_EncryptText)
$i_EncryptCountB = 0
$i_EncryptCountC = ''
$i_EncryptCountD = ''
$i_EncryptCountE = ''
$v_EncryptCipherBy = ''
$v_EncryptCipher = ''
$v_EncryptSwap = ''
$av_EncryptBox = ''
Local $av_EncryptBox
For $i_EncryptCountA = 0 To 255
$av_EncryptBox[$i_EncryptCountA] = Dec(StringTrimLeft(BinaryMid($b_EncryptPassword, Mod($i_EncryptCountA, BinaryLen($b_EncryptPassword)) + 1, 1),2))
$av_EncryptBox[$i_EncryptCountA] = $i_EncryptCountA
Next
For $i_EncryptCountA = 0 To 255
$i_EncryptCountB = Mod(($i_EncryptCountB + $av_EncryptBox[$i_EncryptCountA] + $av_EncryptBox[$i_EncryptCountA]), 256)
$v_EncryptSwap = $av_EncryptBox[$i_EncryptCountA]
$av_EncryptBox[$i_EncryptCountA] = $av_EncryptBox[$i_EncryptCountB]
$av_EncryptBox[$i_EncryptCountB] = $v_EncryptSwap
Next
For $i_EncryptCountA = 1 To BinaryLen($b_EncryptText)
$i_EncryptCountC = Mod(($i_EncryptCountC + 1), 256)
$i_EncryptCountD = Mod(($i_EncryptCountD + $av_EncryptBox[$i_EncryptCountC]), 256)
$i_EncryptCountE = $av_EncryptBox + $av_EncryptBox[$i_EncryptCountD]), 256) ]
$v_EncryptCipherBy = BitXOR(BinaryMid($b_EncryptText, $i_EncryptCountA, 1), $i_EncryptCountE)
$v_EncryptCipher = Binary($v_EncryptCipher) & Binary('0x' & Hex($v_EncryptCipherBy,2))
Next
$b_EncryptText = $v_EncryptCipher
$i_EncryptCountG = ''
$i_EncryptCountH = ''
$v_EncryptModified = ''
For $i_EncryptCountG = 1 To BinaryLen($b_EncryptText)
If $i_EncryptCountH = BinaryLen($b_EncryptPassword) Then
$i_EncryptCountH = 1
Else
$i_EncryptCountH += 1
EndIf
$v_EncryptModified = Binary($v_EncryptModified) & Binary('0x' & Hex(BitXOR(BinaryMid($b_EncryptText, $i_EncryptCountG, 1), BinaryMid($b_EncryptPassword, $i_EncryptCountH, 1), 255),2))
Next
$b_EncryptText = BinaryToString($v_EncryptModified, $i_Flag)
Next
EndIf
Return $b_EncryptText
EndIf
EndFunc ;==>_StringEncrypt
; ===============================================================================================================================
; 函数名称: fn_RepeatedStartControl
; 函数版本: 0.1.1.0718 Alpha
; 函数说明: 控制程序的多重启动
; 函数语法: fn_RepeatedStartControl($iMode, $sFinalTitle)
; 参数说明: $sFinalTitle -- 程序的最终标题
; $iMode -- 多重启动模式
; |0 - 允许本程序多重启动
; |1 - 禁止本程序重复执行
; |2 - 关闭重复的程序后,再继续执行本程序
; 返 回 值: 无返回值
; 函数作者: eSoul
; 创建日期: 2009-04-20
; 更新日期: 2010-07-18
; 函数改良:
; 修改说明:
; 相关函数:
; 链接网页:
; 函数示例:
; Example 1:
; $sFinalTitle = "多目录同步工具 v0.1.0.0420 Alpha"
; fn_RepeatedStartControl($sFinalTitle)
;
; Example 2:
; $sFinalTitle = "多目录同步工具 v0.1.0.0420 Alpha"
; $iMode = 2
; fn_RepeatedStartControl($sFinalTitle, $iMode)
; ===============================================================================================================================
Func fn_RepeatedStartControl($sFinalTitle, $iMode = 1)
;=================================================
; 定义局部变量
; $IsWindowExist -- 判断程序是否启动的变量
;=================================================
Local $IsWindowExist
;=================================================
; 若没检查到程序多重启动,则返回继续操作
;=================================================
$IsWindowExist = WinList($sFinalTitle)
If $IsWindowExist = 0 Then
AutoItWinSetTitle($sFinalTitle)
Return
EndIf
Select
;=================================================
; 允许本程序重复执行
;=================================================
Case $iMode = 0
AutoItWinSetTitle($sFinalTitle)
Return
;=================================================
; 禁止本程序重复执行
;=================================================
Case $iMode = 1
MsgBox(262144+16, "警告", "程序已经在运行,请勿重复执行!" & @LF & "请点击确定退出")
Exit
;=================================================
; 关闭重复的程序后,再继续执行本程序
;=================================================
Case $iMode = 2
Do
WinClose($sFinalTitle)
$IsWindowExist = WinList($sFinalTitle)
Sleep(1000)
Until $IsWindowExist = 0
AutoItWinSetTitle($sFinalTitle)
Return
EndSelect
EndFunc ;==>fn_RepeatedStartControl
;接收端
#Region AutoIt3Wrapper 预编译参数(常用参数)
#AutoIt3Wrapper_UseAnsi=N ;编码
#AutoIt3Wrapper_Icon= ;图标
#AutoIt3Wrapper_OutFile= ;输出文件名
#AutoIt3Wrapper_OutFile_Type=exe ;文件类型
#AutoIt3Wrapper_Compression=4 ;压缩等级
#AutoIt3Wrapper_UseUpx=y ;使用压缩
#AutoIt3Wrapper_Res_Comment= ;注释
#AutoIt3Wrapper_Res_Description= ;详细信息
#AutoIt3Wrapper_Res_Fileversion=0.1.0.0000 Alpha ;文件版本
#AutoIt3Wrapper_Res_FileVersion_AutoIncrement= ;自动更新版本
#AutoIt3Wrapper_Res_LegalCopyright=eSoul ;版权
#AutoIt3Wrapper_Change2CUI=N ;修改输出的程序为CUI(控制台程序)
;#AutoIt3Wrapper_Res_Field=AutoIt Version|%AutoItVer% ;自定义资源段
;#AutoIt3Wrapper_Run_Tidy= ;脚本整理
;#AutoIt3Wrapper_Run_Obfuscator= ;代码迷惑
;#AutoIt3Wrapper_Run_AU3Check= ;语法检查
;#AutoIt3Wrapper_Run_Before= ;运行前
;#AutoIt3Wrapper_Run_After= ;运行后
#EndRegion AutoIt3Wrapper 预编译参数设置完成
#cs ____________________________________
Au3 版本: AutoIt v3.3.6.1
软件语言: 简体中文
执行环境: WinXP
创建日期: 2010-07-18
修改日期:
脚本作者: eSoul
Email: Easied@QQ.com
QQ/TM: 19519549
脚本版本: 0.1.2.0719
脚本功能:
#ce _______________脚本开始_________________
#include <Date.au3>
#NoTrayIcon
FileChangeDir(@ScriptDir)
;==============================================
; 启动控制
;==============================================
If $CmdLine > 0 Then
$IsWindowExist = WinList($CmdLine)
If $IsWindowExist = 0 Then
MsgBox(4096, $CmdLine, $CmdLine &@CRLF& $CmdLine &@CRLF& $CmdLine)
Else
$sExistTexts = WinGetText($CmdLine, "")
$sExistTexts = StringRight($sExistTexts, StringLen($sExistTexts)-3)
$sExistTexts = StringLeft ($sExistTexts, StringLen($sExistTexts)-StringLen("有人联系您,请尽快回复!")-3)
If StringInStr($sExistTexts, $CmdLine) Then
$sNewTexts = $sExistTexts &@CRLF& $CmdLine &@CRLF& $CmdLine
Else
$sNewTexts = $sExistTexts &@CRLF& $CmdLine &@CRLF& $CmdLine &@CRLF& $CmdLine
EndIf
ControlClick($CmdLine, "有人联系您", "", "left", 2)
MsgBox(4096, $CmdLine, $sNewTexts)
EndIf
Exit
EndIf
fn_RepeatedStartControl("悄悄话检测系统-Console")
;==============================================
; 参数预设
;==============================================
$LocalIP = @IPAddress1
$nPort = 18666
;==============================================
; 启动传输控制协议服务
;==============================================
TCPStartUp()
;==============================================
; 建立监听 "套接字"
;==============================================
$MainSocket = TCPListen($LocalIP, $nPort, 100)
If $MainSocket = -1 Then
MsgBox(4096, "", "TCPListen失败,程序退出!")
Exit
EndIf
;==============================================
; 等待客户连接
;==============================================
While 1
$ConnectedSocket = TCPAccept($MainSocket)
If $ConnectedSocket >= 0 Then
While 1
$sRecv = TCPRecv($ConnectedSocket, 2048)
If @error Then ExitLoop
If $sRecv <> "" Then
$sPrompt = StringSplit(_StringEncrypt(0, $sRecv, "123", 1), "|")
Run(@ScriptFullPath &" "& $sPrompt &"?Msg "& $sPrompt &" "& $sPrompt &" "& $sPrompt, @ScriptDir)
EndIf
WEnd
EndIf
Wend
Exit
Func fn_PromptBoX($sPrompt)
fn_RepeatedStartControl($sPrompt & "的悄悄话提示")
MsgBox(4096, $sPrompt, $sPrompt &@CRLF& $sPrompt &@CRLF& $sPrompt)
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: _StringEncrypt
; Description ...: An RC4 based string encryption function.
; Syntax.........: _StringEncrypt($i_Encrypt, $s_EncryptText, $s_EncryptPassword[, $i_EncryptLevel = 1])
; Parameters ....: $i_Encrypt - 1 to encrypt, 0 to decrypt.
; $s_EncryptText - Text to encrypt/decrypt.
; $s_EncryptPassword - Password to encrypt/decrypt with.
; $i_EncryptLevel - Optional: Level to encrypt/decrypt. Default = 1
; $i_Flag - Optional: Changes how the string is stored as binary:
; flag = 1 (default), binary data is ANSI
; flag = 2, binary data is UTF16 Little Endian
; flag = 3, binary data is UTF16 Big Endian
; flag = 4, binary data is UTF8
; Return values .: Success - The Encrypted/Decrypted string.
; Failure - Blank string and @error = 1
; Author ........: Wes Wolfe-Wolvereness <Weswolf at aol dot com>
; Modified.......:
; Remarks .......: WARNING: This function has an extreme timespan if the encryption level or encrypted string are too large!
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _StringEncrypt($i_Encrypt, $s_EncryptText, $s_EncryptPassword, $i_EncryptLevel = 1, $i_Flag = 1)
If $i_Encrypt <> 0 And $i_Encrypt <> 1 Then
SetError(1)
Return ''
ElseIf $s_EncryptText = '' Or $s_EncryptPassword = '' Then
SetError(1)
Return ''
Else
If Number($i_EncryptLevel) <= 0 Or Int($i_EncryptLevel) <> $i_EncryptLevel Then $i_EncryptLevel = 1
Local $b_EncryptText
Local $b_EncryptPassword
Local $v_EncryptModified
Local $i_EncryptCountH
Local $i_EncryptCountG
Local $v_EncryptSwap
Local $av_EncryptBox
Local $i_EncryptCountA
Local $i_EncryptCountB
Local $i_EncryptCountC
Local $i_EncryptCountD
Local $i_EncryptCountE
Local $v_EncryptCipher
Local $v_EncryptCipherBy
$b_EncryptText = StringToBinary($s_EncryptText, $i_Flag)
$b_EncryptPassword = StringToBinary($s_EncryptPassword, $i_Flag)
If $i_Encrypt = 1 Then
For $i_EncryptCountF = 0 To $i_EncryptLevel Step 1
$i_EncryptCountG = ''
$i_EncryptCountH = ''
$v_EncryptModified = Binary('')
For $i_EncryptCountG = 1 To BinaryLen($b_EncryptText)
If $i_EncryptCountH = BinaryLen($b_EncryptPassword) Then
$i_EncryptCountH = 1
Else
$i_EncryptCountH += 1
EndIf
$v_EncryptModified = Binary($v_EncryptModified) & Binary('0x' & Hex(BitXOR(BinaryMid($b_EncryptText, $i_EncryptCountG, 1), BinaryMid($b_EncryptPassword, $i_EncryptCountH, 1), 255),2))
Next
$b_EncryptText = $v_EncryptModified
$i_EncryptCountA = ''
$i_EncryptCountB = 0
$i_EncryptCountC = ''
$i_EncryptCountD = ''
$i_EncryptCountE = ''
$v_EncryptCipherBy = ''
$v_EncryptCipher = ''
$v_EncryptSwap = ''
$av_EncryptBox = ''
Local $av_EncryptBox
For $i_EncryptCountA = 0 To 255
$av_EncryptBox[$i_EncryptCountA] = Dec(StringTrimLeft(BinaryMid($b_EncryptPassword, Mod($i_EncryptCountA, BinaryLen($b_EncryptPassword)) + 1, 1),2))
$av_EncryptBox[$i_EncryptCountA] = $i_EncryptCountA
Next
For $i_EncryptCountA = 0 To 255
$i_EncryptCountB = Mod(($i_EncryptCountB + $av_EncryptBox[$i_EncryptCountA] + $av_EncryptBox[$i_EncryptCountA]), 256)
$v_EncryptSwap = $av_EncryptBox[$i_EncryptCountA]
$av_EncryptBox[$i_EncryptCountA] = $av_EncryptBox[$i_EncryptCountB]
$av_EncryptBox[$i_EncryptCountB] = $v_EncryptSwap
Next
For $i_EncryptCountA = 1 To BinaryLen($b_EncryptText)
$i_EncryptCountC = Mod(($i_EncryptCountC + 1), 256)
$i_EncryptCountD = Mod(($i_EncryptCountD + $av_EncryptBox[$i_EncryptCountC]), 256)
$i_EncryptCountE = $av_EncryptBox + $av_EncryptBox[$i_EncryptCountD]), 256) ]
$v_EncryptCipherBy = BitXOR(BinaryMid($b_EncryptText, $i_EncryptCountA, 1), $i_EncryptCountE)
$v_EncryptCipher = Binary($v_EncryptCipher) & Binary('0x' & Hex($v_EncryptCipherBy, 2))
Next
$b_EncryptText = StringTrimLeft($v_EncryptCipher,2)
Next
Else
$b_EncryptText = $s_EncryptText
For $i_EncryptCountF = 0 To $i_EncryptLevel Step 1
$b_EncryptText = Binary('0x' & $b_EncryptText)
$i_EncryptCountB = 0
$i_EncryptCountC = ''
$i_EncryptCountD = ''
$i_EncryptCountE = ''
$v_EncryptCipherBy = ''
$v_EncryptCipher = ''
$v_EncryptSwap = ''
$av_EncryptBox = ''
Local $av_EncryptBox
For $i_EncryptCountA = 0 To 255
$av_EncryptBox[$i_EncryptCountA] = Dec(StringTrimLeft(BinaryMid($b_EncryptPassword, Mod($i_EncryptCountA, BinaryLen($b_EncryptPassword)) + 1, 1),2))
$av_EncryptBox[$i_EncryptCountA] = $i_EncryptCountA
Next
For $i_EncryptCountA = 0 To 255
$i_EncryptCountB = Mod(($i_EncryptCountB + $av_EncryptBox[$i_EncryptCountA] + $av_EncryptBox[$i_EncryptCountA]), 256)
$v_EncryptSwap = $av_EncryptBox[$i_EncryptCountA]
$av_EncryptBox[$i_EncryptCountA] = $av_EncryptBox[$i_EncryptCountB]
$av_EncryptBox[$i_EncryptCountB] = $v_EncryptSwap
Next
For $i_EncryptCountA = 1 To BinaryLen($b_EncryptText)
$i_EncryptCountC = Mod(($i_EncryptCountC + 1), 256)
$i_EncryptCountD = Mod(($i_EncryptCountD + $av_EncryptBox[$i_EncryptCountC]), 256)
$i_EncryptCountE = $av_EncryptBox + $av_EncryptBox[$i_EncryptCountD]), 256) ]
$v_EncryptCipherBy = BitXOR(BinaryMid($b_EncryptText, $i_EncryptCountA, 1), $i_EncryptCountE)
$v_EncryptCipher = Binary($v_EncryptCipher) & Binary('0x' & Hex($v_EncryptCipherBy,2))
Next
$b_EncryptText = $v_EncryptCipher
$i_EncryptCountG = ''
$i_EncryptCountH = ''
$v_EncryptModified = ''
For $i_EncryptCountG = 1 To BinaryLen($b_EncryptText)
If $i_EncryptCountH = BinaryLen($b_EncryptPassword) Then
$i_EncryptCountH = 1
Else
$i_EncryptCountH += 1
EndIf
$v_EncryptModified = Binary($v_EncryptModified) & Binary('0x' & Hex(BitXOR(BinaryMid($b_EncryptText, $i_EncryptCountG, 1), BinaryMid($b_EncryptPassword, $i_EncryptCountH, 1), 255),2))
Next
$b_EncryptText = BinaryToString($v_EncryptModified, $i_Flag)
Next
EndIf
Return $b_EncryptText
EndIf
EndFunc ;==>_StringEncrypt
; ===============================================================================================================================
; 函数名称: fn_RepeatedStartControl
; 函数版本: 0.1.1.0718 Alpha
; 函数说明: 控制程序的多重启动
; 函数语法: fn_RepeatedStartControl($iMode, $sFinalTitle)
; 参数说明: $sFinalTitle -- 程序的最终标题
; $iMode -- 多重启动模式
; |0 - 允许本程序多重启动
; |1 - 禁止本程序重复执行
; |2 - 关闭重复的程序后,再继续执行本程序
; 返 回 值: 无返回值
; 函数作者: eSoul
; 创建日期: 2009-04-20
; 更新日期: 2010-07-18
; 函数改良:
; 修改说明:
; 相关函数:
; 链接网页:
; 函数示例:
; Example 1:
; $sFinalTitle = "多目录同步工具 v0.1.0.0420 Alpha"
; fn_RepeatedStartControl($sFinalTitle)
;
; Example 2:
; $sFinalTitle = "多目录同步工具 v0.1.0.0420 Alpha"
; $iMode = 2
; fn_RepeatedStartControl($sFinalTitle, $iMode)
; ===============================================================================================================================
Func fn_RepeatedStartControl($sFinalTitle, $iMode = 1)
;=================================================
; 定义局部变量
; $IsWindowExist -- 判断程序是否启动的变量
;=================================================
Local $IsWindowExist
;=================================================
; 若没检查到程序多重启动,则返回继续操作
;=================================================
$IsWindowExist = WinList($sFinalTitle)
If $IsWindowExist = 0 Then
AutoItWinSetTitle($sFinalTitle)
Return
EndIf
Select
;=================================================
; 允许本程序重复执行
;=================================================
Case $iMode = 0
AutoItWinSetTitle($sFinalTitle)
Return
;=================================================
; 禁止本程序重复执行
;=================================================
Case $iMode = 1
MsgBox(262144+16, "警告", "程序已经在运行,请勿重复执行!" & @LF & "请点击确定退出")
Exit
;=================================================
; 关闭重复的程序后,再继续执行本程序
;=================================================
Case $iMode = 2
Do
WinClose($sFinalTitle)
$IsWindowExist = WinList($sFinalTitle)
Sleep(1000)
Until $IsWindowExist = 0
AutoItWinSetTitle($sFinalTitle)
Return
EndSelect
EndFunc ;==>fn_RepeatedStartControl
你是怎么转换的?贴代码来看看。 发送端Opt('MustDeclareVars', 1)
;==============================================
;==============================================
;用户端! 启动服务器之后启动用户端!!!!!!!!!!!!!!!
;==============================================
;==============================================
Example()
Func Example()
; 设置一些常用信息
;--------------------------
Local $ConnectedSocket, $szData
; 设置 $szIPADDRESS 为本地服务器. 这里使用个人电脑名称转换为 IP 地址
; Local $szServerPC = @ComputerName
; Local $szIPADDRESS = TCPNameToIP($szServerPC)
Local $szIPADDRESS ="192.168.0.249"
Local $nPORT = 33891
; 开始 TCP 服务
;==============================================
TCPStartup()
; 初始化一个描述连接的变量
;==============================================
$ConnectedSocket = -1
;尝试连接到服务器IP的 33891 端口.
;=======================================================
$ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)
; 如果发生了错误... 则显示出来
If @error Then
MsgBox(4112, "错误", "TCPConnect 失败于 WSA 错误: " & @error)
; 如果这里没有错误,就循环一个 inputbox 函数,用于发送数据到服务器
Else
;不断循环,每次将询问发送什么数据给服务器
While 1
; 使用 InputBox 函数得到要发送的数据
$szData = InputBox("发送数据给服务器", @LF & @LF & "输入发送给服务器的数据:")
; 如果点击了 InputBox 函数显示输入框的取消按钮,或者使用一个空数据则退出这个循环
If @error Or $szData = "" Then ExitLoop
; 我们确保在 $szData 中有数据... 然后尝试通过连接发送数据.
$szData=StringToBinary($szData,4)
TCPSend($ConnectedSocket, $szData)
; 如果发送失败(@error)将断开连接
;----------------------------------------------------------------
If @error Then ExitLoop
;MsgBox(0,"",$a)
WEnd
EndIf
EndFunc ;==>Example接受端#include <GUIConstantsEx.au3>
Opt('MustDeclareVars', 1)
;==============================================
;==============================================
;首先启动服务器 !!!!!!!!!!!!!!!
;==============================================
;==============================================
Example()
Func Example()
; 设置一些可再用的信息
; 在这里设置您的公共IP地址 (@IPAddress1).
; Local $szServerPC = @ComputerName
; Local $szIPADDRESS = TCPNameToIP($szServerPC)
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891
Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
Local $msg, $recv
; 启动 TCP 服务
;==============================================
TCPStartup()
; 建立监听套接字 "SOCKET".
; 使用您的IP地址和端口 33891.
;==============================================
$MainSocket = TCPListen($szIPADDRESS, $nPORT)
; 如果套接字创建失败, 则退出.
If $MainSocket = -1 Then Exit
; 创建消息 GUI
;==============================================
$GOOEY = GUICreate("我的服务器 (IP: " & $szIPADDRESS & ")", 300, 200)
$edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
GUISetState()
; 初始化一个代表连接的变量,
;==============================================
$ConnectedSocket = -1
;等待和接受连接
;==============================================
Do
$ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1
; 获取已连接用户端接的 IP
$szIP_Accepted = SocketToIP($ConnectedSocket)
; 消息循环 GUI
;==============================================
While 1
$msg = GUIGetMsg()
; 关闭 GUI
;--------------------
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
; 尝试最多接收 2048 字节
;----------------------------------------------------------------
$recv = BinaryToString(TCPRecv($ConnectedSocket, 2048),4)
; 如果接受失败, 则断开套接字 "socket"
;----------------------------------------------------------------
If @error Then ExitLoop
; 用我们接收的数据更新编辑控件
;----------------------------------------------------------------
If $recv <> "" Then GUICtrlSetData($edit, _
$szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
WEnd
If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
TCPShutdown()
EndFunc ;==>Example
; 函数将从一个连接套接字返回IP地址
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
Local $sockaddr, $aRet
$sockaddr = DllStructCreate("short;ushort;uint;char")
$aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
"ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
If Not @error And $aRet = 0 Then
$aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
If Not @error Then $aRet = $aRet
Else
$aRet = 0
EndIf
$sockaddr = 0
Return $aRet
EndFunc ;==>SocketToIP
大概是这么改的,因为另一边得代码在另一台机器 不敢保证一定跟原来的一样 回复 3# pusofalse
P版你说可以自己调用系统函数,是怎么个调用法? 回复 4# love5173
TCPRecv函数的第3个参数被你无视了啊。 - -|||
回复 5# love5173
自己计算字符串的字节长度,自己调用send函数。 回复 6# pusofalse
问题解决了,感谢P版,其实我改的里面还有个错误,二进制转化的位置错了,造成了下面的@error 不能正常表示原来的意思,现在我把更改完的代码发一下,虽然都是小问题,希望给以后发现这个问题的人一点帮助
接收端
#include <GUIConstantsEx.au3>
Opt('MustDeclareVars', 1)
;==============================================
;==============================================
;首先启动服务器 !!!!!!!!!!!!!!!
;==============================================
;==============================================
Example()
Func Example()
; 设置一些可再用的信息
; 在这里设置您的公共IP地址 (@IPAddress1).
; Local $szServerPC = @ComputerName
; Local $szIPADDRESS = TCPNameToIP($szServerPC)
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891
Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
Local $msg, $recv
; 启动 TCP 服务
;==============================================
TCPStartup()
; 建立监听套接字 "SOCKET".
; 使用您的IP地址和端口 33891.
;==============================================
$MainSocket = TCPListen($szIPADDRESS, $nPORT)
; 如果套接字创建失败, 则退出.
If $MainSocket = -1 Then Exit
; 创建消息 GUI
;==============================================
$GOOEY = GUICreate("我的服务器 (IP: " & $szIPADDRESS & ")", 300, 200)
$edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
GUISetState()
; 初始化一个代表连接的变量,
;==============================================
$ConnectedSocket = -1
;等待和接受连接
;==============================================
Do
$ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1
; 获取已连接用户端接的 IP
$szIP_Accepted = SocketToIP($ConnectedSocket)
; 消息循环 GUI
;==============================================
While 1
$msg = GUIGetMsg()
; 关闭 GUI
;--------------------
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
; 尝试最多接收 2048 字节
;----------------------------------------------------------------
$recv = TCPRecv($ConnectedSocket, 2048,1)
; 如果接受失败, 则断开套接字 "socket"
;----------------------------------------------------------------
If @error Then ExitLoop
$recv=BinaryToString($recv,4)
; 用我们接收的数据更新编辑控件
;----------------------------------------------------------------
If $recv <> "" Then GUICtrlSetData($edit, _
$szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
WEnd
If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
TCPShutdown()
EndFunc ;==>Example
; 函数将从一个连接套接字返回IP地址
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
Local $sockaddr, $aRet
$sockaddr = DllStructCreate("short;ushort;uint;char")
$aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
"ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
If Not @error And $aRet = 0 Then
$aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
If Not @error Then $aRet = $aRet
Else
$aRet = 0
EndIf
$sockaddr = 0
Return $aRet
EndFunc ;==>SocketToIP
发送端Opt('MustDeclareVars', 1)
;==============================================
;==============================================
;用户端! 启动服务器之后启动用户端!!!!!!!!!!!!!!!
;==============================================
;==============================================
Example()
Func Example()
; 设置一些常用信息
;--------------------------
Local $ConnectedSocket, $szData
; 设置 $szIPADDRESS 为本地服务器. 这里使用个人电脑名称转换为 IP 地址
; Local $szServerPC = @ComputerName
; Local $szIPADDRESS = TCPNameToIP($szServerPC)
Local $szIPADDRESS ="192.168.0.249"
Local $nPORT = 33891
; 开始 TCP 服务
;==============================================
TCPStartup()
; 初始化一个描述连接的变量
;==============================================
$ConnectedSocket = -1
;尝试连接到服务器IP的 33891 端口.
;=======================================================
$ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)
; 如果发生了错误... 则显示出来
If @error Then
MsgBox(4112, "错误", "TCPConnect 失败于 WSA 错误: " & @error)
; 如果这里没有错误,就循环一个 inputbox 函数,用于发送数据到服务器
Else
;不断循环,每次将询问发送什么数据给服务器
While 1
; 使用 InputBox 函数得到要发送的数据
$szData = InputBox("发送数据给服务器", @LF & @LF & "输入发送给服务器的数据:")
; 如果点击了 InputBox 函数显示输入框的取消按钮,或者使用一个空数据则退出这个循环
If @error Or $szData = "" Then ExitLoop
; 我们确保在 $szData 中有数据... 然后尝试通过连接发送数据.
$szData=StringToBinary($szData,4)
TCPSend($ConnectedSocket, $szData)
; 如果发送失败(@error)将断开连接
;----------------------------------------------------------------
If @error Then ExitLoop
WEnd
EndIf
EndFunc ;==>Example
本帖最后由 scorpio1102 于 2013-7-31 14:54 编辑
为什么我复制7楼的代码运行服务端后,客户端 程序运行有错.网络通的,防火墙关闭的.
____OK了原来是地址搞错了. 本帖最后由 scorpio1102 于 2013-7-31 14:54 编辑
回复 7# love5173
我照抄你的代码,运行后报错了,TCPConnect 失败于WSA错误:10061.网络都通的,防火墙都关了
------------好了,OK了.地址搞错了. OK了.地址搞错了. 客户端(发送)断开以后,服务端(接收)也会自动退出,我想如果让接收端始终监听,发送端时不时练上来发个消息给接收端,该怎么写 用Base64也可解决的这问题.
"UDFB64.au3"https://www.autoitscript.com/forum/topic/145526-base64-converter-files-and-strings-can-be-modified-easily/
Local $sStr = B64Encode('Hello Baby = 宝贝宝贝', False)
TCPSend($iSocket, $sStr)
Local $sMsgfrClient = TCPRecv($Socket, 2048)
Local $sDeCoded = B64Decode($sMsgfrClient, 1)
MsgBox(0, "", $sDeCoded)
虽然看不懂。但来学习学习! 学习学习学习学习
页:
[1]