TCPIPserverTest.py

You can view and download this file on Github: TCPIPserverTest.py

 1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2# This is an EXUDYN example
 3#
 4# Details:  Example for connecting two Python codes via TCP/IP
 5#           See file TCPIPclientTest.py for running on the other Python instance
 6#
 7# Author:   Johannes Gerstmayr
 8# Date:     2021-11-06
 9#
10# Copyright:This file is part of Exudyn. Exudyn is free software. You can redistribute it and/or modify it under the terms of the Exudyn license. See 'LICENSE.txt' for more details.
11#
12#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
14import socket
15import sys
16import time
17import struct
18
19#https://docs.python.org/3/library/struct.html
20packer = struct.Struct('I d d d d') #I=unsigned int, i=int, d=double
21ackPacker = struct.Struct('I')
22
23# Create a TCP/IP socket
24sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
25
26HOST='127.0.0.1'
27PORT = 65124
28
29# # Bind the socket to the port
30# server_address = (HOST, PORT)
31# print('starting up on %s port %s' % server_address)
32# sock.bind(server_address)
33
34# #Calling listen() puts the socket into server mode, and accept() waits for an incoming connection.
35# # Listen for incoming connections
36# sock.listen(1)
37
38
39with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
40    s.bind((HOST, PORT))
41    s.listen()
42    conn, addr = s.accept()
43    with conn:
44        print('Connected by', addr)
45        while True:
46            # data = conn.recv(1024) #data size in bytes
47            data = conn.recv(packer.size) #data size in bytes
48
49            if not data:
50                break #usually does not happen!
51
52            unpacked_data = packer.unpack(data)
53            print('data=',unpacked_data, ', len=',len(unpacked_data))
54
55            chksum = sum(data) #checksum of packed bytes
56            ackData = ackPacker.pack(int(chksum+unpacked_data[1]))
57            conn.sendall(ackData)
58
59
60
61# while True:
62#     # Wait for a connection
63#     print('waiting for a connection')
64#     connection, client_address = sock.accept()
65# #accept() returns an open connection between the server and client, along with the address of the client. The connection is actually a different socket on another port (assigned by the kernel). Data is read from the connection with recv() and transmitted with sendall().
66
67# try:
68#     print('connection from', client_address)
69
70#     # Receive the data in small chunks and retransmit it
71#     while True:
72#         data = connection.recv(16)
73#         print('received "%s"' % data)
74#         if data:
75#             print(sys.stderr, 'sending data back to the client')
76#             connection.sendall(data)
77#         else:
78#             print('no more data from', client_address)
79#             break
80
81# finally:
82#     # Clean up the connection
83#     connection.close()