October 28, 2021, 11:47:27

Author Topic: [bb] UDP Communications by Wayne [ 1+ years ago ]  (Read 1558 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] UDP Communications by Wayne [ 1+ years ago ]
« on: June 29, 2017, 00:28:43 »
Title : UDP Communications
Author : Wayne
Posted : 1+ years ago

Description : Sample UDP Chat program

Code :
Code: BlitzBasic
  1. ;
  2. ; Sample UDP communications program
  3. ;
  4. ; Author: W.Gray  Last updated: 1/2/4
  5. ; Comment: Free code no restrictions, use at your own risk.
  6. ;
  7. ; Instructions:
  8. ; Start program, your IP will be displayed, enter your friends IP addy, type.
  9.  
  10. Print "Sample UDP Communications"
  11.  
  12. Inp_Port=4000 ; Incoming Port
  13. Out_Port=4001 ; Outgoing Port
  14.  
  15.  
  16. ; Display all local user IP's
  17. ;
  18. Print "Local IPs:"
  19. For i=1 To CountHostIPs(GetEnv("localhost"))
  20.     Print DottedIP(HostIP(i))
  21. Next
  22.  
  23. Print "Enter Destination IP#: xxx.xxx.xxx.xxx"
  24. Dest_IP$=Input$(">")
  25.  
  26. ; Convert to integer IP
  27. IP= Int_IP(Dest_IP$)
  28.  
  29. Print
  30. Print "Starting Chat <esc> to quit"
  31.  
  32. ; Allocate ports, get stream id's
  33. udp_rd=CreateUDPStream(Inp_Port) ; Read Stream
  34. udp_wr=CreateUDPStream(Out_Port) ; Write Stream
  35.  
  36. .loop
  37. ; Poll Keyboard for Keypress.
  38. byte1=GetKey()
  39.  
  40. ; transmit each key press.
  41. If byte1>0 Then
  42.         ; If 'Enter' pressed force new line locally, and send it.
  43.         If Byte1=13 Then
  44.         Print
  45.         ; put character in stream buffer.
  46.         WriteByte(udp_wr,byte1)
  47.         ; send all characters in the stream buffer.
  48.         SendUDPMsg udp_wr,IP,Inp_Port
  49.     Else
  50.         ; Show character before we send it.
  51.         Write Chr(Byte1)
  52.         ; put character in stream buffer.
  53.         WriteByte(udp_wr,byte1)
  54.         ; send all characters in the stream buffer.
  55.         SendUDPMsg udp_wr,IP,Inp_Port
  56.     EndIf
  57. EndIf
  58.  
  59. ; Load UDP message into stream
  60. IP_rd=RecvUDPMsg(udp_rd)
  61.  
  62. ; Test for messages in stream
  63. If IP_rd <> 0 Then    
  64.  
  65. ;Get number of bytes in UDP stream
  66.     Buflen=ReadAvail(udp_rd)
  67.     If Buflen > 0 Then
  68.    
  69. ; Get received character and print to display
  70.         byte1=ReadByte(udp_rd)
  71.        
  72. ; Translate <enter> key    
  73.         If byte1=13 Then
  74.             Print
  75.         Else
  76.             Write Chr$(byte1)
  77.         EndIf
  78.        
  79.     EndIf
  80.    
  81. EndIf
  82.  
  83. ; Look for <escape>
  84. If KeyDown(1) Then
  85.     Goto quit
  86. EndIf
  87.  
  88. Goto loop
  89.  
  90. ; Cleanup and terminate
  91. .quit
  92. CloseUDPStream udp_wr
  93. CloseUDPStream udp_rd
  94.  
  95.  
  96. ;--------------------------------------
  97. ; FUNCTION: INT_IP(IP$)
  98. ;
  99. ; Returns integer value of a IP Address string
  100. ; IP$ Format n.n.n.n
  101.  
  102. Function Int_IP(IP$)
  103. a1=val(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,"."))
  104. a2=val(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,"."))
  105. a3=val(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,"."))
  106. a4=val(IP$)
  107. Return (a1 Shl 24) + (a2 Shl 16) + (a3 Shl 8 ) +a4
  108. End Function
  109.  
  110.  
  111. ;--------------------------------------
  112. ; FUNCTION: VAL(STRING$)
  113. ;
  114. ; Returns integer value of a string
  115.  
  116. Function val(String$)
  117. ac=String$
  118. Return String$
  119. End Function


Comments :


bytecode77(Posted 1+ years ago)

 .


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal