|
Subject:
|
Problem with TCPListener...please help.
|
|
Posted By:
|
e_sham
|
Post Date:
|
12/29/2003 9:26:39 PM
|
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
|
|
Reply By:
|
AshBex
|
Reply Date:
|
1/2/2004 11:55:24 AM
|
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
|
|
Reply By:
|
AshBex
|
Reply Date:
|
1/2/2004 12:00:48 PM
|
Sorry, I misunderstood your query... Forget the above API code, that's for detecting whether or not the PC is connected to internet..!
Ash
|
|