or probably..
- From: Supra <supra@xxxxxxxxxxxxxx>
- Date: Wed, 25 May 2005 09:26:02 -0400
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
.
- References:
- Continues TCP recieve
- From: Jody L. Whitlock
- Continues TCP recieve
- Prev by Date: Re: VB-101: Passing Arrays ByVal vs ByRef
- Next by Date: Re: quick question
- Previous by thread: Continues TCP recieve
- Next by thread: How to open an Excel file and save a copy as CSV programatically?
- Index(es):
Relevant Pages
|
Loading