找回密码
 加入
搜索
查看: 4303|回复: 9

[网络通信] [已解决]IP地址改变后端口监听失败是什么原因,如何解决?

  [复制链接]
发表于 2011-11-12 20:12:27 | 显示全部楼层 |阅读模式
本帖最后由 hlzxcjx 于 2011-11-13 23:28 编辑
TCPStartup()
$ip = @IPAddress1
$Customerlsn = TCPListen($ip, 8843)
MsgBox(0, "$Customerlsn", $Customerlsn)
While 1
        $Indextext = TCPAccept($Customerlsn)
        $text = TCPRecv($Indextext, 2048)
        If $text <> "" Then
                ;;;;;;;;;;;;;
        EndIf
        Sleep(1000)
        If $ip <> @IPAddress1 Then
                TCPShutdown()
                Sleep(10)
                TCPStartup()
                $Customerlsn = TCPListen($ip, 8843)
                MsgBox(0, "$Customerlsn", $Customerlsn)
        EndIf
WEnd
当不断更改IP地址时,$Customerlsn=-1,监听失败,重启服务亦无效,如何解决?
发表于 2011-11-12 21:44:02 | 显示全部楼层
端口是不是被占用了,没有释放,换其他的端口试试!
发表于 2011-11-12 21:46:33 | 显示全部楼层
好像要用TCPcloseSocket()来释放端口
发表于 2011-11-13 11:05:56 | 显示全部楼层
试一试

TCPStartup()
$ip = "0.0.0.0"
$Customerlsn = TCPListen($ip, 8843)
MsgBox(0, "$Customerlsn", $Customerlsn)
While 1
        $Indextext = TCPAccept($Customerlsn)
        $text = TCPRecv($Indextext, 2048)
        If  @error Then
                TCPShutdown()
                TCPStartup()
                $Customerlsn = TCPListen($ip, 8843)
                MsgBox(0, "$Customerlsn", $Customerlsn)
        EndIf
        If $text <> "" Then
                ;;;;;;;;;;;;;
        EndIf


WEnd
 楼主| 发表于 2011-11-13 18:19:05 | 显示全部楼层
端口是不是被占用了,没有释放,换其他的端口试试!
xms77 发表于 2011-11-12 21:44



    我是想继续用原来的端口!
 楼主| 发表于 2011-11-13 18:19:31 | 显示全部楼层
好像要用TCPcloseSocket()来释放端口
xms77 发表于 2011-11-12 21:46



    试过无效!
 楼主| 发表于 2011-11-13 18:21:17 | 显示全部楼层
试一试

TCPStartup()
$ip = "0.0.0.0"
$Customerlsn = TCPListen($ip, 8843)
MsgBox(0, "$Customerls ...
yeqing880 发表于 2011-11-13 11:05


我是想当不断更改IP地址时,仍然用原来的端口监听成功!
发表于 2011-11-13 19:49:52 | 显示全部楼层
回复 6# hlzxcjx
你少了一句$ip = @IPAddress1的赋值语句,你一直在使用原来的IP当然会有问题了,以下代码测试成功!
TCPStartup()

$ip = @IPAddress1

$Customerlsn = TCPListen($ip, 8843)

MsgBox(0, "$Customerlsn", $Customerlsn)

While 1

        $Indextext = TCPAccept($Customerlsn)

        $text = TCPRecv($Indextext, 2048)

        If $text <> "" Then

                ;;;;;;;;;;;;;

        EndIf

        Sleep(1000)

        If $ip <> @IPAddress1 Then
                                $ip = @IPAddress1
                TCPCloseSocket($Customerlsn)

                Sleep(10)
                
                $Customerlsn = TCPListen($ip, 8843)

                MsgBox(0, "$Customerlsn", $Customerlsn)

        EndIf

WEnd
 楼主| 发表于 2011-11-13 23:26:57 | 显示全部楼层
回复 8# xms77


    一语惊醒梦中人!
发表于 2011-11-14 11:32:04 | 显示全部楼层
回复 9# hlzxcjx
不要学习太晚,记得要结贴啊!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-10-1 09:36 , Processed in 0.130337 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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