Author Topic: SOCK_RECVIP - Possible?  (Read 6328 times)

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1750
    • View Profile
    • Homepage
Re: SOCK_RECVIP - Possible?
« Reply #15 on: 2011-Apr-02 »
The date and version is old but I just tested it and it works :-)

Set it up on all computers you want to test, aim at the same IP. This IP will recieve all packets and sent to one of the other clients (the IP you set on that client).

Code: GLBasic [Select]
// --------------------------------- //
// Project: NetUDPThruRouter
// Start: Tuesday, September 15, 2009
// IDE Version: 7.104

// Works if you send a message from inside the firewall to the outside IP of the other client
// And then send a message from that client on the outside of the firewall to the inside.
// Both clients can be behind a firewall.


GLOBAL sock%               // The socket connection
GLOBAL port% = 15000       // Port number to use

LOCAL ok%, ip%, rv%, msg$
LOCAL myip$
LOCAL destip$
LOCAL timeout

myip$ = NETGETIP$()        // Get our local IP just for the show
log("Our IP: "+ myip$)

STDOUT "\nDestination IP: "
destip$ = STDIN$()
// Just for less typing when testing
IF destip$ = "" THEN destip$ = "0.0.0.0"                // <---- Fill in default IP here

ok = SOCK_INIT()    // Init network
IF ok
    sock = SOCK_UDPOPEN(port)    // Get the socket so we can send/recieve on the port
    IF sock <> -1
        ip = SOCK_GETIP(destip$) // Convert the string to an integer IP
        // Send 32 packets, waiting one second for an answer between each send
        FOR n = 0 TO 31
            log("Sending message: "+"Hello from "+myip$+" To: "+destip$)
            ok = SOCK_UDPSEND(sock, "Hello from "+myip$, ip, port)
            IF ok <> -1
                log("message sent with "+ok+" bytes")
                timeout = GETTIMERALL() + 1000      // Start timer for one second
                WHILE rv <> -1 AND GETTIMERALL() < timeout
                    rv = SOCK_RECV(sock, msg$, 999)
                    IF rv>0 THEN log("message recieved: ["+msg$+"]")
                    IF rv=-1
                        log(NETGETLASTERROR$())     // Didn't work, tell user why
                        SOCK_CLOSE(sock)
                    ENDIF
                WEND
            ELSE
                log(NETGETLASTERROR$())
            ENDIF
        NEXT
    ELSE
        log(NETGETLASTERROR$())
    ENDIF
ELSE
    log(NETGETLASTERROR$())
ENDIF
KEYWAIT



FUNCTION log: str$
    STDOUT "\n"+str$
ENDFUNCTION
« Last Edit: 2011-Apr-02 by Moru »