 |
|
 |
Thanks for this, it works beautifully. I am using the control and the send works perfectly, but the client_AfterReceive sub is not being triggered. everything else seem to work however? any idea?
|
|
|
|
 |
|
 |
Event triggers for me but message is nothing
|
|
|
|
 |
|
 |
it was with a lot of surprise that i found out that it was very difficult to implement a udp listener on a asp.net page (web-based) please help me thank you in advanced
|
|
|
|
 |
|
 |
Hello,
Thanks for your clean code which explain a lot,
I have noticed that the 1st message sent usually it is lost, but after that it is OK, the 2nd thing is that if I send repeated messages the server can not display many messages.
I have tried to move the InitializeThread() from the Finally block to the new position after the RaiseEvent AfterReceive(Me, New EventArgs), when I have tried it the server was more stable.
|
|
|
|
 |
|
 |
What kind of license has this code?
|
|
|
|
 |
|
 |
i have UDP send program that working well in LAN.But when i try it on the internet,i try to send msg to dynamic ip address(behind router) it seems the destination pc cannot receive any message from the udp sender.
please help me
|
|
|
|
 |
|
 |
The STOP method for terminating the UDP blocking receive don't work. If you call it the _ThreadReceive will always try to reinitialize itself because the Finally block call again the InitializeThread. I have solved rewritting Receive() Public Sub Receive() Dim bAbortedByStop As Boolean '<--- ADDED ' Clear the Message property _Message = "" ' Raise the BeforeReceive event RaiseEvent BeforeReceive(Me, New EventArgs) ' Receive our UDP data Dim _data As Byte() Try Select Case Protocol Case ProtocolType.Udp _data = _UDPClient.Receive(_Server) _BytesReceived = _data.Length Case Else Throw New ProtocolNotSupportedException End Select Catch ex As ThreadAbortException '<--- ADDED bAbortedByStop = True Exit Sub Catch ex As Exception Throw ex Finally ' The thread finished blocking, and ended, so we start again If Not bAbortedByStop Then '<--- ADDED InitializeThread() End If End Try .....
Also the stop cannot SUSPEND the thread otherwise it wont properly abort, also i have used .Join to syncronize with the _ReceiveThread when it deallocate from memory Public Sub [Stop]() ' Close the UDPClient and stop the worker thread Try ' Suspend the thread and then abort it. Keeps it from ' continuing to try to process anything further while ' it winds down. If Not _ThreadReceive Is Nothing Then '_ThreadReceive.Suspend() '<-- Commented _ThreadReceive.Abort() End If If Not (_UDPClient Is Nothing) Then ' Close the UDPClient and then force it to Nothing _UDPClient.Close() _UDPClient = Nothing End If If Not _ThreadReceive Is Nothing Then '<-- Added _ThreadReceive.Join() _ThreadReceive = Nothing End If Catch ex As Exception Throw ex End Try End Sub
Hope this helps. Paolo Marani
|
|
|
|
 |
|
 |
First I am a beginning programmer. I have an application where I need to open a UDP socket, and store the information to a file.
I need code to set up a udp socket, accept data, parse it into a few blocks of data, and then write it into a file. The file could be a csv or an access mdb.
|
|
|
|
 |
|
 |
Hi,
I am developing an app to communicate to and from a Win Mobile 5 device over GPRS. I first achieved this using a TCP socket solution, however have found taht this implementation is too data hungry. I have been able to get a UDP connection to work from both client and server between 2 machines on a LAN.
The problem I encouter is when I try and implement this over GPRS. My GPRS Provider is Telstra(Australia) It appears that there is no problem to send via UDP from the Mobile device back to the server, but cannot send from the server to the Mobile device.
I think this may be relating to the carriers firewall settings. I believe that when the Mobile device sends via udp to the serverside it sets up the UDP source port/destination port mapping, and I must ensure host sends back to the same source port, and the opening in the firewall only lasts 2 minutes.
I donnot have a great understanding of this and would be great if someone else has implemented a solution whereby they can send and recieve data, between a Mobile Device on a GPRS network(client) and a normal PC with a public IP(server).
Many thanks in advance
David
|
|
|
|
 |
|
 |
I am having trouble sending data from a cell phone using the TinyUdp to a server. Basically I am sending GPS data from the cell phone to a central server. I have no trouble sending data to the server over udp from a laptop running the program. However, any data from a mobile device over a cellular network does not seem to go through. You mentioned here you were sucessful in doing this? Can you give me some help on how you did this.
Thanks
|
|
|
|
 |
|
 |
Hi, I'm really happy that you used my article and learned something. Also its very nice that you have mentioned it. By the way great atricle
Kum
|
|
|
|
 |
|
 |
I need a source code that view installed applications and run them.
Thanks.;)
MjRazzaghi
|
|
|
|
 |
|
 |
Hi,
I'm working on a application for UDP comunication and i'm starting to a c application;
the message to send is :
sprintf(tx,"\001@:R\r");
where tx is a char but in C# i'm trying to send:
string sMessageToSend="\001@:R\r";
GLOIP = IPAddress.Parse(ipTextBox1.Text);
GLOINTPORT = Convert.ToInt16(mPortSendRecive.Text);
udpClient.Connect(GLOIP, GLOINTPORT);
bytCommand = Encoding.ASCII.GetBytes(sMessageToSend);
pRet = udpClient.Send(bytCommand, bytCommand.Length);
but i don't recive the same message.
Can you help me?
ALL TOGETHER
|
|
|
|
 |
|
 |
Hi All.. I am having a few issues on creating a new form by raising an event from the child thread. I have created a small scenario demonstrating this, please could anyone give some sugestions.
' Main Form loaded
Dim f As New Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
start.start()
End Sub
Dim WithEvents start As New Class1
Private Sub newf()
'Dim f As New Form2
f.Show()
f.Visible = True
End Sub
Private Sub start_cge() Handles start.cge
newf()
End Sub
End Class
' Class containg the thread create and event
Imports System.Threading
Public Class Class1
Public Event cge()
Public WithEvents tr1 As New System.Windows.Forms.Timer
Dim t As Thread
Public Sub start()
tr1.Interval = 2000
tr1.Start()
End Sub
Public Sub ttick(ByVal s As Object, ByVal e As System.EventArgs) Handles tr1.Tick
t = New Thread(AddressOf ev)
t.Start()
End Sub
Public Sub ev()
RaiseEvent cge()
End Sub
End Class
Any help would be greatly appreciated
Today is another day
|
|
|
|
 |
|
 |
i have test your sample projects and it works okay. sending of data is accurate but when i look at the memory usage of the application, it increases frequently. what are the cause of memory increase? Is there any a couple of statement to optimize the applicaton?
Thanks
|
|
|
|
 |
|
 |
Hi to all,
do you remember when we could make an easy UDP connection with VB 6.0?
we needed only to drag and drop the winsock... one minutes of codes and... we could make two applications "talk together"!!!
Now i have a problem!!!
I want to do the same thing with "Microsoft Visual Studio 2005"... but there is a new way to program that i don't know... even i don't know what is the name/identifier of this new language... (maybe "VB.NET 2005"?)
I have found, on this site, articles/source on UDP connection... but "Microsoft Visual Studio 2005" say that this code is old so it make a convertion... EVEN THOUGH I DON'T UNDERSTAND THE SOURCE!!!
There are a lot more lines of code...
Please Help Me... I NEED A SMALL EXAMPLE (SOURCE FOR "Microsoft Visual Studio 2005") LIKE TWO FORMS THAT CAN CHAT BETWEEN THEM!!!
THAKN YOU ALL!!!
And excuse my english
|
|
|
|
 |
|
 |
Hi all, this wrapper is great: simple and easy to use, thanks!
One problem: (it has been mentioned earlier) is that I cannot get my program completely shutdown. The program still runs in memory. I also tried this with the example program as downloaded from this page.
I have used the code `client.stop()` and tried loads of other things to get that thread cleaned, but nothing seems to help.
I must note that I loaded this code with VB.NET 2005 / .Net 2.0. This might cause the problem? (Thread.Suspend seems to be disposed of with this version).
Has anyone got this completely running with .Net 2.0? Or maybe someone is willing to look into it?
Thanks, it would be great to continue this great piece of code in the 2.0 version of .Net!
|
|
|
|
 |
|
 |
The problem is that _data = _UDPClient.Receive(_Server) does not 'unblock' when _ThreadReceive.Abort() is called. My solution (look for the code between '***BKP Start and '***BKP End): Public Sub [Stop]() ' Close the UDPClient and stop the worker thread Try ' Suspend the thread and then abort it. Keeps it from ' continuing to try to process anything further while ' it winds down. '***BKP Start '_ThreadReceive.Suspend() '_ThreadReceive.Abort() _UDPClient.Close() '***BKP End If Not (_UDPClient Is Nothing) Then ' Close the UDPClient and then force it to Nothing _UDPClient = Nothing End If Catch ex As Exception Throw ex End Try End Sub This will cause _UDPClient.Receive(_Server) to throw a System.net.sockets.SocketException with error code 10004. So you'll have to change Public Sub Receive() to handle it how you wish. Here is my solution (I'm not real happy about the return in the catch block, but hey it's a quick fix): Public Sub Receive() 'Existing code Select Case Protocol Case ProtocolType.Udp _data = _UDPClient.Receive(_Server) _BytesReceived = _data.Length Case Else Throw New ProtocolNotSupportedException End Select '***BKP Start Catch socketEx As System.Net.Sockets.SocketException If socketEx.ErrorCode <> 10004 Then Throw socketEx Else Return End If '***BKP End Catch ex As Exception Throw ex Finally
|
|
|
|
 |
|
 |
This solution did not work for me (it did not unblock the Receive()). Instead I used a different tactic, namely waking up the thread by sending to it a udp package containing a special string that the udp-package processing part recognized as a STOP request.
Ari
|
|
|
|
 |
|
 |
I'm not too familiar with threads and everything, but in my program I do a lot of client.stop and client.starts... (Possibly a couple thousand times in 24 hours!) (this is because I use the same server.sourceport as my port the client is listening on) I notice when the program first opens, and you first start the server, the cpu usage stays relatively small, normally @ 0%. BUT, whenever I stop the server, then start it again, CPU usage jumps up to 99% and stays relatively there.
Do you know any reason this could be? I read something about suspending threads and then resuming them, would this work instead of stopping and starting?
|
|
|
|
 |
|
 |
I got some crazy problem on one PC. When another PC is sending a message to the pc I get an Error while doing the folowing line:
Me.Invoke(New _DisplayMessage(AddressOf DisplayMessage))
is there a typical reason why. Or why is this needed. Can I leave it away?
M.f.G.
Lost-Ha[n]f-PHP
----------------
Besucht www.workstation.de.ki
|
|
|
|
 |
|
 |
Invoke is needed because you are updating the UI from a worker thread. Trying to update the UI from a worker thread without checking .InvokeRequired and using .Invoke (or .BeginInvoke) will cause serious problems. It's hard to figure out the problem you're having without knowing the nature of the error...
|
|
|
|
 |
|
 |
Client listens, server sends?? Shouldn't it be the reverse?
|
|
|
|
 |
|
 |
The naming convention chosen was for a specific application for which this was originally developed. If you think in terms of a push-type server (Anti-virus servers that push their updates onto clients come readily to mind), the naming convention is correct. If you think in terms of a Web server, where the client makes the initial request to the server, then the naming convention is reversed.
As I'm sure you're well aware, the source code (which is supplied) can be easily modified to reverse the naming convention to your specific requirements.
|
|
|
|
 |
|
 |
I've changed the names of both classes to their reverse... I've also made a fix for the exiting problem. I hope it helps someone...
Here the code (remember to rename the filename too):
TinyClient.vb
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Imports System.ComponentModel
Namespace Tiny.UDP
Public Class TinyClient
Inherits System.ComponentModel.Component
#Region " Component Designer generated code "
Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()
'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)
End Sub
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
Private Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
' Default property values
Private _Protocol As ProtocolType = ProtocolType.Udp
Private _ServerAddress As IPAddress = IPAddress.Any
Private _ServerPort As Integer = 0
Private _data As Byte() = New Byte() {}
Private _Encode As EncodingType
_
Public Property Protocol() As ProtocolType
Get
Return (_Protocol)
End Get
Set(ByVal Value As ProtocolType)
If (Value = ProtocolType.Udp) Then
_Protocol = Value
Else
Throw New ProtocolNotSupportedException
End If
End Set
End Property
_
Public Property Server() As IPEndPoint
Get
Return New IPEndPoint(_ServerAddress, _ServerPort)
End Get
Set(ByVal Value As IPEndPoint)
_ServerAddress = Value.Address
_ServerPort = Value.Port
End Set
End Property
_
Public Property ServerAddress() As IPAddress
Get
Return _ServerAddress
End Get
Set(ByVal Value As IPAddress)
_ServerAddress = Value
End Set
End Property
_
Public Property ServerPort() As Integer
Get
Return _ServerPort
End Get
Set(ByVal Value As Integer)
_ServerPort = Value
End Set
End Property
_
Public Property Encode() As EncodingType
Get
Return (_Encode)
End Get
Set(ByVal Value As EncodingType)
_Encode = Value
End Set
End Property
Public Sub SendMessage(ByVal message As String)
' Encode message per settings
Select Case Encode
Case EncodingType.Default
_data = Encoding.Default.GetBytes(message)
Case EncodingType.ASCII
_data = Encoding.ASCII.GetBytes(message)
Case EncodingType.Unicode
_data = Encoding.Unicode.GetBytes(message)
Case EncodingType.UTF7
_data = Encoding.UTF7.GetBytes(message)
Case EncodingType.UTF8
_data = Encoding.UTF8.GetBytes(message)
Case Else
Throw New BadEncodingException
End Select
' Send the message
Try
Select Case Protocol
Case ProtocolType.Udp
SendUDPMessage(_data)
End Select
Catch ex As Exception
Throw ex
End Try
End Sub
Private Function SendUDPMessage(ByVal _data As Byte()) As Integer
' Create a UDP Server and send the message, then clean up
Dim _UDPClient As UdpClient = Nothing
Dim ReturnCode As Integer
Try
_UDPClient = New UdpClient
ReturnCode = 0
_UDPClient.Connect(Server)
ReturnCode = _UDPClient.Send(_data, _data.Length)
Catch ex As Exception
Throw ex
Finally
If Not (_UDPClient Is Nothing) Then
_UDPClient.Close()
End If
End Try
Return ReturnCode
End Function
End Class
End Namespace
TinyServer.vb
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.ComponentModel
Namespace Tiny.UDP
Public Class TinyServer
Inherits System.ComponentModel.Component
#Region " Component Designer generated code "
Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()
'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)
End Sub
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
Private Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
' Default Property values
Private _Protocol As ProtocolType = ProtocolType.Udp
Private _ThreadReceive As Thread
Private _ClientPort As Integer = 0
Private _Message As String = ""
Private _Encode As EncodingType = EncodingType.Default
Private _UDPClient As UdpClient
Private _Client As New IPEndPoint(IPAddress.Any, 0)
Private _BytesReceived As Integer = 0
Private _closing As Boolean = False
_
Public Event BeforeReceive As EventHandler
_
Public Event AfterReceive As EventHandler
_
Public Property Encode() As EncodingType
Get
Return (_Encode)
End Get
Set(ByVal Value As EncodingType)
_Encode = Value
End Set
End Property
_
Public ReadOnly Property BytesReceived() As Integer
Get
Return (_BytesReceived)
End Get
End Property
_
Public ReadOnly Property Message() As String
Get
Return (_Message)
End Get
End Property
_
Public Property Client() As IPEndPoint
Get
Return _Client
End Get
Set(ByVal Value As IPEndPoint)
_Client = Value
End Set
End Property
_
Public Property ServerPort() As Integer
Get
Return (_ClientPort)
End Get
Set(ByVal Value As Integer)
_ClientPort = Value
End Set
End Property
_
Public Property Protocol() As ProtocolType
Get
Return (_Protocol)
End Get
Set(ByVal Value As ProtocolType)
_Protocol = Value
End Set
End Property
Public Sub Receive()
' Clear the Message property
_Message = ""
' Raise the BeforeReceive event
RaiseEvent BeforeReceive(Me, New EventArgs)
' Receive our UDP data
Dim _data As Byte()
Try
Select Case Protocol
Case ProtocolType.Udp
_data = _UDPClient.Receive(_Client)
_BytesReceived = _data.Length
Case Else
Throw New ProtocolNotSupportedException
End Select
Catch ex As Exception
If Not _closing Then
Throw ex
Else
Exit Sub
End If
Finally
' The thread finished blocking, and ended, so we start again
If Not _closing Then InitializeThread()
End Try
' Encode the data per the Encode property
Dim _strdata As String
Select Case Encode
Case EncodingType.Default
_strdata = System.Text.Encoding.Default.GetString(_data)
Case EncodingType.ASCII
_strdata = System.Text.Encoding.ASCII.GetString(_data)
Case EncodingType.Unicode
_strdata = System.Text.Encoding.Unicode.GetString(_data)
Case EncodingType.UTF7
_strdata = System.Text.Encoding.UTF7.GetString(_data)
Case EncodingType.UTF8
_strdata = System.Text.Encoding.UTF8.GetString(_data)
Case Else
Throw New BadEncodingException
End Select
' Set the message
_Message = _strdata
' Raise the AfterReceive event
RaiseEvent AfterReceive(Me, New EventArgs)
End Sub
Private Sub InitializeClient()
' Configure a UDPClient
Select Case Protocol
Case ProtocolType.Udp
If (_UDPClient Is Nothing) Then
_UDPClient = New UdpClient(ServerPort)
End If
Case Else
Throw New ProtocolNotSupportedException
End Select
End Sub
Private Sub InitializeThread()
' Start a worker thread
Try
_ThreadReceive = New Thread(AddressOf Receive)
_ThreadReceive.Start()
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub Start()
' Initialize the Client and the Thread
InitializeClient()
InitializeThread()
End Sub
Public Sub [Stop]()
' Close the UDPClient and stop the worker thread
Try
_closing = True
If Not (_UDPClient Is Nothing) Then
' Close the UDPClient and then force it to Nothing
_UDPClient.Close()
_UDPClient = Nothing
End If
_ThreadReceive.Abort()
Catch ex As Exception
Throw ex
End Try
End Sub
End Class
End Namespace
|
|
|
|
 |