Sourcecode


Structure ip_mreq
    ipm_multiaddr.l
    ipm_interface.l
EndStructure

If InitNetwork() : EndIf
#StartPort = 1234
S1.l = CreateNetworkServer(0, #StartPort, #PB_Network_UDP) ;S1 is the returned Socket-Handle...
ipmreq.ip_mreq
ipmreq\ipm_multiaddr = inet_addr_("224.0.0.1")
ipmreq\ipm_interface = 0
setsockopt_(S1, 0, 5, ipmreq, SizeOf(ipmreq))

Dim C.l(1)
If S1 = 0
   MessageRequester("Error", "At least one Server couldn't get started!")
   End
EndIf
OpenWindow(0, 250, 250, 300, 135, "Multicast-Test-Chat", #PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))
ListIconGadget(0, 5, 5, 290, 100, "Server#", 80)
AddGadgetColumn(0, 1, "Message", 206)
   TextGadget(1, 5, 30 + 82, 40, 20, "Chat")
   StringGadget(2, 47, 30 + 80, 140, 20, "")
   ButtonGadget(3, 192, 30 + 80, 40, 20, "Send")
   ButtonGadget(4, 237, 30 + 80, 60, 20, "Connect")
   DisableGadget(2, 1)
   DisableGadget(3, 1)
Repeat
Buffer.s = Space(100)
   Select WindowEvent()
      Case 0
         Select NetworkServerEvent()
            Case 0
               Delay(5)
            Case #PB_NetworkEvent_Connect
               AddGadgetItem(0, 0, Str(EventServer()) + #LF$ + "New Client: " + Str(EventClient()))
            Case #PB_NetworkEvent_Disconnect
               AddGadgetItem(0, 0, Str(EventServer()) + #LF$ + "Client quit: " + Str(EventClient()))
            Case #PB_NetworkEvent_Data
               S = ReceiveNetworkData(EventClient(), @Buffer, Len(Buffer))
               AddGadgetItem(0, 0, Str(EventServer()) + #LF$ + "Text: " + Left(Buffer, S))
         EndSelect
      Case #PB_Event_Gadget
         Gadget.l = EventGadget()
         Select Gadget
            Case 3
               a$ = GetGadgetText(2)
               *G.LONG = @C
               If a$ And *G\l
                  SendNetworkString(*G\l, a$)
                  SetGadgetText(2, "")
               EndIf
            Case 4
               *G.LONG = @C
               If *G\l
                  CloseNetworkConnection(*G\l)
                  SetGadgetText(Gadget, "Connect")
                  DisableGadget(2, 1)
                  DisableGadget(3, 1)
                  *G\l = 0
               Else
                  *G\l = OpenNetworkConnection("224.0.0.1", #StartPort, #PB_Network_UDP)
                  If *G\l
                     SetGadgetText(Gadget, "Disconnect")
                     DisableGadget(2, 0)
                     DisableGadget(3, 0)
                  EndIf
               EndIf
         EndSelect
      Case #PB_Event_CloseWindow
         Break
   EndSelect
ForEver
CloseNetworkServer(0)
MessageRequester("Information", "All Servers have been closed...")