autoit单一socket

用来修改时间.虽然花了两天写得破绽百出,不过由此开始接触了一个好工具.

这缩进,,这代码高亮..........-_-!,自己凑合吧

server:


#include    <GUIConstants.au3>
#include    <Date.au3>

#Region    ###    START    Koda    GUI    section    ###    Form=
$Form1    =    GUICreate("Form1",    362,    221,    193,    115)
$Input1    =    GUICtrlCreateInput("Input1",    1,    1,    289,    21)
GUISetState(@SW_SHOW)
#EndRegion    ###    END    Koda    GUI    section    ###

$l_ip=    "1.1.1.1"
$Asocket=-1

TCPStartup()
$Msocket=TCPListen($l_ip,12345)
If    $Msocket    =-1    Then    Exit        

Func    sendme()
        TCPSend($Asocket,@YEAR    &    "/"    &    @MON    &    "/"    &    @MDAY    &    "-"    &    @HOUR    &    ":"    &    @MIN)
EndFunc

While    1
        $nMsg    =    GUIGetMsg()
        Switch    $nMsg
                Case    $GUI_EVENT_CLOSE
                        TCPCloseSocket(    $Asocket    )
                        TCPShutDown()
                        Exit
        EndSwitch
        If    $Asocket    =    -1    Then    
                $Asocket    =    TCPAccept($Msocket)
                If    $Asocket    <    0    Then
                                                                $Asocket    =    -1
                Else
                                                                WinSetTitle($Form1,"","Connected")
                                                                sendme()
                EndIf
        EndIf
        $recvtemp=TCPRecv($Asocket,2048)
        If    $recvtemp    <>    ""    Then    
                        $timeretemp=StringSplit($recvtemp,"    ")
                        _SetTime($timeretemp[2],$timeretemp[3])
                        $dateretemp=StringSplit($timeretemp[1],"/")
                        _SetDate($dateretemp[3],$dateretemp[2])
                        GUICtrlSetData($Input1,$recvtemp)
                        Sendme()
                ElseIf    @error    Then
                    WinSetTitle($Form1,"","Client    Disconnected")
                                                                                                                                    TCPCloseSocket($Asocket)
                                                                                                                                    $Asocket    =    -1
        endif    

WEnd



client:



#include    <GUIConstants.au3>
Opt("TrayMenuMode",1)
#Region    ###    START    Koda    GUI    section    ###    Form=
$Form1    =    GUICreate("Form1",    605,    233,    193,    115)
$MonthCal1    =    GUICtrlCreateMonthCal("2008/08/12",    8,    8,    275,    161)
$Button1    =    GUICtrlCreateButton("submit",    252,    192,    89,    25,    0)
$Button2    =    GUICtrlCreateButton("let's    connect",    100,    192,    89,    25,    0)
$Label1    =    GUICtrlCreateLabel("",    404,    8,    292,    33)
$Label2    =    GUICtrlCreateLabel("2008/08/12    15:58",    304,    112,    284,    17)
$Label3    =    GUICtrlCreateLabel("server    time:",    304,    8,    76,    25)
$Input1    =    GUICtrlCreateInput("15:58",    304,    144,    289,    21)

GUISetState(@SW_SHOW)
#EndRegion    ###    END    Koda    GUI    section    ###

$g_IP    =    "1.1.1.1"
$monthvtemp=GUICtrlRead($MonthCal1)
$socket=-1
TCPStartup()
Func    trycon()
$socket=TCPConnect($g_IP,12345)
If    $socket    >    2    Then
        WinSetTitle($Form1,"","client    connected")
Else
        WinSetTitle($Form1,"","client    fail")
        $socket=-1
EndIf
EndFunc

trycon()

While    1
        If    $socket    >2    Then
                GUICtrlSetData($Button2,"connected")
                GUICtrlSetState($Button2,$GUI_HIDE)
        ElseIf    $socket    <2    and    GUICtrlRead    ($Button2)    ==    "connected"    Then
                GUICtrlSetData($Button2,"let's    connect")
                GUICtrlSetState($Button2,$GUI_SHOW)
        EndIf
        $nMsg    =    GUIGetMsg()
        Switch    $nMsg
                Case    $GUI_EVENT_CLOSE
                        TCPCloseSocket($socket)
                        TCPShutDown()
                        Exit
                Case    $MonthCal1
                        $monthvtemp=GUICtrlRead($MonthCal1)
                case    $Button2
                        trycon()
                Case    $Button1
                        GUICtrlSetData($Label2,$monthvtemp    &    "    "    &    GUICtrlRead($Input1))
                        $timetemp=StringSplit(GUICtrlRead($Input1),":")
                        if    $timetemp[0]    <>    2    or    $timetemp[1]    >    23    or    $timetemp[2]    >59        then    
                                MsgBox(0,"error!","time    format    error")
                        Else
                                $cccc=TCPSend($socket,GUICtrlRead($MonthCal1)    &    "    "    &        $timetemp[1]    &    "    "        &    $timetemp[2])
                                if    $cccc    ==    0    Then    
                                        WinSetTitle($Form1,"","client    disconnect!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
                                        $socket=-1
                                EndIf
                        EndIf
        EndSwitch

        $recv=TCPRecv($socket,2048)
        If    $recv    <>    ""    then    
                GUICtrlSetData($Label1,$recv)
        EndIf
WEnd




点击在新窗口中浏览此图片