Feature request > Network
\0 terminated SOCK_RECV command
Kitty Hello:
It acts as expected. You send data and the other side can read data.
So, if you want to send a string and do _not_ close the connection but want to write another string, you must append a string length header. That's what I do with the NET commands.
Here's what you want:
--- Code: (glbasic) ---// --------------------------------- //
// Project: _moo
// Start: Friday, January 22, 2010
// IDE Version: 7.242
// SETCURRENTDIR("Media") // seperate media and binaries?
GLOBAL Net_ServerIP$
GLOBAL EstablishedConnection%
GLOBAL Net_SocketNumber%
Net_SocketNumber=1234
Net_ServerIP$="127.0.0.1"
SOCK_INIT()
// MAIN
//-------------------------------------------------------------------------
Done=0
STDOUT "Press 1 to be server\n"
STDOUT "Press 2 to be client\n"
LOCAL Result$=INKEY$()
IF Result$ = "1" // pressed 1
STDOUT "Server\n"
GOSUB server
ELSEIF Result$= "2" // pressed 2
STDOUT "Client\n"
GOSUB Client
ENDIF
FUNCTION SendString%: sock%, str$
// pack the length as a 10 byte string
SOCK_TCPSEND(sock%, FORMAT$(10,0,LEN(str$)) + str$)
ENDFUNCTION
FUNCTION ReceiveString$: sock%
LOCAL size%, str$, ok%
ok% = SOCK_RECV(sock%, str$, 10) // length package
IF ok% <= 0 THEN RETURN "" // nothing there
// now we must read the string - dead or alive, you're sending to me
size% = str$
// no need to loop - GLBasic does that for you
WHILE TRUE
ok% = SOCK_RECV(sock%, str$, size%)
IF ok% = -2 THEN CONTINUE // no data sent, yet. Retry
BREAK
WEND
RETURN str$
ENDFUNCTION
//-------------------------------------------------------------------------
SUB server:
LOCAL Done=0
LOCAL ClientIPAddress%
LOCAL Sock%
STDOUT "Waiting for connection...\n"
Sock=SOCK_TCPLISTEN(Net_SocketNumber) // Listen for connections on our socket
WHILE Done=0
EstablishedConnection=SOCK_TCPACCEPT(Sock,ClientIPAddress) // Check for established client connection
IF EstablishedConnection > 0 THEN Done=1 // If a connection was successful
WEND
// We now have a connection
STDOUT "Sending...\n"
LOCAL names$[]
DIMDATA names$[], "John Rambo", "Jim Knopf", "Kermit the Frog", "Dr. Shiver - buy now"
WHILE TRUE
SendString(EstablishedConnection, names$[RND(LEN(names$[])-1)] )
SLEEP 3000
WEND
ENDSUB
//-------------------------------------------------------------------------
SUB Client:
LOCAL Done=0
LOCAL ReturnCode
LOCAL ReceivedMessage$
LOCAL YLoc=50
STDOUT "Establishing connection with "+Net_ServerIP$+" on port "+Net_SocketNumber+"...\n"
EstablishedConnection=SOCK_TCPCONNECT(Net_ServerIP$,Net_SocketNumber) // Try to connect to server
IF EstablishedConnection = -1 // Failure
STDERR "Network error : client failed to create connection"
END
ENDIF
WHILE TRUE
ReceivedMessage$ = ReceiveString$(EstablishedConnection)
IF LEN(ReceivedMessage$)
STDOUT "Got: \"" + ReceivedMessage$ + "\" len="+LEN(ReceivedMessage$)+"\n"
ENDIF
SLEEP 500
WEND
ENDSUB
--- End code ---
Kitty Hello:
Read it into a string then send. The LEN and SEND functions do not care about '\0' characters.
Kitty Hello:
the UUENCODE is something you have to do yourself. The socket commands don't do that.
I have posted an article how to upload a png file to a webserver (HTTP), see that.
I think all problems are solved now, no?
Moru:
I don't realy see the problem, GLB treats strings as binary data right up to the instant you try to print it to the screen, right? So just use it as binary data, don't print it to the screen.
Navigation
[0] Message Index
[*] Previous page
Go to full version