473,386 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Continues TCP recieve

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
Nov 21 '05 #1
2 1469
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



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


Nov 21 '05 #2
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

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ENathan | last post by:
I have a public sub in a class that does something like: If ValuesChanged() Then UpdateDatabase End If ValuesChanged and UpdateDatabase are Private functions within the class. My problem...
2
by: K.K. | last post by:
I am writing program that will recieve or sent data to selected port (users can select which port they want to recieve or sent) but I don't know how or whick object I can use.
2
by: Robin Tucker | last post by:
Hiya, I can't get my panel to recieve mousewheel events at all. When I handle the Mousedown event, I call Me.Focus() in order to have the input focus, but my mousewheel event never fires. Is...
1
by: Kiran | last post by:
how to send and recieve objects to java web services from .net client
1
by: Dave | last post by:
I can't figure out why my code does not recieve the message. The only message that it receives is the one going out from my machine. I am suppose to send out a message to a server. Upon...
7
by: sreehari | last post by:
Hi All, I have this problem, I have developed a Master - Slave kinda appliacation , were the slave is generally a device.when i broadcast a message from my application, the Device seems to...
2
by: ericlangland | last post by:
Hi, I have a small managed code application (windows forms) that executes on startup and immidiatly minimizes to the system tray. It launches and shows the form when I double click it's small...
6
by: oliver | last post by:
Hey Everyone, This is probably going to sound like a bit of a stupid question - but why does (in the following code) the script just continue to run past the raw_input, when the user hasn't...
1
by: 4project | last post by:
i wanted to design a user interface such that i can handle all the incoming and outgoing calls , also send and recieve sms. I know that for sure that we can 1a .call 2a send sms but not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.