Question
Assignment 3 Due: Oct 6 Write A Python TCP Server Program That Will Accept An Unlimited Number Of Connections, One At A Time, Just As
Assignment 3 Due: Oct 6 Write A Python TCP Server Program That Will Accept An Unlimited Number Of Connections, One At A Time, Just As In The Sample Program In The Lecture. Upon Receiving A TCP Connection Request, It Should Reply With The Client's IP Address And Port Number. Then, It Waits For Commands From The Client. Valid Commands Are \"TIME\", And \"EXIT.
All the above are put in one file (pdf or doc) Example of obtaining a time string Here is an interactive Python session showing how to come up with a string containing the current time of day using the time.ctime () method from the time module. >>> import time >>> now=time.ctime() >>> now Tue Sep 28 23:00:05 2021 Set a timeout for a socket This example shows how to set a timeout on a socket used to accept incoming connections and how to detect a timeout events as an exception. import socket ss = socket.socket (socket.AF INET, socket.SOCK STREAM) ss.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #Note: SO REUSEADDR will avoid port number re-use conflict when you #start the TCP socket program several times in a short time. ss.bind( (\", 4000)) ss.listen (1) while True : print \"waiting for connection\" new, addr = ss.accept() new.settimeout (10) msg = 'init' while len(msg): #when you close client socket, len(msg)=0. try: msg = new.recv(30) except socket. timeout: print \"got timeout\" break print msg new.close() ss.close()
Python file: Client.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 4000))
msg=s.recv(30) print msg
s.send(\"TIME\") msg = s.recv(30) print msg
s.send(\"HELLO\") msg = s.recv(30) print msg
s.send(\"EXIT\")
s.close()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started