Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python In Part 1, you learned of creating a simple connection (known as a TCP an

ID: 3794320 • Letter: P

Question

Python In Part 1, you learned of creating a simple connection (known as a TCP and UDP socket). In Part 2, a group of two students (as a team, or individually) will develop a more realistic application. Then, you will implement it using either TCP socket or UDP socket mechanisms. You can write complete client-and-server programs using Python. If you prefer to do it by yourself, you should figure out to run two programs (a client and a server) on two different host machines (either physical or virtual machines.) This is your responsibility to create this platform (either physical or virtual). Deciding an application (e.g., gaming) is up to you or your team's decision and, if possible, using your imagination and creativity (at least some level) can play an important role. The list of tasks includes: 1. 'Think" and decide a client-server network application you wish to develop. Determine either UDP socket or TCP socket in implementing the chosen application. Justify your decision about a socket type (e.g., why did your team or you choose a TCP socket rather than a UDP socket), based on what you have learned in this class. Implement the chosen client-server network application using either UDP socket or TCP socket. captured screen shots to show the step-by-step process and the results, the source code with comments Thanks for your help

Explanation / Answer

Here i am developing clinet server application which receives incoming messages and echos them back to the sender.

Step 1 creating a TCP/IP socket.

import socket import sys # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Step 2:

bind() is used to associate the socket with the server address. In this case, the address is localhost, referring to the current server, and the port number is 10000.

# Bind the socket to the port server_address = ('localhost', 10000) print >>sys.stderr, 'starting up on %s port %s' % server_address sock.bind(server_address)

Step 3:

Calling listen() puts the socket into server mode, andaccept() waits for an incoming connection.

# Listen for incoming connections sock.listen(1) while True: # Wait for a connection print >>sys.stderr, 'waiting for a connection' connection, client_address = sock.accept()

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().

try: print >>sys.stderr, 'connection from', client_address # Receive the data in small chunks and retransmit it while True: data = connection.recv(16) print >>sys.stderr, 'received "%s"' % data if data: print >>sys.stderr, 'sending data back to the client' connection.sendall(data) else: print >>sys.stderr, 'no more data from', client_address break finally: # Clean up the connection connection.close()

When communication with a client is finished, the connection needs to be cleaned up using close(). This example uses atry:finally block to ensure that close() is always called, even in the event of an error.

Client side Step by step Process

The client program sets up its socket differently from the way a server does.

Step1:Instead of binding to a port and listening, it uses connect() to attach the socket directly to the remote address.

import socket import sys # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Connect the socket to the port where the server is listening server_address = ('localhost', 10000) print >>sys.stderr, 'connecting to %s port %s' % server_address sock.connect(server_address)

Step 2:

After the connection is established, data can be sent through the socket with sendall() and received with recv(), just as in the server.

try: # Send data message = 'This is the message. It will be repeated.' print >>sys.stderr, 'sending "%s"' % message sock.sendall(message) # Look for the response amount_received = 0 amount_expected = len(message) while amount_received < amount_expected: data = sock.recv(16) amount_received += len(data) print >>sys.stderr, 'received "%s"' % data finally: print >>sys.stderr, 'closing socket' sock.close()

When the entire message is sent and a copy received, the socket is closed to free up the port.

Client and Server Together

The client and server should be run in separate terminal windows, so they can communicate with each other. The server output is:

$ python ./socket_echo_server.py starting up on localhost port 10000 waiting for a connection connection from ('127.0.0.1', 52186) received "This is the mess" sending data back to the client received "age. It will be" sending data back to the client received " repeated." sending data back to the client received "" no more data from ('127.0.0.1', 52186) waiting for a connection

The client output is:

$ python socket_echo_client.py connecting to localhost port 10000 sending "This is the message. It will be repeated." received "This is the mess" received "age. It will be" received " repeated." closing socket $