; ; UDP receive code ; ; This receives data on its socket (which is bound to a port ; on localhost), from a host that connects to it like TCP ; and receives data with recv_() instead of recvfrom_(). ; ; Written by Anton Reinauer . ; ; Thanks to Paul Burkey for TCP_Funcs and to Dr. Ercole Spiteri ; for TCP-to-Blitz. ; WBStartup NoCli Hostname.s="localhost" ; you need to bind to localhost #PORT=3001 ; port to bind to INCLUDE "TCPFuncs.bb" DEFTYPE .w ;******************************************************** .ReadUDP2 Function .s ReadUDP2{} SHARED sock.l,TCPmem.l ; ; This Function reads data from a connection made from another program ; If there is no messages then it will return an empty string ="" ; sockread.l=0 ;Clear Readmask sockread.l BitSet sock.l ;Set Readmask on our socket e=IoctlSocket_(sock.l,#FIONREAD,TCPmem.l) ;How much data is there? f.l=Peek.l(TCPmem.l) ;Place value in f If f>0 c=recv_(sock.l,TCPmem.l,f,0) ; read data from the socket ; which is connected to another host as in TCP If c>0 a=0 : c$="" Repeat c$=c$+Chr$(Peek.b(TCPmem+a)) : a+1 ; build string Until a=c EndIf EndIf Function Return c$ End Function .ConnectUDP2 Function ConnectUDP2{host$,port.w} SHARED sock.l,host.sockaddrin,hostlen.w sock.l=socket_(2,2,0) ; create UDP 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 host.sockaddrin\sin_family=2 ;Set type to AT_INET hostlen=SizeOf.sockaddrin ;Get lenght of structure sockaddrin If bind_(sock,host,hostlen)=0 ; bind socket to local port number Function Return True ; so we can receive data on that port EndIf End Function ;**************************************** WbToScreen0 Window 0,300,100,320,150,$1|$2|$4|$8|$400,"Receive UDP2",1,2 WindowOutput0 WindowInput 0 If ConnectUDP2 {Hostname,#PORT} ; open socket and bind it to a port number NPrint "Bound to ",Hostname," ",#PORT Else Print "Can't bind to host!" Goto Exit: EndIf ypos=10 ;**************************************** .Main Repeat Delay_(1) a$=ReadUDP2{} If a$<>"" t$=a$ Gosub Print_String EndIf ev.l=Event Until ev=$200 ; shut down if close gadget hit CloseTCP{} ; close connection WLocate 10,10 NPrint"Connection closed" Exit: ; NPrint Errno_ ; print error number if needed FreeMem TCPmem.l,$2000 Delay_(50) End ;**************************************** .Print_String ypos+10 If ypos=130 Then ypos=20 WLocate 10,ypos Print " " WLocate 10,ypos Print "RECEIVED: ",t$ ; print string received Return