Very simple UDP example with WinSock

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Very simple UDP example with WinSock

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by altesocke.

hi forum,

some of you requested UDP for purebasic.
here is a very simple, rudimentary client server example.
the client sends an string to the server.
if you need UDP you might expand it for sending files.

start the server first then the client, both with debugger (there is no window in the example only debugger output).

here is my code:

Code: Select all

;UDP Client Example

*ptr = sockinfo.sockaddr_in

sockinfo\sin_family = #AF_INET
sockinfo\sin_port = htons_(1234)  ;The htons_() function converts a u_short from host to TCP/IP network byte order
sockinfo\sin_addr = inet_addr_("127.0.0.1")  ;The inet_addr_() function converts a string containing an (Ipv4)
; Internet Protocol dotted address into a proper address for the IN_ADDR structure.

If InitNetwork()
   SockIdent = SOCKET_(#AF_INET, #SOCK_DGRAM, 0)  ;create socket (#SOCK_DGRAM for UDP)
   If SockIdent = #INVALID_SOCKET
      Debug "Socket cant be created!"
   Else
      Debug "UDP socket created!"
      string$ = "hello server"  ; string to send
      *stringbuffer = AllocateMemory(512)
      PokeS(*stringbuffer, string$)   ; poke string into memory
      sendto_(SockIdent, *stringbuffer, 512, 0, *ptr, SizeOf(sockaddr_in))   ; send the string to the server specified
;in the sockaddr_in structure
      Debug "string sent!"
   EndIf    
Else
   Debug "WinSock cant be initialized!"
   res = WSAGetLastError_() 
   Debug "ERROR: "+Str(res)   
   WSASetLastError_(0)
EndIf
End 

; end UPD client
----------------------------------------------------------------

Code: Select all

;UDP Server Example

*ptr = sockinfo.sockaddr_in
sockinfo\sin_family = #AF_INET
sockinfo\sin_port = htons_(1234) ;The htons function converts a u_short from host to TCP/IP network byte order
*ptr2 = sockinfo\sin_addr  ; leave that blank it would be filled with the client IP from the recvfrom_() function

If InitNetwork()
  SockIdent = SOCKET_(#AF_INET, #SOCK_DGRAM, 0) ;create socket (#SOCK_DGRAM for UDP)
  If SockIdent = #INVALID_SOCKET
    Debug "Socket cant be created!"
  Else 
    Debug "UDP socket created!"
    res = bind_(SockIdent, *ptr, SizeOf(sockaddr_in))  ; bind the socket to port 1234 specified in sockinfo\sin_port
    If res = 0
      Debug "bind to port 1234 - OKAY"	
      *buf = AllocateMemory(512)  ; buffer for recvfrom_()
      Debug "recvfrom - blocking call"
      res = recvfrom_(SockIdent, *buf, 512, 0, *ptr2, 0)   ;recvfrom_() receives data from the client
      ; this is a blocking call your prog blocks until it receives data from a client, use it in a thread			
      If res <> #SOCKET_ERROR
        Debug Str(res)+" bytes received"
        string$ = PeekS(*buf)    ; peeks the received data from memory
        Debug "received: "+string$
      Else
        Debug "SOCKET_ERROR"
        res = WSAGetLastError_()
        Debug "ERROR: "+Str(res)	
        WSASetLastError_(0)	
      EndIf 
    ElseIf res = #SOCKET_ERROR
      Debug "bind to port 12345 - FAILED"	
      res = WSAGetLastError_()
      Debug "ERROR: "+Str(res)	
      WSASetLastError_(0)			
    EndIf 
  EndIf 	
Else 
  Debug "WinSock cant be initialized!"
  res = WSAGetLastError_()
  Debug "ERROR: "+Str(res)	
  WSASetLastError_(0)
EndIf 
End  

;End UDP Server Example

cu socke
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by TheBeck.

Wow, very nice! Thanks for the contribution.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

WOW! Fine! Nice Example!! Thanks you very much!

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
System: Windows 2000 Server, 512 MB Ram, GeForce4200 TI 128 MB DDR, Hercules Theater 6.1 DTS Sound
System 2: Mobile Pentium 4 2.4GHz 512 MB DDR GeForce4 420-32, Windows XP Home
Post Reply