zhongfufan 发表于 2011-1-27 05:50:30

借抄下.wsycharles0o 的代码.提问下.

本帖最后由 zhongfufan 于 2011-1-27 06:44 编辑

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GuiIPAddress.au3>

HotKeySet("{F8}","QUIT")

Global $GUICLOSE=1,$NEXT="NONE",$WATING=0,$Message=0,$LOCALIP=@IPAddress1,$Connected=-1,$REMOTEIP=1


Func QUIT()
      TCPShutdown()
      Exit
EndFunc

Func LINK()
      Local $GUI=0,$G,$link
      $G=GUICreate("TCP Net Chatting",180,90)
      GUICtrlCreateLabel("请输入对方IP地址:",20,10,160,50)
      $InputIP = _GUICtrlIpAddress_Create ($G,20,30,140,20)
      $link=GUICtrlCreateButton("连接",50,60,80,20)
      GUISetState()
    While 1
                $GUI=GUIGetMsg()
            If $GUI=$GUI_EVENT_CLOSE Then QUIT()
                If $GUI=$link then
                        $REMOTEIP = _GUICtrlIpAddress_Get($InputIP)
                        ExitLoop
                EndIf
      WEnd
      $Connected=TCPConnect($REMOTEIP,5238)
      If Not ($Connected > 0) then
                MsgBox(0,"Error","连接失败!")
                quit()
      EndIf
      GUIDelete()
EndFunc

Func HOST()
      Local $GUI=0
      $WATING = TCPListen($LOCALIP,5238)
      If $WATING = 0 Or $WATING =-1 Then
                MsgBox(0,"error","error occur! @error Mark:"&@error)
                QUIT()
      EndIf
      GUICreate("TCP Net Chatting",180,70)
      GUICtrlCreateLabel("端口已打开,等待连接...",10,10,160,20)
      guictrlcreatelabel("本地IP:"&$LOCALIP,10,30,160,20)
      GUISetState()
      Do
                $Connected = TCPAccept($WATING)
                $GUI=GUIGetMsg()
            If $GUI=$GUI_EVENT_CLOSE Then QUIT()
      Until $Connected <> -1
    GUIDelete()
EndFunc


Func StartUp()
      Local $ping,$i=1,$host,$link,$lian="连接中.",$L,$Connection=0,$ESC,$Choose,$TCPSTART
      MsgBox(0,"TCP Net Chatting","       Welcome!",2)
      GUICreate("TCP Net Chatting",150,300)
      $link=GUICtrlCreateButton("主动连接",30,200,90,20)
      GUICtrlSetState($link,$GUI_DISABLE)
      $host=GUICtrlCreateButton("等待连接",30,240,90,20)
      GUICtrlSetState($host,$GUI_DISABLE)
      $L=GUICtrlCreateLabel($lian,45,100,90,20)
      GUISetState()
      For $i=0 To 3
                $ping[$i]=Ping("baidu.com",1000)
                If $ping[$i]=0 then
                        $ping[$i]=">1000"
                Else
                        $Connection=1
                EndIf
                $lian=$lian&"."
                GUICtrlSetData($L,$lian)
      Next
      $TCPSTART = TCPStartup()
      If $TCPSTART =0 Then
                GUICtrlCreateLabel("本地TCP服务启动失败!",20,100,120,20)
                MsgBox(0,"Error","@error:"&@error)
      Else
                GUICtrlSetState($host,$GUI_ENABLE)
                GUICtrlSetState($link,$GUI_ENABLE)
                GUIctrlDelete($L)
                GUICtrlCreateLabel("连接延迟(Ping):",20,30,90,20)
                GUICtrlCreateLabel($ping&" 毫秒",60,60,90,20)
                GUICtrlCreateLabel($ping&" 毫秒",60,80,90,20)
                GUICtrlCreateLabel($ping&" 毫秒",60,100,90,20)
                GUICtrlCreateLabel($ping&" 毫秒",60,120,90,20)
                GUICtrlCreateLabel("本地TCP服务启动成功!",20,150,120,20)
      EndIf
      
      If $Connection=0 Then
      GUICtrlCreateLabel("所有PING返回大于1000"&@CRLF&" 您可能没有接入网络",15,170,120,30)
      EndIf

      While $ESC=0
                $Choose=GUIGetMsg()
                If $Choose = $GUI_EVENT_CLOSE Then $ESC=1
                If $Choose = $link Then
                        $NEXT="LINK"
                        $ESC=1
                EndIf
                If $Choose = $host Then
                        $NEXT="HOST"
                        $ESC=1
                EndIf
      WEnd
GUIDelete()
EndFunc

Func GETIP()
      TrayTip("TCP Net Chatting", "TCP连接成功!正在获取对方IP地址...",0)
      While ($REMOTEIP = 1)
                $REMOTEIP=TCPRecv($Connected,20)
      WEnd
      TrayTip("TCP Net Chatting", "IP地址获取成功!",0)
EndFunc

Func SENDIP()
      TrayTip("TCP Net Chatting", "TCP连接成功!正在发送本地IP地址...",0)
    TCPSend($Connected,$LOCALIP)
      TrayTip("TCP Net Chatting", "本地IP地址发送成功!",0)
EndFunc

StartUp()

If $NEXT = "NONE" Then QUIT()
If $NEXT = "HOST" Then HOST()
If $NEXT = "LINK" Then LINK()

$GUIID=GUICreate("ICU--TCP Testing Project EDIT",400,200)
$WORDS=GUICtrlCreateEdit("TCP Online Chatting! Welcome!!!"&@CRLF,10,10,380,150,$Es_READONLY)
GUICtrlSetStyle($WORDS,$WS_VSCROLL)
$MSG=GUICtrlCreateInput("Write here!",10,170,330,20)
$SEND=GUICtrlCreateButton("SEND",340,170,50,20)
GUICtrlSetBkColor($WORDS,16777215)
GUISetState()

If $NEXT = "HOST" Then GETIP()
If $NEXT = "LINK" Then SENDIP()

GUICtrlSetData($WORDS,GUICtrlRead($WORDS)&"TCP连接已建立,对方IP:"&$REMOTEIP&@CRLF)


While $GUICLOSE
      $GUI=GUIGetMsg()
      If $GUI=$GUI_EVENT_CLOSE Then $GUICLOSE=0
      If $GUI=$SEND Then
                TCPSend($Connected,GUICtrlRead($MSG))
            GUICtrlSetData($WORDS,GUICtrlRead($WORDS)&"Me:"&GUICtrlRead($MSG)&@CRLF)
      EndIf
      $Message=TCPRecv($Connected,2048)
      If $Message <> "" Then
            GUICtrlSetData($WORDS,GUICtrlRead($WORDS)&"Someone:"&$Message&@CRLF)
                $Message = ""
      EndIf
WEnd
TCPShutdown()


我能否让他这个代码.连接指定IP(192.168.1.18).然后自动显示本机IP的后2位数.例如192.168.1.101就显示01,
还有个就是,这个代码似乎不支持中文多字的??中文打多几个字在本机有显示,在其他机器只显示1个中文....无奈啊..
有高人指点下吗.谢谢.

the886 发表于 2011-1-27 14:49:19

本帖最后由 the886 于 2011-1-27 14:52 编辑

1.返回字符串中从右开始指定数量的字符.
StringRight ( "字符串", 数量 )

2.需要将发送字符转为2进制,2进制接收,在把2进程转为字符

$sData = StringToBinary($SendFile, 4) ;字符转二进程

$recv = TCPRecv($ConnectedSocket, 1024 * 100, 1);二进程接收客户机传过来的数据

$recv = BinaryToString($recv, 4);二进制转字符,好像有时3会好一些

zhongfufan 发表于 2011-1-28 08:19:26

回复 2# the886


    研究下。谢谢。
页: [1]
查看完整版本: 借抄下.wsycharles0o 的代码.提问下.