I have a system that communicates using the TCP/IP protocol. I regularly communicate with it using the socket module in Python. For proper inputs, the system gives an acknowledgement (Ack) immediately, then processes the input and gives another useful message. For incorrect inputs (unknown format or checksum error), the system gives a Not-Acknowledge (NAck) immediately and does not process the input further.
Now I want to test the working of this system on the basis of 'communication': is it responding with the proper Ack/NAck messages?
For this, I used the term "tera term. I connected over SSH, passing the system's IP Address (say: 192.168.0.68) and port (say 123456) and sent and received the expected response as in brackets
- a short message and long message (NAck - input length short & NAck - input length big)
- a message with improper checksum (NAck - checksum error)
- a message with improper sequence of keywords (format) (NAck - input wrong format)
- a message with illegal (invalid) input (NAck - input invalid)
- entered a character and did not finish before the timeout seconds (NAck - timeout on input)
[The system will understand if it is the end of input or not, with the '\r\n' characters] I got all the expected behavior.
But when I tried to do the same with socket module in python, I was not able to get 'NAck - input length big' for a 'long message' input to the system, rather I get: " NAck - timeout on input"
Using the socket module, I tried this:
import socket
def send_data(ip_address, data, timeout=5000):
try:
# Create a socket object
ocket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
# Connect to the target IP address
sock.connect((ip_address, 12800))
# Send the data
# sock.sendall(data.encode())
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 65536)
total_sent = 0
tal_sent len(data):
sent = sock.send(data[total_sent:])
print(sent)
total_sent += sent
# print(str(sock.recv(2048)))
# Close the connection
sock.close()
return True # Data sent successfully
except socket.timeout:
return False # Timeout error
# Usage
ip_address = "192.168.0.68"
temp_str = "ThisisTheCoreInput" * 50 +'\r\n'
data = temp_str.encode()
result = send_data(ip_address, data)
if result:
print("Data sent successfully")
else:
print("Timeout error occurred")
I have used the socket.sendAll() method also, but with -ve. I have also used multiple buffer sizes.
This is another method I tried using the asyncio module:
import asyncio
async def send_data():
try:
reader, writer = await asyncio.open_connection('192.168.0.68', 12800)
data = "ThisisTheCoreInput" * 50 +'\r\n' # to generatea very big string
writer.write(data.encode())
await writer.drain()
data = await reader.read(4096)
msg = data.decode()
print(msg)
writer.close()
await writer.wait_closed()
except ConnectionRefusedError:
print("Connection refused. Make sure the server is running.")
async def main():
await send_data()
asyncio.run(main())
This was also -ve.
I figured out that I am not able to flush the data as quickly as possible. As tera term is able to give me the 'NAck:input length big' as expected,