Click here to Skip to main content
15,891,513 members

TCPClient hangs on unresponsive IP

Rixterz123 asked:

Open original thread
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:

VB
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
Tags: Visual Basic, TCP/IP

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900