or probably..



u may have to used addhandled keyword

Jody L. Whitlock wrote:

I'm kinda stumped at the moment, so...  Here's what I'm looking to do:
I have a ClientSocket, and I would like to free-thread the recieve end
of it.  I tried a delegate, but that won't fire.  I'm thinking of using
a networkstream, but can't really find anything on that.  The purpose
behind this is becuase my current solution is dropping bytes.  I will
post my class here.
________________________________________________________________________
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading

Public Class NetClient
   Public Event Connected()
   Public Event Disconnected()
   Public Event DataRecieved(ByVal sender As Object, ByVal DataIn As
String)
   Private Delegate Sub TwoArg(ByVal Arg1() As Byte, ByVal Arg2 As
Integer)
   Private ClientSocket As Socket
   Private Enc As New ASCIIEncoding
   Private objLockA As Object
   '''
------------------------------------------------------------------------
-----
   ''' <summary>
   ''' Initializes the server connection
   ''' </summary>
   ''' <param name="ServerName">Hostname of the server</param>
   ''' <param name="ServerPort">Port number of the server</param>
   ''' <remarks>
   ''' </remarks>
   ''' <history>
   ''' 	[JLWHITL]	5/23/2005	Created
   ''' </history>
   '''
------------------------------------------------------------------------
-----
   Public Sub Connect(ByVal ServerName As String, ByVal ServerPort As
Integer)
       '-- Resolve the name to an IP Address
       Dim Addr As IPAddress = Dns.Resolve(ServerName).AddressList(0)
       If Not Addr Is Nothing Then
           '-- Create a new IP Endpoint
           Dim ep As New IPEndPoint(Addr, ServerPort)
           '--Create new Socket
           Me.ClientSocket = New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
           '-- Connect Async
           ClientSocket.BeginConnect(ep, AddressOf ConnectCallback,
Nothing)
       End If
   End Sub
   '''
------------------------------------------------------------------------
-----
   ''' <summary>
   ''' Closes the server connection
   ''' </summary>
   ''' <remarks>
   ''' </remarks>
   ''' <history>
   ''' 	[JLWHITL]	5/23/2005	Created
   ''' </history>
   '''
------------------------------------------------------------------------
-----
   Public Sub Disconnect()
       ClientSocket.Shutdown(SocketShutdown.Both)
       ClientSocket.Close()
       RaiseEvent Disconnected()
   End Sub
   Private Sub ConnectCallback(ByVal ar As IAsyncResult)
       ClientSocket.EndConnect(ar)
       RaiseEvent Connected()
       '-- Begin recieving data
       Dim buff(4096) As Byte
       ClientSocket.BeginReceive(buff, 0, buff.Length,
SocketFlags.None, AddressOf RecieveCallBack, buff)
   End Sub
   Public Sub DataProcess(ByVal DataSlot() As Byte, ByVal BytesRecvd
As Integer)
       SyncLock Me.objLockA
           If BytesRecvd = 0 Then '-- Server has closed connection
               ClientSocket.Shutdown(SocketShutdown.Both)
               ClientSocket.Close()
           Else '-- We have data
               Dim Recv As String = Enc.GetString(DataSlot)
               '-- Clear the buffer
               Array.Clear(DataSlot, 0, DataSlot.Length)
               '-- Begin recieve again
               RaiseEvent DataRecieved(Me, Recv)
               'ClientSocket.BeginReceive(DataSlot, 0,
DataSlot.Length, SocketFlags.None, AddressOf RecieveCallBack, buff)
           End If
       End SyncLock
   End Sub
   Private Sub RecieveCallBack(ByVal ar As IAsyncResult)
       Try
           Dim buff() As Byte = CType(ar.AsyncState, Byte())
           Dim numBytes As Integer = ClientSocket.EndReceive(ar)
           'Dim buff(2048) As Byte
           ' Dim dlg As New TwoArg(AddressOf DataProcess)
           'dlg.Invoke(buff, numBytes)
           If numBytes = 0 Then '-- Server has closed connection
               ClientSocket.Shutdown(SocketShutdown.Both)
               ClientSocket.Close()
           Else '-- We have data
               Dim Recv As String = Enc.GetString(buff)
               '-- Clear the buffer
               Array.Clear(buff, 0, buff.Length)
               '-- Begin recieve again
               RaiseEvent DataRecieved(Me, Recv)
               ClientSocket.BeginReceive(buff, 0, buff.Length,
SocketFlags.None, AddressOf RecieveCallBack, buff)
           End If
       Catch ex As Exception
           'Throw New Exception(ex.Message, ex.InnerException)
       End Try
   End Sub
   '''
------------------------------------------------------------------------
-----
   ''' <summary>
   ''' Sends data to the server
   ''' </summary>
   ''' <param name="DataOut">String to send</param>
   ''' <remarks>
   ''' </remarks>
   ''' <history>
   ''' 	[JLWHITL]	5/23/2005	Created
   ''' </history>
   '''
------------------------------------------------------------------------
-----
   Public Sub Send(ByVal DataOut As String)
       Dim buff() As Byte = Enc.ASCII.GetBytes(DataOut)
       ClientSocket.Send(buff)
   End Sub
End Class
________________________________________________________________________

Thank you,
Jody


.



Relevant Pages

  • .Net remoting doesnt work in Wan area.
    ... i try to broadcast events raised in server to ... Public Sub DoSomething ... Dim del As ... Dim clientProv As New BinaryClientFormatterSinkProvider ...
    (microsoft.public.dotnet.framework)
  • Re: How to Test Database Connectivity From a Workstation?
    ... set to an instance of an object" and "sql server does not exist". ... Private Sub PrintSqlErrors(ByVal errors As SqlErrorCollection, ByVal what As String) ... Dim e As SqlError ...
    (comp.databases.ms-sqlserver)
  • Multi-threaded app memory leak
    ... Dim RunningThreads As Integer = 0 ... Private Sub RunProcess() ... ' Attains server list from source .... ... Public PredictiveDiskFail As Boolean = False ...
    (microsoft.public.dotnet.languages.vb)
  • Re: .NET vb db access for crystal report
    ... Below is a sub I created to automate uid, pwd, server name, etc, mostly so ... Dim crtablelogoninfos As New TableLogOnInfos ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: CDO.Person - Invalid Class String
    ... CDO 1.21 can run both on the client and server. ... > Dim sDestURL As String ... > Set iDsrc = OrigMsg ... > End Sub ...
    (microsoft.public.dotnet.framework.interop)

Loading