找回密码
 加入
搜索
查看: 2785|回复: 4

[网络通信] TCP服务器怎么回复客户端信息

[复制链接]
发表于 2017-1-15 21:30:48 | 显示全部楼层 |阅读模式
本帖最后由 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[8]")

        $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $Shocket, "ptr", DllStructGetPtr($SockAddr), "int*", DllStructGetSize($SockAddr))
        If NOT @error AND $aRet[0] = 0 Then
                $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($SockAddr, 3))
                If NOT @error Then $aRet = $aRet[0]
        Else
                $aRet = 0
        EndIf

        Return $aRet
EndFunc  
 楼主| 发表于 2017-1-17 13:32:07 | 显示全部楼层
本帖最后由 zhuyl369 于 2017-1-17 13:37 编辑

难道没有人知道这个怎么弄吗?
发表于 2017-1-17 14:40:15 | 显示全部楼层
为什么一定要用Ws2_32.dll?每台机子的Ws2_32.dll版本问题怎么解决?
在帮助的“网络管理”里有例子。
发表于 2017-1-17 23:35:56 | 显示全部楼层
TCP是通过socket进行通讯的,帮助里面有示例,建议你使用之前在论坛先搜索一下。
 楼主| 发表于 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[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文件
[SERVER]
IP=192.168.1.100
PORT=9978
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-11-21 21:33 , Processed in 0.096631 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表