UDP Peer to Peer on a single PC

Share your advanced PureBasic knowledge/code with the community.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

UDP Peer to Peer on a single PC

Post by pdwyer »

Disclaimer: Dare made me do it, if it doesn't work blame him! :twisted:

This is a simple (not thread or anything else safe) UDP peer (or in PB terms UDP Client/Server). Its port usage is a little odd as it's designed to run on the one PC. Compile it to exe then run it twice. It should run once listening on 7001 with a partner of 7002 and then the second time you run it it will be the other way around.

if you want to use your broadcast address ( http://en.wikipedia.org/wiki/Broadcast_address ) then you will need more than one PC so you can listen on the same port on different IP's then just change the IP below from 127.0.0.1 to your broadcast address.

Code: Select all


If InitNetwork() = 0
    MessageRequester("Error", "Can't initialize the network !", 0)
    End
EndIf

Declare UDPListen(Dummy.l)

Port1 = 7001
Port2 = 7002

CID = CreateNetworkServer(#PB_Any, Port1, #PB_Network_UDP)

If cid = 0
    CID = CreateNetworkServer(#PB_Any, Port2, #PB_Network_UDP)
    MyPort = Port2
    PartnerPort = Port1
Else
    MyPort = Port1
    PartnerPort = Port2
EndIf

If CId = 0
    MessageRequester("Error", "Ports in use", 0)
    End
EndIf

;=============================================================

OpenConsole()

    PrintN("Listening on: " + Str(MyPort))
    CreateThread(@UDPListen(),0)
    
    While 1
        PrintN("Type Data to send: ")
        SendData.s = Input()
        
        ConnectionID = OpenNetworkConnection("127.0.0.1", PartnerPort, #PB_Network_UDP)
        SendNetworkString(ConnectionID, SendData)
        CloseNetworkConnection(ConnectionID)
    Wend

CloseConsole()

;=============================================================

Procedure UDPListen(Dummy.l)

    bufferlen = 1024

    While 1
        EventType = NetworkServerEvent()
        If EventType = #PB_NetworkEvent_Data
            *DataBuffer = AllocateMemory(bufferlen)  
            Result = ReceiveNetworkData(EventClient(), *DataBuffer, bufferlen) 
            PrintN("Data From: " + IPString(GetClientIP(EventClient())))
            ;Debug "bytes " + Str(result)
            PrintN("Data: " + PeekS(*DataBuffer))
            PrintN("")
            PrintN("Type Data to send: ")
            FreeMemory(*DataBuffer)
        EndIf
        
        Delay(50)
    Wend

EndProcedure
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Well, it works here! :D

Very nice and thank you.
Dare2 cut down to size
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Damn, I was hoping it wouldn't work so I could blame dare and give him a good kicking!

Oh well!

:twisted:

Thanks for sharing.
I may look like a mule, but I'm not a complete ass.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Here is a version for just a normal UDP chat example, you will need two systems to use it. the IP is hardcoded but you could change that in an input() command. If systems are on the same lan you can use the broadcast address and you will also see what you send come back

swap "**IP_HERE**" with the target IP address

Code: Select all



If InitNetwork() = 0
    MessageRequester("Error", "Can't initialize the network !", 0)
    End
EndIf

Declare UDPListen(Dummy.l)

Port = 7000


CID = CreateNetworkServer(#PB_Any, Port, #PB_Network_UDP)

If CId = 0
    MessageRequester("Error", "Ports in use", 0)
    End
EndIf

;=============================================================

OpenConsole()

    PrintN("Listening on: " + Str(port))
    CreateThread(@UDPListen(),0)
    
    While 1
        PrintN("Type Data to send: ")
        SendData.s = Input()
        
        ConnectionID = OpenNetworkConnection("**IP_HERE**", port, #PB_Network_UDP)
        SendNetworkString(ConnectionID, SendData)
        CloseNetworkConnection(ConnectionID)
    Wend

CloseConsole()

;=============================================================

Procedure UDPListen(Dummy.l)

    bufferlen = 1024

    While 1
        EventType = NetworkServerEvent()
        If EventType = #PB_NetworkEvent_Data
            *DataBuffer = AllocateMemory(bufferlen)  
            Result = ReceiveNetworkData(EventClient(), *DataBuffer, bufferlen) 
            PrintN("Data From: " + IPString(GetClientIP(EventClient())))
            ;Debug "bytes " + Str(result)
            PrintN("Data: " + PeekS(*DataBuffer))
            PrintN("")
            PrintN("Type Data to send: ")
            FreeMemory(*DataBuffer)
        EndIf
        
        Delay(50)
    Wend

EndProcedure

Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Pantcho!!
Enthusiast
Enthusiast
Posts: 538
Joined: Tue Feb 24, 2004 3:43 am
Location: Israel
Contact:

Post by Pantcho!! »

Thanks Paul, UDP programming is rare in the forum, very nice simple examples.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Thanks for it, pdwyer
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply