UDP Socket in Web to receive?

Hi all,

I’m wondering how to receive data via a udp socket on my web app. I can send data via a socket initiated via code, but can’t figure out how to read data client side.

I’m trying to send data from a desktop application to my web app (running on the same PC).

Should I be looking at another way to do this or will UDP work?

Thanks!
James

Why UDP? why not use HTTP and App.HandleSpecialURL or App.HandleURL ?

Thanks Dirk

This will do the trick! I havent used the web platform much and didn’t know about it!

Thanks again!

James

This is hard to get the right answer from XOJO, I don’t understand why?, I try many way to make it work, but no, no one can send you the right hints, same as what you need a simple sample code for UDP or TCP receive data to my Web apps.

This is what XOJO so difficult and not popular! it is hard to get any answer…good luck!

[quote=400365:@MK Siu]This is hard to get the right answer from XOJO, I don’t understand why?, I try many way to make it work, but no, no one can send you the right hints, same as what you need a simple sample code for UDP or TCP receive data to my Web apps.

This is what XOJO so difficult and not popular! it is hard to get any answer…good luck![/quote]
Part of the reason is that without knowing the details of your app there is no “right answer”, so I’ll suggest that you look at the example projects for UDP and ServerSocket (for TCP).

He didn’t ask anything on this forum, IMO this is a BS-message of MK Siu, just kicking to cause trouble. Ignore it!

Andre, he’s a beta tester and therefore a paying customer and deserves the same respect as anyone else. If you don’t mind, I’m just trying to help.

[quote=400365:@MK Siu]This is hard to get the right answer from XOJO, I don’t understand why?, I try many way to make it work, but no, no one can send you the right hints, same as what you need a simple sample code for UDP or TCP receive data to my Web apps.

This is what XOJO so difficult and not popular! it is hard to get any answer…good luck![/quote]

It is very hard for any of us on this forum to give you an answer when you have never asked a question! And in that context is very unfair of you to suggest that we have not been helpful, we are not XOJO employees, just other developers like yourself. An apology is in order.

Hi guys.

In my case ‘handlespecialurl’ worked well. I couldn’t get a udp receive socket to work but that could have been me;)

J

I apology to you guys! I just feel every sorry of this problem!
The question is quite simple and straight forward, I try to receive some text message from desktop server to my web client, I searched the whole web, I cann’t get the solution! I know this is not easy because I know the web browser does not have permission for receive any data, even that I have no idea about the syntax!
I spent the whole week and stick up in the question, I feel quite worry and frustrated about how XOJO can do that or not!?

Please have a look for the follows, may be someone can give me some idea, thank you so much.

Dim socket1 As New TCPSocket

// set its port and address
// TextField1 contains a valid url, such as “www.realsoftware.com
socket1.Address = “210.3.53.xx”
socket1.Port = 1900

// connect the socket
socket1.Connect

// while the socket isn’t connected
While Not socket1.IsConnected
// check to see if the socket got an error
If socket1.LastErrorCode <> 0 Then
Exit
End If
// poll the socket to let it do its thing
socket1.Poll
Wend

// if we broke the loop because we’re connected
If socket1.IsConnected Then
// here would be a great place to do a synchronous read operation…

dim res as string
Dim d As New Date
res =  d.LongDate + " @ " + d.ShortTime

Socket1.Write "Client:  " + res
Socket1.Poll                                 <<<<<<<< it work! server can received all test and reply ....

App.DoEvents
' 
' some delay
'
if socket1.BytesAvailable <> 0  then
  dim tx as string
  
  tx = socket1.ReadAll                  <<<<<< No message reply from server isde
  msgbox tx
end if

Else
//The socket broke out of the loop due to an error
End If

// close the socket
socket1.Close

Thanks again for all kind people!

First of all, going forward come here first if you want answers about Xojo. You won’t find a better forum for that than this one, the people are generally nicer and you will often get more than one good answer.

Ok, there are a few problems with your code. Firstly, to be clear the code you’ve written above doesn’t actually run in the browser. It runs in the web app on the server and the results are transmitted to the browser afterwards.

Next, I suggest you stop trying to do this synchronously using DoEvents. Subclass TCPSocket, implement the DataAvailable event and deal with the data as it comes in. Otherwise your web app will be very slow. There are specific uses for DoEvents like creating your own event loop in a console app but most of the time what you get is a very hard to debug bit of code. In a web app it could cause thread context changes which can lead to session data leakage depending on how your code is written elsewhere.

Next, you don’t need Socket.Poll. There is only a few very specialized uses for that call and a simple routine like this isn’t one of them. You’re forcing the socket to do things earlier than they otherwise would, but will result in bad performance if more than one session is calling this code at a time.

Next, you are making an assumption that the server will respond immediately based on this comment

//The socket broke out of the loop due to an error

The DoEvents call you’ve made above (which you shouldn’t be using anyway), defaults to a 10 millisecond delay… and it’s not a loop in of itself. By the time 10ms has occurred, the server may not have completely responded. It may work on local testing, but in the real world, I doubt it.

It’s also important to remember that data from a socket always comes in on the main thread. Since webSessions execute commands on their own cooperative thread, you’ll have to guard against this and make sure the data is sent to the correct session.

It would also be helpful to have a better understanding of what you are trying to accomplish though. I don’t have a clear understanding of that and a description may lead to more complete ideas or suggestions.

FWIW, people on this forum tend to offer advice rather than code (as you can see from my note above). You may get a code snippet from time to time, or if someone has solved your particular problem, you may get a link to some code that might be helpful, but mostly it’s helpful guidance for you to take what you have and figure out the problem on your own so you actually learn it.