The purpose of this Challenge is for you to gain insights into the mechanics of establishing sockets and their operation. Although this Challenge is to be exec
The purpose of this Challenge is for you to gain insights into the mechanics of establishing sockets and their operation.
Although this Challenge is to be executed on one machine (your's; aka 'localhost,' or 127.0.0.1), it is conceptually similar to executing socket programs across multiple computers connected via a network.
PREPARATION
- Read all of Chap 4 (Interprocess Communication) BEFORE attempting this Challenge.
- Be sure you have a sound understanding of 'localhost' (127.0.0.1). You should be knowledgeable regarding the purpose of localhost AND WHERE localhost IS DEFINED ON YOUR MACHINE!
- Download, save, and view the "Socket Demo Execution" video; here's the link
INSTRUCTIONS
- Download and save both the socket_server_student.py and the socket_client_student.py files into the SAME folder/directory.
- Be sure you know the path to both files.
- Open two Terminals/Command Lines. One is for the client program; the other for the server program.
- In your OS' Terminal/Command Line, navigate to the folder where the files are located.
- In their various Terminal/Command Line environments, inspect both programs; there are purposeful errors (and possibly omissions) present in the files.
- Notice Line 23 in the 'client' program: BE SURE YOU UNDERSTAND the purpose and meaning of the 'b' preceding the message string!
- Use a Text Editor and remedy the errors and omissions.
- As shown in the Demo Video, first start the server program from the Terminal/Command Line.
- NOTE: You are LIKELY (but not guaranteed) to encounter a firewall message from your OS (macOS or Windows).
- If you do, allow the program to gain the otherwise restricted access.
- Start the client program from its Terminal/Command Line.
- Observe the results and take two screenshots; one for the server, the other for the client.
- List the contents of the directory.
- Print (e.g., cat in macOS; type in Windows) the file: CIS64E_socket_server_demo_output.dat
- Take a screenshot of the contents of CIS64E_socket_server_demo_output.dat
- Upload your working programs and screenshots into Canvas.
WRITTEN RESPONSE
In the space provided below, answer this question: Assuming the programs properly execute and a socket is formed, what is being transmitted from client to server (1 word, 7 letters)?
#!/usr/bin/python3 __author__ = 'Rick Hubbard' # Developed for DeAnza College CIS64E # localhost (127.0.0.1) Socket Client import socket size = 512 host = '' #Will default to localhost (127.0.0.1) port = 9242 #Arbitrary port selection #Define Socket Family as Internet and Type as TCP (i.e., Socket Stream) socket_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Define a Python tuple with coordinates to the server server_coordinates = (host, port) #[Attempt to] Connect with the server socket_client.connect(server_coordinates) #If connection with server was successful, then send a message as a BYTE string (not chr string) try: message = b' This is an example of sending a message via a socket from a client to a servern' #Ideally, message will be sent from client to server via the provisioned socket and server will process message socket_client.sendall(message) except socket.errno as e: #If message send was unsuccessful, capture error message print("Socket error ", e) #Display error message finally: socket_client.closer() #When doen, always Close() sockets!
,
#!/usr/bin/python3 __author__ = 'Rick Hubbard' # Developed for DeAnza College CIS64E # localhost (127.0.0.1) Socket Server import socket size = 512 host = '' #Will default to localhost (127.0.0.1) port = 9242 #Arbitrary port selection #Define Socket Family as Internet and Type as TCP (i.e., Socket Stream) socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Options for Socket socket_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #Bind socket to a specific IP address and port socket_server.bind((host, port)) #Is anyone there? socket_server.listen(5) #When a connection is successful (that is, a 'client' connects with this server), # then store data about the client c, addr = socket_server.accept() #Receive message (data) from client message = c.recv(size) if message: f_out = open("CIS64E_socket_server_demo_output.dat", '+w') #Create an output file for posterity print("Connection established with: ", addr[0]) f_out.write(addr[0]) f_out.write(":") f_out.write(message.decode("utf-8")) f_out.close() #When doen, always Close() files! socket_server.closer() #When doen, always Close() sockets!
Collepals.com Plagiarism Free Papers
Are you looking for custom essay writing service or even dissertation writing services? Just request for our write my paper service, and we'll match you with the best essay writer in your subject! With an exceptional team of professional academic experts in a wide range of subjects, we can guarantee you an unrivaled quality of custom-written papers.
Get ZERO PLAGIARISM, HUMAN WRITTEN ESSAYS
Why Hire Collepals.com writers to do your paper?
Quality- We are experienced and have access to ample research materials.
We write plagiarism Free Content
Confidential- We never share or sell your personal information to third parties.
Support-Chat with us today! We are always waiting to answer all your questions.