zhuyl369 发表于 2017-1-15 21:30:48

TCP服务器怎么回复客户端信息

本帖最后由 zhuyl369 于 2017-1-17 13:30 编辑

TCP服务器怎么接收到信息后回复客户端信息?客户端怎么接受?
求指导。。。

找到一个函数可以从socket获取到客户端IP地址
; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
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

      Return $aRet
EndFunc

zhuyl369 发表于 2017-1-17 13:32:07

本帖最后由 zhuyl369 于 2017-1-17 13:37 编辑

难道没有人知道这个怎么弄吗?{:face (229):}

ila 发表于 2017-1-17 14:40:15

为什么一定要用Ws2_32.dll?每台机子的Ws2_32.dll版本问题怎么解决?
在帮助的“网络管理”里有例子。

jsdn2000 发表于 2017-1-17 23:35:56

TCP是通过socket进行通讯的,帮助里面有示例,建议你使用之前在论坛先搜索一下。

zhuyl369 发表于 2017-1-20 13:54:48

本帖最后由 zhuyl369 于 2017-1-20 13:57 编辑

自己写的一个局域网聊天程序,请高手指点错误的地方#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")
    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $iSocket, "struct*", $tSockAddr, "int*", DllStructGetSize($tSockAddr))
    If Not @error And $aRet = 0 Then
      $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($tSockAddr, 3))
      If Not @error Then Return $aRet
    EndIf
    Return 0
EndFunc

Func OnAutoItExit()
        TCPCloseSocket($iListenSocket)
    TCPShutdown() ; 关闭 TCP 服务.
EndFuncConf.ini文件

IP=192.168.1.100
PORT=9978
页: [1]
查看完整版本: TCP服务器怎么回复客户端信息