'how to communicate with each client connected to a multi threaded server using a separate command prompt
client.py
import socket
ClientSocket = socket.socket()
host = '192.168.1.101'
port = 7070
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print("Your Computer Name is:" + hostname)
print("Your Computer IP Address is:" + IPAddr)
try:
ClientSocket.connect((host, port))
except socket.error as e:
print(str(e))
while True:
Input = input('Say Something: ')
ClientSocket.send(str.encode(Input))
Response = ClientSocket.recv(2048)
print(Response)
print(Response.decode('utf-8'))
if not Response:
break
ClientSocket.close()
server.py
import socket
from _thread import *
def start_new_sock_thread(connection):
while True:
data = connection.recv(2048)
print(data)
client_id = data.decode('utf-8')
print (client_id)
Input = input('Tell me: ')
connection.send(str.encode(Input))
connection.close()
def client_listener():
HOST_IP = "192.168.1.101"
HOST_PORT = 7070
ThreadCount = 0
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind((HOST_IP, HOST_PORT))
except socket.error as sock_error:
print(str(sock_error))
s.listen(100)
while True:
conn, addr = s.accept()
print("Connected to : " + addr[0] + ":" ,str(addr[1]))
start_new_thread(start_new_sock_thread, (conn, ))
ThreadCount += 1
print('Thread Number: ' + str(ThreadCount))
client_listener()
multiple clients can run and connect to this server. but i need a prompt / shell to communicate with each connected client. we can automate in server that what we need to do in client side. but i need to manually control the client. using a command shell. i need to communicate with each connected client using a separate command and control shell.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
