Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > Pro VB.NET 2002/2003
|
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 29th, 2003, 10:26 PM
Registered User
 
Join Date: Dec 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to e_sham Send a message via Yahoo to e_sham
Default Problem with TCPListener...please help.

Hi everybody.. I'm new in this posting. Hope y'all can help me in my programing problem. I also hope that I can share what ever i know in programing to help y'all too. Ok.. now here come the problem.. I'm doing a programming in VB.NET, a client-server application. I'm using sample code that is in MSDN library. My code uses the UserConnection class. The problem is..when my application act as a server, it cannot detect the client disconnect. Actually it detected something.. it receive a null string when the client disconnect, but I don't know how I can close the client connection in the server so that my server can listen to other connection.. I also use threading in my application.. In here i attached the code that I'm using. Hope to get respond from y'all..

************************************************** *******************

Option Strict On

Imports System.Net.Sockets
Imports System.Text

' The UserConnection class encapsulates the functionality of a TcpClient connection
' with streaming for a single user.
Public Class UserConnection
    Const READ_BUFFER_SIZE As Integer = 511

    ' Overload the New operator to set up a read thread.
    Public Sub New(ByVal client As TcpClient)
        Me.client = client

        ' This starts the asynchronous read thread. The data will be saved into
        ' readBuffer.
        Me.client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
    End Sub

    Private client As TcpClient
    Private readBuffer(READ_BUFFER_SIZE) As Byte
    Private strName As String

    ' The Name property uniquely identifies the user connection.
    Public Property Name() As String
        Get
            Return strName
        End Get
        Set(ByVal Value As String)
            strName = Value
        End Set
    End Property


    Public Event LineReceived(ByVal sender As UserConnection, ByVal Data As String)

    ' This subroutine uses a StreamWriter to send a message to the user.
    Public Sub SendData(ByVal Data As String)
        ' Synclock ensure that no other threads try to use the stream at the same time.
        SyncLock client.GetStream
            Dim writer As New IO.StreamWriter(client.GetStream)
            writer.Write(Data & Chr(13) & Chr(10))

            ' Make sure all data is sent now.
            writer.Flush()
        End SyncLock
    End Sub

    ' This is the callback function for TcpClient.GetStream.Begin. It begins an
    ' asynchronous read from a stream.
    Private Sub StreamReceiver(ByVal ar As IAsyncResult)
        Dim BytesRead As Integer
        Dim strMessage As String

        Try
            ' Ensure that no other threads try to use the stream at the same time.
            SyncLock client.GetStream
                ' Finish asynchronous read into readBuffer and get number of bytes read.
                BytesRead = client.GetStream.EndRead(ar)
            End SyncLock

            ' Convert the byte array the message was saved into, minus one for the
            ' Chr(13).
            strMessage = Encoding.ASCII.GetString(readBuffer, 0, BytesRead)
            RaiseEvent LineReceived(Me, strMessage)

            ' Ensure that no other threads try to use the stream at the same time.
            SyncLock client.GetStream
                ' Start a new asynchronous read into readBuffer.
                client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
            End SyncLock
        Catch e As Exception
            'MsgBox(e.Message)
        End Try
    End Sub
End Class

 
Old January 2nd, 2004, 12:55 PM
Registered User
 
Join Date: Dec 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try usin this API function (InternetGetConnectedState)
It retrieves the connected state of the local system.

Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal dwReserved As Long) As Long


Parameter Information

· lpdwFlags
[out] Pointer to an unsigned long integer variable where the connection description should be returned. This can be a combination of the following values:
INTERNET_CONNECTION_CONFIGURED
Local system has a valid connection to the Internet, but it may or may not be currently connected.
INTERNET_CONNECTION_LAN
Local system uses a local area network to connect to the Internet.
INTERNET_CONNECTION_MODEM
Local system uses a modem to connect to the Internet.
INTERNET_CONNECTION_MODEM_BUSY
No longer used.
INTERNET_CONNECTION_OFFLINE
Local system is in offline mode.
INTERNET_CONNECTION_PROXY
Local system uses a proxy server to connect to the Internet.
INTERNET_RAS_INSTALLED
Local system has RAS installed.

· dwReserved
[in] Reserved. Must be set to zero.


Return Values

Returns TRUE if there is an Internet connection, or FALSE otherwise.


Examples

- GetConnectedState
- IsConnected




Ash
 
Old January 2nd, 2004, 01:00 PM
Registered User
 
Join Date: Dec 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, I misunderstood your query...
Forget the above API code, that's for detecting whether or not the PC is connected to internet..!



Ash





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create TCPListener windows service usingC# baqer C# 0 September 24th, 2003 08:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.