UDP Examples
Rockyboa23-Dec-2008/1:08:11+1:00
Je crois avoir des problèmes avec un de mes serveurs virtuels. Je crois que les paquets UDP ne passent pas bien a travers mon interface réseau virtuel. Je n'ai eu auncun problème a faire un petit serveur et client TCP, mais UDP semble ne pas fonctionner du tout de la même facon.

Si je voudrais me faire un petit truc qui ouvre un port UDP sur le serveur et vérifie si tout va bien d'un client, aurais-t-il un example de code sur lequel je pourrais me pencher et analyser..

Merci des pointeurs
ldci23-Dec-2008/15:50:41+1:00
Salut voici un exemple de connexion UDP qui fonctionnait avec le bus IVY
Bons tests


Le serveur

REBOL [
Title: "Rebol Ivy Bus Monitor"
Author: "François Jouen"
]

Default_Brodcast: 127.255.255.255
Default_Port: 2010
Connected: false
App_Name: "Rebol Client"


Connect: does [
   udp-in: open/lines join udp://:(Default_Port)
   tcp-in: open/lines join tcp://:(Default_Port + 1)
   udp-out: open/lines to-url join "udp://" cnx_domain/text
   set-modes udp-out [broadcast: on]
   Connected: true
   insert udp-out join app_name [" " (Default_Port + 1)]
   show console
   time: now/time
   str: join "[" [time "] Connect Domain [" cnx_Domain/text "]" newline]
   
   if cb2 [messages/text: head messages/text insert messages/text str]
   show messages
   Process
]

Disconnect: does [
   close udp-in
   close tcp-in
   close udp-out
   Connected: false
   time: now/time
   str: join "[" [time "] Disconnect Domain" newline]
   if cb2 [messages/text: head messages/text insert messages/text str]
   show messages
]

Logout: does [
if confirm "Quit ?" [
   if connected [insert udp-out reform [App_Name "left."] Disconnect]
   
quit]
]

Process: does [
forever [

connexion: wait [udp-in tcp-in]
if not viewed? mainwin [Logout]
   
if connexion [
      if connexion/scheme = 'udp [append console/text copy connexion]
      if connexion/scheme = 'tcp [append messages/text copy first connexion]
      show [console messages]
   ]   
sl1/data: 1
scroll-para console sl1
show sl1
]
]
MainWin: layout [
   origin 0x0
   across
   at 5x5 box 375x100 frame gray
   at 10x10 label 100 left "Domain" cnx_domain: field 150 join to-string Default_Brodcast[":" Default_Port]
   cnx: toggle gray "Connect" "Disconnect" [either face/data [Connect] [Disconnect] ]
   at 10x40 label 100 left "Reg. Exp." cnx_expression: field 150 "(.*)" expression: toggle gray "Bind" "Unbind"
   at 10x70 label 100 left "Message" talk: field 150 "Hello World" message: button gray "Send Message"
   
   at 10x105 label "Clients"
   at 5x125 box 375x100 frame gray
   space 0x5
   at 10x130 console: area 350x90 wrap
   sl1: slider 16x90 [scroll-para console sl1]
   at 10x225 label "Output"
   at 5x245 box 375x300 frame gray
   space 5x5
   at 10x260 text "Messages" cb1: check true
   text "Status" cb2: check true
   text "Errors" cb3: check true
   at 275x255 button gray "Clear Output" [clear messages/text messages/line-list: none show messages ]
   space 0x5
   at 10x285 messages: area 350x250 wrap
   sl2: slider 16x250 [scroll-para messages sl2]
   at 275x550 button gray "Quit" [Logout]
]
View Center-face MainWin

et maintenant un client

REBOL [
Title: "Client Bus Ivy"
Author: "François Jouen"
]

app_name: "Rebol Client"

cnx: false

aport: 2010

Connect: does [
   either not cnx [
   udp-in: open/no-wait/lines join udp://:(aport)
   tcp-in: open/no-wait join tcp://:(aport + 1)
   udp-out: open/lines join udp://127.255.255.255:(aport)
   set-modes udp-out [broadcast: on]
   cnx: true
   insert udp-out join name/text [" " (aport + 1)]
   show console
   ]
   [alert "Connexion already exists! "]
   
]

Logout: does [
if confirm "Quit ?" [
   if cnx [insert udp-out reform [name/text "left."]]
quit]
]

Send_Message: does [
   either cnx [
if empty? talk/text [exit]
insert udp-out rejoin [name/text ": " talk/text]
clear talk/text
]
[alert "Please connect !"]
]

Process: does [
forever [
focus talk
connexion: wait [udp-in tcp-in]
if not viewed? mainwin [Logout]
   
if connexion [
      if connexion/scheme = 'udp [append console/text copy connexion]
      if connexion/scheme = 'tcp [append messages/text copy first connexion]
      show [console messages]
   ]   
sl/data: 1
scroll-para console sl
show sl
]
]




MainWin: layout[
   across
   origin 0x0
   space 0x5
   at 5x5 name: field app_name
   button 70 "Connect" [connect process]
   button 45 "Quit" [Logout]
   at 5x30 console: info 300x100 wrap
   sl: slider 16x100 [scroll-para console sl]
   at 5x140 messages: area 300x200
   
   at 5x350 talk: field 200x48 "Hello world"
   key #"^M" [Send_Message]
   at 205x350 button "Send Message" [Send_Message]
   at 205x375 button "Test" [if cnx [insert udp-out join name/text [": " face/text " button was pressed" ]]]
]
View center-face MainWin

Login required to Post.


Powered by RebelBB and REBOL 2.7.8.4.2