What is the mistake. Why is the connection being broken?


#1

I took the TCPClient_MultiThread example.

local ip	= "127.0.0.2"
local port	= 49211
local err	= 0
local socket= 0
local tcpData = nil
local buf	= nil
local data	= nil

while true do
	::create_server::
	err, socket = TCPCreate(false, ip, port)
	if err ~= 0 then
		--print("Failed to create socket, re-connecting")
		Sleep(1000)
		goto create_server
	end
	err = TCPStart(socket, 0)
	if err ~= 0 then
		--print("Failed to start server, re-connecting")
		TCPDestroy(socket)
		Sleep(1000)
		goto create_server
	end
	TCPWrite(socket,"start")
	while true do
			TCPWrite(socket," (0) ")
		err, buf = TCPRead(socket, 1000,"string")
			TCPWrite(socket," (1) ")
		data =buf.buf
		if err ~= 0 then
			TCPDestroy(socket)
			--TCPWrite(socket," (2) ")
			--print("Failed to read data, re-connecting")
			TCPDestroy(socket)
			Sleep(1000)
			break
		else
			TCPWrite(socket,"O'k;  ")
		end
		
		if  data== "S" then
		--print("Receive data:",data)
		
		TCPWrite(socket,"stop")
		else
		TCPWrite(socket,"No stop")
		
		end
	end
end

The connection is disconnected if there is no response. And it doesn’t restart anymore. It is not known on which line the interpreter stopped.

Time[22:45:00 480] Send: [127.0.0.1:61168] 客户端上线
Time[22:45:00 481] Receive: [127.0.0.1:61168] start (0) 
Time[22:45:01 482] Receive: [127.0.0.1:61168]  (1) 
Time[22:45:01 506] Send: [127.0.0.1:61168] 客户端下线

If I manage to submit the “S”, I see the response from the virtual robot, but then the connection is still broken.

Time[22:41:07 536] Send: [127.0.0.1:61150] 客户端上线
Time[22:41:07 543] Receive: [127.0.0.1:61150] start (0) 
Time[22:41:08 057] Send: [127.0.0.1:61150] S
Time[22:41:08 066] Receive: [127.0.0.1:61150]  (1) O'k;  
Time[22:41:08 066] Receive: [127.0.0.1:61150] stop (0) 
Time[22:41:09 066] Receive: [127.0.0.1:61150]  (1) 
Time[22:41:09 074] Send: [127.0.0.1:61150] 客户端下线

#2
err, buf = TCPRead(socket, 1000,"string")

returns err = 4. There is no such error in the documentation

Is there a complete list of errors?


#3

it is also interesting to know all the names of the returned buf table (buf.buf, buf.???)