Continues TCP recieve

J

Jody L. Whitlock

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
 
S

Supra

following my example i put another class1.vb

Delegate Sub OneArgSub(ByVal rtb As RichTextBox, ByVal szText As
String, ByVal szColour As String, ByVal szNum As Integer)
Private deleg As OneArgSub

Private Sub _Connection_onSeverMessage(ByVal szText As String)
Handles _Connection.onSeverMessage
deleg = New OneArgSub(AddressOf DisplayMessage)
deleg.Invoke(nStatus.rtbStatus, szText.ToString,
GetUserColours.colourOther, 12)
End Sub

don't put delegate in NetClient's class.
regards








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
 
S

Supra

u may have to used addhandled keyword
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top