#include <MISC.AU3>
#include <String.au3>
#include <Date.AU3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <array.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
Global $Form1 = GUICreate("Chat", 620, 435, -1, -1)
Global $Edit1 = GUICtrlCreateEdit("", 0, 0, 620, 400)
GUICtrlSetColor(-1, 0x0066CC)
GUICtrlSetData(-1, "")
GUICtrlSetStyle(-1,$ES_READONLY)
GUICtrlSetStyle(-1,$ES_AUTOVSCROLL)
GUICtrlSetStyle(-1,$ES_AUTOHSCROLL)
Global $Input1 = GUICtrlCreateInput("", 2, 405, 510, 25)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
Global $Button1 = GUICtrlCreateButton("Send", 520, 405, 90, 25)
GUICtrlSetState($Input1, $GUI_FOCUS) ; 控件得到输入焦点.
GUISetState(@SW_SHOW)
Global $Socket,$LSocket,$User
Global $SIP,$SPORT
Local $Conf=@ScriptDir&"\Conf.ini"
$SIP=IniRead($Conf,"SERVER","IP",0)
$SPORT=IniRead($Conf,"SERVER","PORT",0)
Global $ToIp=InputBox("Input Connect IP"," ","10.106.83.178","",50,120)
WinSetTitle ($Form1,"","与 "&$ToIp&" 对话中...")
TCPStartup()
OnAutoItExitRegister("OnAutoItExit")
Global $iListenSocket = TCPListen($SIP, $SPORT, 100)
If @error Then
$iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "不能接听, 错误代码: " & $iError)
Exit
EndIf
GUICtrlSetData($Edit1,"ListenIP: "&$SIP&" Port: "&$SPORT)
While 1
$nMsg = GUIGetMsg()
AcceptClient($iListenSocket)
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
SendMSG($ToIp)
EndSwitch
WEnd
TCPShutdown()
Func SendMSG($ToIp)
$LSocket=TCPConnect($ToIp,$SPORT)
$nMaxTimeout=@error
If @error = 10060 Then
$nMaxTimeout=@error
If $nMaxTimeout < 0 Then
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "无法连接, 之后 " & 10 - $nMaxTimeout & " 超时")
Return False
EndIf
ElseIf @error Then
$iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "无法连接, 错误代码: " & $iError)
Return False
Else
;MsgBox($MB_SYSTEMMODAL, "", "连接成功 ",2)
;GUICtrlSetData($Edit1,GUICtrlRead($Edit1)&@CRLF&"连接成功")
EndIf
$MSG=GUICtrlRead($Input1)
TCPSend($LSocket,_StringToHex($MSG))
If @error Then
$iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "客户端:" & @CRLF & "无法发送数据, 错误代码: " & $iError,2)
Return False
Else
GUICtrlSetData($Edit1,GUICtrlRead($Edit1)&@CRLF&"To: <"&$MSG&">")
EndIf
TCPCloseSocket($LSocket)
GUICtrlSetData($Input1,"")
GUICtrlSetState($Input1, $GUI_FOCUS)
EndFunc
Func AcceptClient($iListenSocket)
Local $iSocket = 0
$iSocket = TCPAccept($iListenSocket)
If @error Then
$iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "无法接收传入连接, 错误代码: " & $iError)
Return False
EndIf
if $iSocket <> -1 Then
Local $FoIp=SocketToIP($iSocket)
$ToIp=$FoIp
WinSetTitle ($Form1,"","与 "&$FoIp&" 对话中...")
Local $sReceived = _HexToString(TCPRecv($iSocket, 1024))
Local $Lsmsg=GUICtrlRead($Edit1)
Local $NewMsg=$Lsmsg&@CRLF&$FoIp&" "&_NowTime()&":"&$sReceived
GUICtrlSetData($Edit1,$NewMsg)
EndIf
TCPCloseSocket($iSocket)
EndFunc
Func SocketToIP($iSocket)
Local $tSockAddr = 0, $aRet = 0
$tSockAddr = DllStructCreate("short;ushort;uint;char[8]")
$aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $iSocket, "struct*", $tSockAddr, "int*", DllStructGetSize($tSockAddr))
If Not @error And $aRet[0] = 0 Then
$aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($tSockAddr, 3))
If Not @error Then Return $aRet[0]
EndIf
Return 0
EndFunc
Func OnAutoItExit()
TCPCloseSocket($iListenSocket)
TCPShutdown() ; 关闭 TCP 服务.
EndFunc
Conf.ini文件