Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON SOCKET PROGRAMMING I am trying to separate the Client and Server into 2 different programs, They work fine as a single program. It should

PYTHON SOCKET PROGRAMMING I am trying to separate the Client and Server into 2 different programs, They work fine as a single program. It should have the same output, But 2 different Client.py and Server.py programs #### CODE STARTS HERE #### import threading import time import random import socket as mysoc # server task def server(): global ss try: ss=mysoc.socket(mysoc.AF_INET, mysoc.SOCK_STREAM) print("[S]: Server socket created") except mysoc.error as err: print('{} '.format("socket open error ",err)) server_binding=('',50007) ss.bind(server_binding) ss.listen(1) host=mysoc.gethostname() print("[S]: Server host name is: ",host) localhost_ip=(mysoc.gethostbyname(host)) print("[S]: Server IP address is ",localhost_ip) csockid,addr=ss.accept() print ("[S]: Got a connection request from a client at", addr) # send a intro message to the client. msg="Welcome to CS 352" csockid.send(msg.encode('utf-8')) # Close the server socket ss.close() exit() #client task def client(): global cs try: cs=mysoc.socket(mysoc.AF_INET, mysoc.SOCK_STREAM) print("[C]: Client socket created") except mysoc.error as err: print('{} '.format("socket open error ",err)) # Define the port on which you want to connect to the server port = 50007 sa_sameas_myaddr =mysoc.gethostbyname(mysoc.gethostname()) # connect to the server on local machine server_binding=(sa_sameas_myaddr,port) cs.connect(server_binding) data_from_server=cs.recv(100) #receive data from the server print("[C]: Data received from server:: ",data_from_server.decode('utf-8')) # close the cclient socket cs.close() exit() t1 = threading.Thread(name='server', target=server) t1.start() time.sleep(random.random()*5) t2 = threading.Thread(name='client', target=client) t2.start() input("Hit ENTER to exit") exit()

### OUT PUT ###

[S]: Server socket created [S]: Server host name is: DELL [S]: Server IP address is 172.31.135.60 Hit ENTER to exit[C]: Client socket created [S]: Got a connection request from a client at ('172.31.135.60', 51907) [C]: Data received from server:: Welcome to CS 352

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Organizing Your Speech Points

Answered: 1 week ago