;                      UDP Send code
;
;    This code connects to a port like TCP, and then sends
;  each string with send_() instead of sendto_()
;
;  Written by Anton Reinauer <anton@ww.co.nz.
;
;  Thanks to Paul Burkey for TCP_Funcs and to Dr. Ercole Spiteri
;  for TCP-to-Blitz.
;
;  Turn overflow errors off if running on a 68000


WBStartup
NoCli
Hostname.s="localhost"    ; host to connect to
#PORT=3001                ; port to connect to

INCLUDE "TCPFuncs.bb"
DEFTYPE .w

;********************************************************

.WriteUDP2
Statement WriteUDP2{ad.l,size.w}
  SHARED sock.l

  ; This routine writes data via UDP

  sockwrite.l=0                         ;Clear Writemask
  sockwrite.l BitSet sock.l             ;set Writemask on our socket
  g=WaitSelect_(2,0,&sockwrite.l,0,0,0) ;Wait until server is ready to read our data

  c=send_(sock.l,ad,size,0)  ; send data to connected socket
End Statement

.ConnectUDP2
Function  ConnectUDP2{host$,port.w}
  SHARED sock.l,host.sockaddrin,hostlen2.l
  ;
  ; Connect to host at specified port
  ; Return true or False if Connection is made
  ;

  sock.l=socket_(2,2,0)      ; open socket

  *a.hostent=gethostbyname_(host$)

  ;Copy Details to our Sockaddrin structure

  bb=CopyMem_(*a.hostent\h_addr_list\ItemA,&host.sockaddrin\sin_addr,*a.hostent\h_length)

  host.sockaddrin\sin_port=port    ;Set port number to connect to
  host.sockaddrin\sin_family=2     ;Set type to AT_INET
  hostlen2.l=SizeOf.sockaddrin     ;Get lenght of structure sockaddrin

  If connect_(sock.l,host.sockaddrin,hostlen2.l)=-1   ; connect to port on other host like TCP
     CloseSocket_(sock.l)
     NPrint "Not Connected to ",Host$," ",#PORT
     Function Return False
  Else
     NPrint "   Connected to ",Host$," ",#PORT
     Function Return True
  EndIf

End Function


;****************************************

WbToScreen0
Window 0,0,20,300,220,$1|$2|$4|$8|$400,"UDP Send2",1,2

WindowOutput0
WindowInput 0

If ConnectUDP2 {Hostname,#PORT} =True  ; connect to host
   NPrint "    Type in data, and hit"
   NPrint "      return to send"
Else
  Print "Can't connect to host!"
  Goto Exit:
EndIf

ypos=40
xpos=10

;****************************************

.Main

 Repeat
    Delay_(1)
    b$=Inkey$
    If b$<>""
       If b$=Chr$(13)       ;send string on return key
         WLocate 10,30
         Print "                                       "
         xpos=10

         t$="SENT: " + send$
         Gosub Print_String

         WriteUDP2{&send$,Len(send$)}    ; send string
         send$=""
       Else                 ; build string to send
         send$=send$+b$
         WLocate xpos,30
         xpos+8
         Print b$          ; echo key press
       EndIf
    EndIf

    ev.l=Event

Until ev=$200    ; shut down if close gadget hit

CloseTCP{}                       ; close connection

WLocate 10,30
Print "Connection  closed"

Exit:
FreeMem TCPmem.l,$2000
Delay_(10)

End

;****************************************

.Print_String
      ypos+10
      If ypos=200 Then ypos=80
      WLocate 10,ypos
      Print "                                             "
      WLocate 10,ypos
      Print t$               ; print string received
Return

