-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added TCP echo server and modified example to use it
- Loading branch information
Björn Ritzl
committed
Jul 27, 2017
1 parent
13a4f93
commit 4c28d2a
Showing
3 changed files
with
43 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |