Results 1 to 3 of 3

Thread: Socket disconnect event

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Location
    Porto Alegre, RS
    Posts
    210

    Socket disconnect event

    I need to know when the remote host disconects me, i hav to make a thread that ask for connected state??

    I'm using the follow class:

    VB Code:
    1. Imports System
    2. Imports System.Net
    3. Imports System.Net.Sockets
    4. Imports System.Text
    5.  
    6. Public Class MSSocket
    7.     Class StateObject
    8.         Public workSocket As Socket = Nothing
    9.         Public BufferSize As Integer = 32767
    10.         Public buffer(32767) As Byte
    11.         Public sb As New StringBuilder
    12.     End Class
    13.  
    14.     Public Event onConnect()
    15.     Public Event onError(ByVal Description As String)
    16.     Public Event onDataArrival(ByVal Data As Byte(), ByVal TotalBytes As Integer)
    17.     Public Event onDisconnect()
    18.     Public Event onSendComplete(ByVal DataSize As Integer)
    19.  
    20.     Private Shared response As [String] = [String].Empty
    21.     Private Shared port As Integer = 44
    22.     Private Shared ipHostInfo As IPHostEntry = Dns.Resolve("localhost")
    23.     Private Shared ipAddress As ipAddress = ipHostInfo.AddressList(0)
    24.     Private Shared client As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    25.  
    26.     Public Property configIP()
    27.         Get
    28.             Return ipAddress
    29.         End Get
    30.         Set(ByVal Value)
    31.             ipAddress = Value
    32.         End Set
    33.     End Property
    34.  
    35.     Public Sub Connect(ByVal RemoteHostName As String, ByVal RemotePort As Integer)
    36.         Try
    37.             client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    38.             port = RemotePort
    39.             ipHostInfo = Dns.Resolve(RemoteHostName)
    40.             ipAddress = ipHostInfo.AddressList(0)
    41.             Dim remoteEP As New IPEndPoint(ipAddress, port)
    42.             client.BeginConnect(remoteEP, AddressOf sockConnected, client)
    43.         Catch
    44.             RaiseEvent onError(Err.Description)
    45.             Exit Sub
    46.         End Try
    47.     End Sub
    48.  
    49.     Public Sub SendData(ByVal Data() As Byte)
    50.         Try
    51.             Dim byteData As Byte() = Data
    52.             client.BeginSend(byteData, 0, byteData.Length, 0, AddressOf sockSendEnd, client)
    53.             'client.Send(byteData, 0, byteData.Length, 0)
    54.         Catch
    55.             RaiseEvent onError(Err.Description)
    56.             Exit Sub
    57.         End Try
    58.     End Sub
    59.  
    60.     Public Sub Disconnect()
    61.         Try
    62.             client.Shutdown(SocketShutdown.Both)
    63.         Catch
    64.         End Try
    65.         client.Close()
    66.     End Sub
    67.  
    68.     Public Function StringToBytes(ByVal Data As String) As Byte()
    69.         StringToBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
    70.     End Function
    71.  
    72.     Public Function BytestoString(ByVal Data As Byte()) As String
    73.         BytestoString = System.Text.ASCIIEncoding.ASCII.GetString(Data)
    74.     End Function
    75.  
    76.     Private Sub sockConnected(ByVal ar As IAsyncResult)
    77.         Try
    78.             If client.Connected = False Then RaiseEvent onError("Connection refused.") : Exit Sub
    79.             Dim state As New StateObject
    80.             state.workSocket = client
    81.             client.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf sockDataArrival, state)
    82.             RaiseEvent onConnect()
    83.         Catch
    84.             RaiseEvent onError(Err.Description)
    85.             Exit Sub
    86.         End Try
    87.     End Sub
    88.  
    89.     Private Sub sockDataArrival(ByVal ar As IAsyncResult)
    90.         Dim state As StateObject = CType(ar.AsyncState, StateObject)
    91.         Dim client As Socket = state.workSocket
    92.         Dim bytesRead As Integer
    93.  
    94.         Try
    95.             bytesRead = client.EndReceive(ar)
    96.         Catch
    97.             Exit Sub
    98.         End Try
    99.  
    100.         Try
    101.             Dim Data() As Byte = state.buffer
    102.             If bytesRead = 0 Then
    103.                 client.Shutdown(SocketShutdown.Both)
    104.                 client.Close()
    105.                 RaiseEvent onDisconnect()
    106.                 Exit Sub
    107.             End If
    108.             ReDim state.buffer(32767)
    109.  
    110.             client.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf sockDataArrival, state)
    111.             RaiseEvent onDataArrival(Data, bytesRead)
    112.         Catch
    113.             RaiseEvent onError(Err.Description)
    114.             Exit Sub
    115.         End Try
    116.     End Sub
    117.  
    118.     Private Sub sockSendEnd(ByVal ar As IAsyncResult)
    119.         Try
    120.             Dim client As Socket = CType(ar.AsyncState, Socket)
    121.             Dim bytesSent As Integer = client.EndSend(ar)
    122.             RaiseEvent onSendComplete(bytesSent)
    123.         Catch
    124.             RaiseEvent onError(Err.Description)
    125.             Exit Sub
    126.         End Try
    127.     End Sub
    128.  
    129.     Public Function Connected() As Boolean
    130.         Try
    131.             Return client.Connected
    132.         Catch
    133.             RaiseEvent onError(Err.Description)
    134.             Exit Function
    135.         End Try
    136.     End Function
    137. End Class

    Thank you,
    Guilherme Costa

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    From what I've experienced, when a socket disconnects, you recieve a zero byte message.

  3. #3
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    Originally posted by Mike Hildner
    From what I've experienced, when a socket disconnects, you recieve a zero byte message.
    I will have to confirm this, when a remote client/server disconnects, the receive call will be called with a zero byte packet, so...

    Code:
                If bytesRead = 0 Then
                    client.Shutdown(SocketShutdown.Both)
                    client.Close()
                    RaiseEvent onDisconnect()
                    Exit Sub
                End If
    means the remote connection disconnected

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width