TCPClient挂起无响应的IP [英] TCPClient hangs on unresponsive IP

查看:132
本文介绍了TCPClient挂起无响应的IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我设置了一个程序,将DISCONNECTCAMERA的消息发送到某个IP地址。

当它收到消息后,它会断开网络摄像头的连接。

(我正在制作一个类似skype的程序,而我朋友的'ip'是在My.settings中)



问题是当我连接到没有响应的IP地址时,我必须等待超时才能达到超时。

你可以更改超时所以你不必等待发现它没有反应?



我试过My.Computer.Network.Ping(ipaddress,超时)但是没有工作



我的代码:



Hello everyone

I set up a program to send a message of "DISCONNECTCAMERA" to a certain IP address.
When it receives the message, it disconnects its webcam.
(i''m making a program like skype, and my friend''s ip''s are in My.settings)

The problem is that when i connect to an ip address that is unresponsive, i have to wait ages for the timeout to be reached.
Can you change the timeout so you dont have to wait to find out that it''s unresponsive?

I tried My.Computer.Network.Ping(ipaddress, timeout) but that didn''t work

My code:

Imports System.Net.Sockets
Imports System.IO
Public Class DisconnectAllCameras
    Dim Receiver As New TcpListener(65535)
    Dim Snder As New TcpClient
    Private Sub DisconnectAllCameras_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Receiver.Stop()
        Snder.Close()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        My.Computer.Network.Ping(My.Settings.Camera1IP)
        Receiver.Start()
        SendMessage(My.Settings.IP, "DISCONNECTCAMERA")
       End If
    End Sub
    Function SendMessage(ByVal IP As String, ByVal Message As String)
        Snder = New TcpClient(IP, 65535)
        Dim Writer As New StreamWriter(Snder.GetStream())
        Writer.Write(Message)
        Writer.Flush()
        If My.Settings.Camera1IP = IP Then
            Label1.Text = "Camera 1 : Offline"
            PictureBox1.Image = My.Resources.tick
        End If
        If My.Settings.Camera2IP = IP Then
            Label2.Text = "Camera 2 : Offline"
            PictureBox2.Image = My.Resources.tick
        End If
        If My.Settings.Camera3IP = IP Then
            Label3.Text = "Camera 3 : Offline"
            PictureBox3.Image = My.Resources.tick
        End If
        If My.Settings.Camera4IP = IP Then
            Label4.Text = "Camera 4 : Offline"
            PictureBox4.Image = My.Resources.tick
        End If
    End Function
    Private Sub MessageReceiver_Tick() Handles MessageReceiver.Tick
        If Receiver.Pending = True Then
            Snder = Receiver.AcceptTcpClient()
            Dim Reader As New StreamReader(Snder.GetStream())
            Dim Message As String = ""
            While Reader.Peek > -1
                Message &= Convert.ToChar(Reader.Read()).ToString
            End While
            MsgBox(Message)
        End If
    End Sub
End Class

推荐答案

当然,客户端无法修改服务器 - 边超时。你不觉得如果有些服务允许这样的事情,那就不好了吗?你对它的安全性有什么看法?但您可以在客户端断开连接或停止连接。当然,让所有客户沟通都是一个单独的线程是件好事。



-SA
Of course, the client cannot modify the server-side timeout. Don''t you think that if some service allowed such things, it would be no good? What would you say about its security than. But you can disconnect or stop connecting on client side. Of course, it''s good to have all the client communication is a separate thread.

—SA


这篇关于TCPClient挂起无响应的IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆