473,805 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Sock ets
Imports System.Text
Imports System.Threadin g

Public Class NetClient
Public Event Connected()
Public Event Disconnected()
Public Event DataRecieved(By Val 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="ServerNam e">Hostname of the server</param>
''' <param name="ServerPor t">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(Ser verName).Addres sList(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(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p)
'-- Connect Async
ClientSocket.Be ginConnect(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.Sh utdown(SocketSh utdown.Both)
ClientSocket.Cl ose()
RaiseEvent Disconnected()
End Sub
Private Sub ConnectCallback (ByVal ar As IAsyncResult)
ClientSocket.En dConnect(ar)
RaiseEvent Connected()
'-- Begin recieving data
Dim buff(4096) As Byte
ClientSocket.Be ginReceive(buff , 0, buff.Length,
SocketFlags.Non e, AddressOf RecieveCallBack , buff)
End Sub
Public Sub DataProcess(ByV al DataSlot() As Byte, ByVal BytesRecvd
As Integer)
SyncLock Me.objLockA
If BytesRecvd = 0 Then '-- Server has closed connection
ClientSocket.Sh utdown(SocketSh utdown.Both)
ClientSocket.Cl ose()
Else '-- We have data
Dim Recv As String = Enc.GetString(D ataSlot)
'-- Clear the buffer
Array.Clear(Dat aSlot, 0, DataSlot.Length )
'-- Begin recieve again
RaiseEvent DataRecieved(Me , Recv)
'ClientSocket.B eginReceive(Dat aSlot, 0,
DataSlot.Length , SocketFlags.Non e, AddressOf RecieveCallBack , buff)
End If
End SyncLock
End Sub
Private Sub RecieveCallBack (ByVal ar As IAsyncResult)
Try
Dim buff() As Byte = CType(ar.AsyncS tate, Byte())
Dim numBytes As Integer = ClientSocket.En dReceive(ar)
'Dim buff(2048) As Byte
' Dim dlg As New TwoArg(AddressO f DataProcess)
'dlg.Invoke(buf f, numBytes)
If numBytes = 0 Then '-- Server has closed connection
ClientSocket.Sh utdown(SocketSh utdown.Both)
ClientSocket.Cl ose()
Else '-- We have data
Dim Recv As String = Enc.GetString(b uff)
'-- Clear the buffer
Array.Clear(buf f, 0, buff.Length)
'-- Begin recieve again
RaiseEvent DataRecieved(Me , Recv)
ClientSocket.Be ginReceive(buff , 0, buff.Length,
SocketFlags.Non e, AddressOf RecieveCallBack , buff)
End If
Catch ex As Exception
'Throw New Exception(ex.Me ssage, ex.InnerExcepti on)
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.GetBy tes(DataOut)
ClientSocket.Se nd(buff)
End Sub
End Class
_______________ _______________ _______________ _______________ ____________

Thank you,
Jody
Nov 21 '05 #1
2 1483
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_onS everMessage(ByV al szText As String)
Handles _Connection.onS everMessage
deleg = New OneArgSub(Addre ssOf DisplayMessage)
deleg.Invoke(nS tatus.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.Sock ets
Imports System.Text
Imports System.Threadin g

Public Class NetClient
Public Event Connected()
Public Event Disconnected()
Public Event DataRecieved(By Val 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="ServerNam e">Hostname of the server</param>
''' <param name="ServerPor t">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(Ser verName).Addres sList(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(AddressF amily.InterNetw ork,
SocketType.Str eam, ProtocolType.Tc p)
'-- Connect Async
ClientSocket.Be ginConnect(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.Sh utdown(SocketSh utdown.Both)
ClientSocket.Cl ose()
RaiseEvent Disconnected()
End Sub
Private Sub ConnectCallback (ByVal ar As IAsyncResult)
ClientSocket.En dConnect(ar)
RaiseEvent Connected()
'-- Begin recieving data
Dim buff(4096) As Byte
ClientSocket.Be ginReceive(buff , 0, buff.Length,
SocketFlags.No ne, AddressOf RecieveCallBack , buff)
End Sub
Public Sub DataProcess(ByV al DataSlot() As Byte, ByVal BytesRecvd
As Integer)
SyncLock Me.objLockA
If BytesRecvd = 0 Then '-- Server has closed connection
ClientSocket.Sh utdown(SocketSh utdown.Both)
ClientSocket.Cl ose()
Else '-- We have data
Dim Recv As String = Enc.GetString(D ataSlot)
'-- Clear the buffer
Array.Clear(Dat aSlot, 0, DataSlot.Length )
'-- Begin recieve again
RaiseEvent DataRecieved(Me , Recv)
'ClientSocket.B eginReceive(Dat aSlot, 0,
DataSlot.Lengt h, SocketFlags.Non e, AddressOf RecieveCallBack , buff)
End If
End SyncLock
End Sub
Private Sub RecieveCallBack (ByVal ar As IAsyncResult)
Try
Dim buff() As Byte = CType(ar.AsyncS tate, Byte())
Dim numBytes As Integer = ClientSocket.En dReceive(ar)
'Dim buff(2048) As Byte
' Dim dlg As New TwoArg(AddressO f DataProcess)
'dlg.Invoke(buf f, numBytes)
If numBytes = 0 Then '-- Server has closed connection
ClientSocket.Sh utdown(SocketSh utdown.Both)
ClientSocket.Cl ose()
Else '-- We have data
Dim Recv As String = Enc.GetString(b uff)
'-- Clear the buffer
Array.Clear(buf f, 0, buff.Length)
'-- Begin recieve again
RaiseEvent DataRecieved(Me , Recv)
ClientSocket.Be ginReceive(buff , 0, buff.Length,
SocketFlags.No ne, AddressOf RecieveCallBack , buff)
End If
Catch ex As Exception
'Throw New Exception(ex.Me ssage, ex.InnerExcepti on)
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.GetBy tes(DataOut)
ClientSocket.Se nd(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.Sock ets
Imports System.Text
Imports System.Threadin g

Public Class NetClient
Public Event Connected()
Public Event Disconnected()
Public Event DataRecieved(By Val 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="ServerNam e">Hostname of the server</param>
''' <param name="ServerPor t">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(Ser verName).Addres sList(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(AddressF amily.InterNetw ork,
SocketType.Str eam, ProtocolType.Tc p)
'-- Connect Async
ClientSocket.Be ginConnect(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.Sh utdown(SocketSh utdown.Both)
ClientSocket.Cl ose()
RaiseEvent Disconnected()
End Sub
Private Sub ConnectCallback (ByVal ar As IAsyncResult)
ClientSocket.En dConnect(ar)
RaiseEvent Connected()
'-- Begin recieving data
Dim buff(4096) As Byte
ClientSocket.Be ginReceive(buff , 0, buff.Length,
SocketFlags.No ne, AddressOf RecieveCallBack , buff)
End Sub
Public Sub DataProcess(ByV al DataSlot() As Byte, ByVal BytesRecvd
As Integer)
SyncLock Me.objLockA
If BytesRecvd = 0 Then '-- Server has closed connection
ClientSocket.Sh utdown(SocketSh utdown.Both)
ClientSocket.Cl ose()
Else '-- We have data
Dim Recv As String = Enc.GetString(D ataSlot)
'-- Clear the buffer
Array.Clear(Dat aSlot, 0, DataSlot.Length )
'-- Begin recieve again
RaiseEvent DataRecieved(Me , Recv)
'ClientSocket.B eginReceive(Dat aSlot, 0,
DataSlot.Lengt h, SocketFlags.Non e, AddressOf RecieveCallBack , buff)
End If
End SyncLock
End Sub
Private Sub RecieveCallBack (ByVal ar As IAsyncResult)
Try
Dim buff() As Byte = CType(ar.AsyncS tate, Byte())
Dim numBytes As Integer = ClientSocket.En dReceive(ar)
'Dim buff(2048) As Byte
' Dim dlg As New TwoArg(AddressO f DataProcess)
'dlg.Invoke(buf f, numBytes)
If numBytes = 0 Then '-- Server has closed connection
ClientSocket.Sh utdown(SocketSh utdown.Both)
ClientSocket.Cl ose()
Else '-- We have data
Dim Recv As String = Enc.GetString(b uff)
'-- Clear the buffer
Array.Clear(buf f, 0, buff.Length)
'-- Begin recieve again
RaiseEvent DataRecieved(Me , Recv)
ClientSocket.Be ginReceive(buff , 0, buff.Length,
SocketFlags.No ne, AddressOf RecieveCallBack , buff)
End If
Catch ex As Exception
'Throw New Exception(ex.Me ssage, ex.InnerExcepti on)
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.GetBy tes(DataOut)
ClientSocket.Se nd(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
1769
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 is, when I finish debugging ValuesChanged and click the stop debugging button, it appears the code continues behind these scenes and runs
2
4895
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
1836
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 there some special trick I have to use here? Thanks
1
1181
by: Kiran | last post by:
how to send and recieve objects to java web services from .net client
1
2013
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 receiving that message on the server side the server is suppose to send a message back. I can't get my app to receive that message. It only receives the one being sent out by my machine. I know that the server is sending out a message because there is...
7
2747
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 recieve the message and responds correctly, and replies with a broadcast frame. Here is where i face the problem, what happens is that my application seems to Time out even if Ethereal ( Network Sniffer ) detects and shows all the frames that were
2
3928
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 icon in the system tray. So far so good. I'd like for the app to maximize outside of the system tray when the Windows + ? combination keys are selected. It seems that I can catch keystrokes when the form is showing but when minimized(Hide()) it in...
6
2265
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 entered anything? if __name__ == "__main__": bucket_name = raw_input('Name of the bucket you wish the files to be placed into? ') update_s3()
1
2051
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 sure if we can 1b. recieve a call 2b., receieve an sms.
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10609
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10105
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9185
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.