Hi momo7807,
You can use TCPRecv and TCPSend either on server or client equally, but if you are using it on the server, client should be connected, otherwise, you will get an error. If you send it from the client to the server, you also have to know server's socket, where to send data. That means, that you should be connected to the server. That's all.
Small example:
Server
local const $IP_ADDR = "127.0.0.1"
;We set an IP adress of your server here. I set this IP that you can test this Script on one PC for the server and client either.
local const $PORT = 31782
;Your server's port. Client should be connected also by using this port.
local const $MAX_INCOMMING_CONNECTIONS = 10
;This way you can set how many connections server can accept
local $listen = -1
;Variable, which will be used to listen for incomming connections
local $client_socket = 0
;This variable holds an information about the connected client socket.
tcpStartup()
;We started the TCP services here. But don't forget when you will do some real program to check an errors for all these actions.
$listen = tcpListen($IP_ADDR, $PORT, $MAX_INCOMMING_CONNECTIONS)
;We created a listenning socket here.
do
$client_socket = tcpAccept($listen)
;Accept the incomming connection. We simply put this code itno forever loop to catch if someone wants to connect.
until $client_socket <> -1
;Do this forever loop until client socket wil be unequal to -1. -1 is a value, which are returned by TCPAccept on failure. So if -1 is returned, it means, that noone connected to your server yet and you need to wait for a while. You also can use if @error = -2 then it means, that client is not connected.
local $msg_from_client_socket = ""
;This variable will be used to store a messages from client socket.
do
$msg_from_client_socket = tcpRecv($client_socket, 1024)
;We are waiting for the client's message. 1024 means, that we accept max 1 KB of data.
until $msg_from_client_socket <> ""
;If we got something here, this variable returns a message from the client socket, otherwise, it will be just empty string, like "". So we also put it into a forever loop to wait.
msgBox(0, "Message from the client socket", $msg_from_client_socket)
tcpSend($client_socket, "Hi, I'm a server.")
;We send something to client here.
tcpCloseSocket($listen)
tcpCloseSocket($client_socket)
tcpShutDown()
;We just turned off the server.
Now for the client
local const $SERVER_IP_ADDR = "127.0.0.1"
;Use this if you want to test it on the same PC where the server is started.
local const $SERVER_PORT = 31782
;The server's port.
tcpStartup()
local $server_socket = tcpConnect($SERVER_IP_ADDR, $SERVER_PORT)
;We connect to the server and get the server's socket.
tcpSend($server_socket, "Hi, I'm a client.")
;We sent a message to the server.
local $msg_from_server_socket = ""
do
$msg_from_server_socket = tcpRecv($server_socket, 1024)
;We scanning for incomming message here.
until $msg_from_server_socket <> ""
msgBox(0, "Message from server socket", $msg_from_server_socket)
tcpCloseSocket($server_socket)
tcpShutdown()
That's it, but as I already mentioned, don't forget to check errors everywhere.
I hope this helps.