Skip to content

Commit

Permalink
Added TCP echo server and modified example to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Ritzl committed Jul 27, 2017
1 parent 13a4f93 commit 4c28d2a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions examples/tcp_client/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# TCP client example
This example shows how to create a TCP client and send and receive TCP data.

There's a Python based echo server in the tools folder. Start it by running `python echoserver.py` from a terminal. Connect to it from `localhost:5555`.
4 changes: 2 additions & 2 deletions examples/tcp_client/tcp_client.gui_script
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local tcp_client = require "defnet.tcp_client"

local SERVER_PORT = 5000
local SERVER_PORT = 5555

function init(self)
msg.post(".", "acquire_input_focus")
Expand Down Expand Up @@ -43,6 +43,6 @@ function on_input(self, action_id, action)
gui.set_enabled(self.send, false)
end)
elseif gui.pick_node(self.send, action.x, action.y) and gui.is_enabled(self.send) then
self.client.send("foobar " .. tostring(os.time()))
self.client.send("foobar " .. tostring(os.time()) .. "\r\n")
end
end
39 changes: 39 additions & 0 deletions tools/echoserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Example of simple echo server
# www.solusipse.net
# https://gist.github.com/solusipse/6419144

import socket


def listen():
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
connection.bind(('0.0.0.0', 5555))
connection.listen(10)
print "Waiting for connection"
while True:
current_connection, address = connection.accept()
print "Client connection " + str(address)
while True:
data = current_connection.recv(2048)

if data == 'quit\r\n':
current_connection.shutdown(1)
current_connection.close()
break

elif data == 'stop\r\n':
current_connection.shutdown(1)
current_connection.close()
exit()

elif data:
current_connection.send(data)
print data


if __name__ == "__main__":
try:
listen()
except KeyboardInterrupt:
pass

0 comments on commit 4c28d2a

Please sign in to comment.