Question regarding UDP network connections

Just starting out? Need help? Post your questions and find answers here.
User avatar
Thumper
User
User
Posts: 97
Joined: Sat Feb 01, 2014 2:16 am
Location: Alabama USA

Re: Question regarding UDP network connections

Post by Thumper »

infratec wrote:Hi,

an other approach: Since the PC is not the 'server', I use only the client stuff.
So you don't need to close the 'connection' and it should also work on XP.

Bernd
Okay, Bernd, I see why my version failed. After studying your idea some more, which I think is very good and would be a tad more efficient, and adding debug code to my program, they reason why it fails is GetClientIP() returns the broadcast address, in my case, 192.168.13.255. I need GetClientIP() to work in order to identify the IP address of the device that responded.

Of course, I still have the problem opening a broadcast connection on the Mac, but I'll make another post for that.

Again, thank you very much for your looking into this.

Dan
I enter the PureBasic world, targeting both Mac and Windows, by way of PowerBasic, Win32API, EZGUI back to CP/M via various BASIC dialects, xBase and other languages as needed.
Side project: http://www.thecomputerarchive.com
infratec
Always Here
Always Here
Posts: 7666
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Question regarding UDP network connections

Post by infratec »

Hi,

to bad ...

GetClientIP() only works after EventClient() which is only for NetworkServerEvent().
It's not possible, at the moment, to get the IP-Address from a NetworkClientEvent().

Maybe Fred will release next a version which updates and adds network features instead of new graphical stuff.

For example:
It is not possible to write a tftp server in native PB.
User avatar
Thumper
User
User
Posts: 97
Joined: Sat Feb 01, 2014 2:16 am
Location: Alabama USA

Re: Question regarding UDP network connections

Post by Thumper »

infratec wrote:Maybe Fred will release next a version which updates and adds network features instead of new graphical stuff.

For example:
It is not possible to write a tftp server in native PB.
You know, I am enjoying PureBasic. Sure, it is different than from the last dialect of Basic that I used and there are pluses and minuses in each each direction, networking has been my only real hindrance.

So, Fred, if you're listening.... "Pretty please, may we have some more networking functionality?" :D

Well, that and smooth line and curve drawing capabilities. (Okay, okay, so I do want it all !)

And thanks again for your help looking into the communications problems.
I enter the PureBasic world, targeting both Mac and Windows, by way of PowerBasic, Win32API, EZGUI back to CP/M via various BASIC dialects, xBase and other languages as needed.
Side project: http://www.thecomputerarchive.com
infratec
Always Here
Always Here
Posts: 7666
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Question regarding UDP network connections

Post by infratec »

An alternative is to use the socket API.
This is more or less crossplatform.

I use it for multithreaded network stuff on Window and Linux.
Since Mac OS X is more or less Unix based it should work there too.

Bernd
User avatar
Thumper
User
User
Posts: 97
Joined: Sat Feb 01, 2014 2:16 am
Location: Alabama USA

Re: Question regarding UDP network connections

Post by Thumper »

infratec wrote:An alternative is to use the socket API.
This is more or less crossplatform.

I use it for multithreaded network stuff on Window and Linux.
Since Mac OS X is more or less Unix based it should work there too.

Bernd
Am I interpreting you correctly in that you suggest that I use compiler directives to use low-level, OS-specific communications?

If I were only concerned with Windows, I can do that, but this Objective-c has be baffled. I've been looking through the Apple docs for the entire evening trying to figure it out. (I only stopped a few minutes ago for a 1 AM kolachi run!)

My goal is simply to write one Mac-specific routine to send out the broadcast packet (passed via pointer and length) to a defined address.

If this is not what you are referring to, could you provide me a link to information on the Socket API for the Mac (edit: or Linux) so that I may study it?
I enter the PureBasic world, targeting both Mac and Windows, by way of PowerBasic, Win32API, EZGUI back to CP/M via various BASIC dialects, xBase and other languages as needed.
Side project: http://www.thecomputerarchive.com
infratec
Always Here
Always Here
Posts: 7666
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Question regarding UDP network connections

Post by infratec »

Hi,

an example for UDP socket with broadcast for windows and linux (maybe mac):

Code: Select all

;
; PIC Ethernet Discover
;

; | 

#Version = "1.00"

EnableExplicit


CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Linux
    #AF_INET = 2
    #SOCK_DGRAM = 2
    #SO_BROADCAST = 6
    #SOL_SOCKET = 1
    #INADDR_ANY = 0
    #INADDR_BROADCAST = $FFFFFFFF
    
    #IPPROTO_UDP = 17
    
    #FIONBIO = $5421
    
    #INVALID_SOCKET = -1
    #SOCKET_ERROR = -1
    
    
    Structure sockaddr_in Align #PB_Structure_AlignC
      sin_family.u
      sin_port.u
      sin_addr.l
      sin_zero.a[8]
    EndStructure
    
  CompilerCase #PB_OS_MacOS
    #AF_INET = 2
    #SOCK_DGRAM = 2
    #SO_BROADCAST = $20
    #SOL_SOCKET = $FFFF
    #INADDR_ANY = 0
    #INADDR_BROADCAST = $FFFFFFFF
    
    #IPPROTO_UDP = 17
    
    #INVALID_SOCKET = -1
    #SOCKET_ERROR = -1
    
    
    Structure sockaddr_in Align #PB_Structure_AlignC
      sin_family.u
      sin_port.u
      sin_addr.l
      sin_zero.a[8]
    EndStructure
CompilerEndSelect


Define service.sockaddr_in
Define RxAddr.sockaddr_in
Define RxAddrLen.i
Define TxAddr.sockaddr_in


Define Socket.i
Define argp.l
Define *Buffer

Define Querry$, Receive$, Found$, Name$, MAC$, IP$

Define.i Starttime, Avail, First, Exit, Event, Found, i

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  Define wsaData.WSADATA
  ; initialize the WindowsSocketAPI
  If WSAStartup_($101, @wsaData) <> #NO_ERROR
    End 2
  EndIf
CompilerEndIf

; open an UDP socket
Socket = SOCKET_(#AF_INET, #SOCK_DGRAM, #IPPROTO_UDP)
If Socket = #INVALID_SOCKET
  End 3
EndIf


; set it to none blocking
argp = 1
CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Linux
    ioctl_(Socket, #FIONBIO, @argp)
  CompilerCase #PB_OS_MacOS
    #F_SETFL = 4
    #O_NONBLOCK = 4
    fcntl_(Socket, #F_SETFL, #O_NONBLOCK)
  CompilerCase #PB_OS_Windows
    ioctlsocket_(Socket, #FIONBIO, @argp)
CompilerEndSelect

; allow broadcasts on that socket
argp = $FFFFFFFF
setsockopt_(Socket, #SOL_SOCKET, #SO_BROADCAST, @argp, 4)


; bind the socket to port 30303 and listen to all incoming addresses
service\sin_family = #AF_INET
service\sin_addr = #INADDR_ANY
service\sin_port = htons_(30303)

If bind_(Socket, @service, SizeOf(sockaddr_in)) <> #SOCKET_ERROR
  
  *Buffer = AllocateMemory(1536)
  If *Buffer
    
    ; needed for recvfrom()
    RxAddrLen = SizeOf(sockaddr_in)
    
    Querry$ = "Discovery: Who is out there?"
    
    RxAddr\sin_family = #AF_INET
    RxAddr\sin_port = htons_(30303)
    RxAddr\sin_addr = #INADDR_ANY
    
    TxAddr\sin_family = #AF_INET
    TxAddr\sin_port = htons_(30303)
    TxAddr\sin_addr = #INADDR_BROADCAST
    
    
    OpenWindow(0, 0, 0, 400, 200, "Ethernet Discover V" + #Version + "   (c) 2012 Infratec AG", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
    
    ListIconGadget(0, 10, 10, 380, 180, "Name", 100, #PB_ListIcon_FullRowSelect)
    AddGadgetColumn(0, 1, "MAC", 110)
    AddGadgetColumn(0, 2, "Address", 100)
    
    SetActiveGadget(0)
    
    AddWindowTimer(0, 0, 100)
    
    First = #True
    Exit = #False
    Repeat
      Event = WaitWindowEvent(10)
      Select Event
        Case #PB_Event_Timer
          If EventTimer() = 0
            If First
              RemoveWindowTimer(0, 0)
              AddWindowTimer(0, 0, 5000)
              First = #False
            EndIf
            sendto_(Socket, @Querry$, Len(Querry$), 0, @TxAddr, SizeOf(sockaddr_in))
          EndIf
        Case #PB_Event_Gadget
          If EventType() = #PB_EventType_LeftDoubleClick
            IP$ = GetGadgetItemText(0, GetGadgetState(0), 2)
            RunProgram("http://" + IP$)
          EndIf
        Case #PB_Event_CloseWindow : Exit = #True
      EndSelect
      
      Avail = recvfrom_(Socket, *Buffer, MemorySize(*Buffer), #Null, @RxAddr, @RxAddrLen)
      If Avail > 0
        Receive$ = PeekS(*Buffer, Avail)
        If Receive$ <> Querry$
          Receive$ = ReplaceString(Receive$, #CRLF$, #LF$)
          Name$ = StringField(Receive$, 1, #LF$)
          MAC$ = StringField(Receive$, 2, #LF$)
          ;Info$ = StringField(Receive$, 3, #LF$)
          IP$ = IPString(RxAddr\sin_addr)
          
          Found = #False  
          For i = 0 To CountGadgetItems(0) - 1
            If GetGadgetItemText(0, i, 1) = MAC$
              Found = #True
              Break
            EndIf
          Next i
          
          If Found
            SetGadgetItemText(0, i, Name$, 0)
            SetGadgetItemText(0, i, IP$, 2)
          Else
            AddGadgetItem(0, -1, Name$ + #LF$ + MAC$ + #LF$ + IP$)
          EndIf
          
        EndIf
      EndIf
      
    Until Exit

    FreeMemory(*Buffer)
    
  EndIf
EndIf


CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
  close_(Socket)
CompilerElse
  closesocket_(Socket)

  WSACleanup_()
CompilerEndIf
It discovers MicroChip ethernet PIC CPUs when the TCP/IP stack is running.

Bernd
Last edited by infratec on Fri Aug 15, 2014 8:43 am, edited 2 times in total.
User avatar
Thumper
User
User
Posts: 97
Joined: Sat Feb 01, 2014 2:16 am
Location: Alabama USA

Re: Question regarding UDP network connections

Post by Thumper »

infratec wrote:Hi,

an example for UDP socket with broadcast for windows and linux (maybe mac):

Code: Select all

;
; PIC Ethernet Discover
;
It discovers MicroChip ethernet PIC CPUs when the TCP/IP stack is running.

Bernd
Oh, that is perfect. From that I did some searching and that is Winsock and just never realized that it was a cross platform standard. So if that is implement the same on Mac, that is perfect! Then I discovered something...

Fred, if you are reading this, are you using these same routines and if so, in setsocketopt for UDP communications, are you including SO_BROADCAST which is described as "Permits sending of broadcast messages, if this is supported by the protocol. This option takes an int value. This is a Boolean option."

Thank you very much!

Now I'm off to implement this in the UDP sender... Prayerfully, I'll be back with some good news!
I enter the PureBasic world, targeting both Mac and Windows, by way of PowerBasic, Win32API, EZGUI back to CP/M via various BASIC dialects, xBase and other languages as needed.
Side project: http://www.thecomputerarchive.com
User avatar
Thumper
User
User
Posts: 97
Joined: Sat Feb 01, 2014 2:16 am
Location: Alabama USA

Re: Question regarding UDP network connections

Post by Thumper »

infratec wrote:
It discovers MicroChip ethernet PIC CPUs when the TCP/IP stack is running.

Bernd
Well, I took your program as an outline and put it into my program and experienced the exact same set of parameters:

Windows 8.1 worked perfectly both direct and broadcast
Mac 10.7 worked directly but not for broadcast.

I pulled out some old Winsock code from decades past and can't see anything different there.

Why does the Mac have to be so difficult about things? I bet in the past week I have spent over 100 hours trying to get the broadcast to work.

I have been searching the 'Net but when you type "Mac", most of the references are for "MAC addresses" but I can't filter out "address" because there could easily be references for "IP address". I have limited my searches to the Apple developers forums and really haven't found much that's different.

I just thought I'd post this update to you. Meanwhile I'm getting back to getsockopt_ because it is returning a failure trying to get the result of the SO_Broadcast value. setsockopt_ works. Well, it doesn't return an error. Maybe if I can get the getsockopt_ to work to validate the SO_Broadcast value is truly getting set. (I'm wondering if Windows has this set by default.)

I'll keep you posted and thanks for hanging in there with me.

Dan
I enter the PureBasic world, targeting both Mac and Windows, by way of PowerBasic, Win32API, EZGUI back to CP/M via various BASIC dialects, xBase and other languages as needed.
Side project: http://www.thecomputerarchive.com
deesko
User
User
Posts: 39
Joined: Fri Sep 21, 2012 11:40 pm
Location: Portugal

Re: Question regarding UDP network connections

Post by deesko »

Thumper wrote:I have been searching the 'Net but when you type "Mac", most of the references are for "MAC addresses" but I can't filter out "address" because there could easily be references for "IP address".
Well, if you're using Google to perform the search you can limit the findings if you add -"ip address" along with the rest, as:

Code: Select all

 mac -"ip address" 
User avatar
Thumper
User
User
Posts: 97
Joined: Sat Feb 01, 2014 2:16 am
Location: Alabama USA

Re: Question regarding UDP network connections

Post by Thumper »

deesko wrote:
Thumper wrote:I have been searching the 'Net but when you type "Mac", most of the references are for "MAC addresses" but I can't filter out "address" because there could easily be references for "IP address".
Well, if you're using Google to perform the search you can limit the findings if you add -"ip address" along with the rest, as:

Code: Select all

 mac -"ip address" 
Limit is a funny word when compared to the whole Internet thing. hehe I'm still getting more results than McDonald's has sold hamburgers!

However I think I'm on to something here. On the PC, I'm getting a 10014 error enabling the Broadcast using a function that is not available on the Mac, WSAGetLastError() or GetLastError(), that indicates a Bad Address. I just changed my calling around and now I'm not getting the error on the PC. Still am on the Mac.

Hmm, I'm using the x86 version on the Mac. Maybe I need to install the 64-bit version. Now how do I do that.... (thinking mode)

Continuing to work here. Thanks for your thought, Deesko!
I enter the PureBasic world, targeting both Mac and Windows, by way of PowerBasic, Win32API, EZGUI back to CP/M via various BASIC dialects, xBase and other languages as needed.
Side project: http://www.thecomputerarchive.com
infratec
Always Here
Always Here
Posts: 7666
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Question regarding UDP network connections

Post by infratec »

Hi,

I investigated the Mac OS X.
Some defines are different, and also the nonblocking stuff is different.
But now my program works on Mac OS X 10.9.
(Also the broadcast :mrgreen: )

I changed the code above.

Bernd
User avatar
Thumper
User
User
Posts: 97
Joined: Sat Feb 01, 2014 2:16 am
Location: Alabama USA

Re: Question regarding UDP network connections

Post by Thumper »

infratec wrote:Hi,

I investigated the Mac OS X.
Some defines are different, and also the nonblocking stuff is different.
But now my program works on Mac OS X 10.9.
(Also the broadcast :mrgreen: )

I changed the code above.

Bernd
I just wanted to drop a note to thank you for your efforts as I could not have made it this far in a timely fashion without your help.

As of this posting, most everything is working on both platforms but it wasn't fun. Much was just trial and error learning a new platform, language and how the equipment interacts.

Dan
I enter the PureBasic world, targeting both Mac and Windows, by way of PowerBasic, Win32API, EZGUI back to CP/M via various BASIC dialects, xBase and other languages as needed.
Side project: http://www.thecomputerarchive.com
Post Reply