echoserver_udp.monkey2 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #rem
  2. Quick guide to writing UDP client/server apps:
  3. * Server:
  4. 1) Create server socket using Socket.Bind (not Listen!).
  5. 2) Wait for client messages using Socket.ReceiveFrom.
  6. 3) Use the SocketAddress filled in by ReceiveFrom to determine the client the message is from.
  7. 4) Use the same SocketAddress with Socket.SendTo to reply to the client.
  8. * Client:
  9. 1) Connect to server using Socket.Connect with SocketType.Stream as the last parameter.
  10. 2) Communicate with the server using Socket.Send and Socket.Receive.
  11. #end
  12. #Import "<mojox>"
  13. #Import "<mojo>"
  14. #Import "<std>"
  15. Using mojox..
  16. Using mojo..
  17. Using std..
  18. Const HOST:="localhost" 'Note: Use "" for 'public' host.
  19. Const PORT:=40123
  20. Class MyWindow Extends Window
  21. Method New()
  22. New Fiber( Server )
  23. For Local i:=0 Until 5
  24. New Fiber( Client )
  25. Next
  26. End
  27. Method Server()
  28. Local server:=Socket.Bind( PORT )
  29. If Not server print "Server: Failed to create server" ; Return
  30. Print "Server @"+server.Address+" ready"
  31. server.SetOption( "SO_REUSEADDR",1 )
  32. 'To keep track of connected clients...
  33. Local clients:=New Map<SocketAddress,Int>
  34. Local addr:=New SocketAddress
  35. Repeat
  36. Local data:Int
  37. If server.ReceiveFrom( Varptr data,4,addr )<>4 Exit
  38. Print "Server received msg:"+data+" from client @"+addr
  39. 'check if client exists
  40. If Not clients[addr]
  41. Local id:=clients.Count()+1
  42. clients[ New SocketAddress( addr ) ]=id
  43. Print "New Client! id="+id
  44. Endif
  45. data=-data
  46. server.SendTo( Varptr data,4,addr )
  47. Forever
  48. server.Close()
  49. End
  50. Method Client()
  51. Global _id:Int
  52. _id+=1
  53. Local id:=_id
  54. Fiber.Sleep( .5 ) 'wait a bit for server to start
  55. Local client:=Socket.Connect( HOST,PORT,SocketType.Datagram )
  56. If Not client Print "Client("+id+"): Couldn't connect to server" ; Return
  57. Print "Client("+id+") @"+client.Address+" connected to @"+client.PeerAddress
  58. Local address:=New SocketAddress
  59. For Local i:=0 Until 10
  60. Fiber.Sleep( Rnd( .2,.4 ) )
  61. Local data:Int=i*10
  62. client.Send( Varptr data,4 )
  63. If client.Receive( Varptr data,4 )<>4 Exit
  64. Print "Client("+id+") received reply:"+data+" from server"
  65. Next
  66. Print "Client("+id+") finished!"
  67. End
  68. Method OnRender( canvas:Canvas ) Override
  69. ' App.RequestRender()
  70. Global ticks:=0
  71. ticks+=1
  72. canvas.DrawText( ticks,0,0 )
  73. End
  74. End
  75. Function Main()
  76. New AppInstance
  77. New MyWindow
  78. App.Run()
  79. End