Results 1 to 3 of 3

Thread: System.Net.Sockets

  1. #1

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

    System.Net.Sockets

    How can i use the events (Connect, disconnect, read data, error and etc...) using the socket object, or the tcpclient.

    Anybody have any tips to help me?

    Thank you,
    Guilherme Costa

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    VB Code:
    1. Imports System
    2. Imports System.Net
    3. Imports System.Net.Sockets
    4. Imports System.Text
    5.  
    6. Public Class StateObject
    7.     Public workSocket As Socket = Nothing
    8.     Public BufferSize As Integer = 32767
    9.     Public buffer(32767) As Byte
    10.     Public sb As New StringBuilder()
    11. End Class
    12.  
    13. Public Class SocketsClient
    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 Sub Connect(ByVal RemoteHostName As String, ByVal RemotePort As Integer)
    27.         Try
    28.             client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    29.             port = RemotePort
    30.             ipHostInfo = Dns.Resolve(RemoteHostName)
    31.             ipAddress = ipHostInfo.AddressList(0)
    32.             Dim remoteEP As New IPEndPoint(ipAddress, port)
    33.             client.BeginConnect(remoteEP, AddressOf sockConnected, client)
    34.         Catch
    35.             RaiseEvent onError(Err.Description)
    36.             Exit Sub
    37.         End Try
    38.     End Sub
    39.  
    40.     Public Sub SendData(ByVal Data() As Byte)
    41.         Try
    42.             Dim byteData As Byte() = Data
    43.             client.BeginSend(byteData, 0, byteData.Length, 0, AddressOf sockSendEnd, client)
    44.             'client.Send(byteData, 0, byteData.Length, 0)
    45.         Catch
    46.             RaiseEvent onError(Err.Description)
    47.             Exit Sub
    48.         End Try
    49.     End Sub
    50.  
    51.     Public Sub Disconnect()
    52.         Try
    53.             client.Shutdown(SocketShutdown.Both)
    54.         Catch
    55.         End Try
    56.         client.Close()
    57.     End Sub
    58.  
    59.     Public Function StringToBytes(ByVal Data As String) As Byte()
    60.         StringToBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
    61.     End Function
    62.  
    63.     Public Function BytestoString(ByVal Data As Byte()) As String
    64.         BytestoString = System.Text.ASCIIEncoding.ASCII.GetString(Data)
    65.     End Function
    66.  
    67.     Private Sub sockConnected(ByVal ar As IAsyncResult)
    68.         Try
    69.             If client.Connected = False Then RaiseEvent onError("Connection refused.") : Exit Sub
    70.             Dim state As New StateObject()
    71.             state.workSocket = client
    72.             client.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf sockDataArrival, state)
    73.             RaiseEvent onConnect()
    74.         Catch
    75.             RaiseEvent onError(Err.Description)
    76.             Exit Sub
    77.         End Try
    78.     End Sub
    79.  
    80.     Private Sub sockDataArrival(ByVal ar As IAsyncResult)
    81.         Dim state As StateObject = CType(ar.AsyncState, StateObject)
    82.         Dim client As Socket = state.workSocket
    83.         Dim bytesRead As Integer
    84.  
    85.         Try
    86.             bytesRead = client.EndReceive(ar)
    87.         Catch
    88.             Exit Sub
    89.         End Try
    90.  
    91.         Try
    92.             Dim Data() As Byte = state.buffer
    93.             If bytesRead = 0 Then
    94.                 client.Shutdown(SocketShutdown.Both)
    95.                 client.Close()
    96.                 RaiseEvent onDisconnect()
    97.                 Exit Sub
    98.             End If
    99.             ReDim state.buffer(32767)
    100.  
    101.             client.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf sockDataArrival, state)
    102.             RaiseEvent onDataArrival(Data, bytesRead)
    103.         Catch
    104.             RaiseEvent onError(Err.Description)
    105.             Exit Sub
    106.         End Try
    107.     End Sub
    108.  
    109.     Private Sub sockSendEnd(ByVal ar As IAsyncResult)
    110.         Try
    111.             Dim client As Socket = CType(ar.AsyncState, Socket)
    112.             Dim bytesSent As Integer = client.EndSend(ar)
    113.             RaiseEvent onSendComplete(bytesSent)
    114.         Catch
    115.             RaiseEvent onError(Err.Description)
    116.             Exit Sub
    117.         End Try
    118.     End Sub
    119.  
    120.     Public Function Connected() As Boolean
    121.         Try
    122.             Return client.Connected
    123.         Catch
    124.             RaiseEvent onError(Err.Description)
    125.             Exit Function
    126.         End Try
    127.     End Function
    128. End Class
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    example use

    VB Code:
    1. Private WithEvents wsClient As New SocketsClient()
    2.     Dim bConnected As Boolean
    3.     Public UniqueID As String = "halberd"
    4.     Public IP As String = "127.0.0.1"
    5.     Public Port As Integer = 56667
    6.     'Dim lastCommand As String
    7.  
    8.     Private Sub Connection()
    9.         wsClient.Connect(IP, Port)
    10.         Do Until bConnected
    11.             'wait till connection is succesfull
    12.         Loop
    13.     End Sub
    14.  
    15.     Public Sub GetVersion()
    16.         ' get version of ABC connected to
    17.         Connection()
    18.         Dim sQuery As String = "ID|" & UniqueID & vbLf & "VERSION|"
    19.         wsClient.SendData(wsClient.StringToBytes(sQuery))
    20.     End Sub
    21.  
    22.     Private Sub onDataArrival(ByVal Data As Byte(), ByVal TotalBytes As Integer) Handles wsClient.onDataArrival
    23.         MessageBox.Show(wsClient.BytestoString(Data))
    24.     End Sub
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

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