0% found this document useful (0 votes)
35 views2 pages

Handle - Client: "Word Length:, First Character: "

This document contains code for a server and client program that communicate over sockets. The server receives words from connected clients, determines the length and first character of each word, and sends the results back. The client code connects to the server, sends a list of words, and prints the responses received.

Uploaded by

oobaa bbbssa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

Handle - Client: "Word Length:, First Character: "

This document contains code for a server and client program that communicate over sockets. The server receives words from connected clients, determines the length and first character of each word, and sends the results back. The client code connects to the server, sends a list of words, and prints the responses received.

Uploaded by

oobaa bbbssa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#server

import socket

def handle_client(client_socket):
word = client_socket.recv(1024).decode()
word_length = len(word)
first_character = word[0]
response = "Word length: {}, First character: {}".format(word_length, first_character)
client_socket.sendall(response.encode())
client_socket.close()

if __name__ == '__main__':
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("localhost", 6060))
server.listen(4)

print("Server started")

while True:
client_socket, client_address = server.accept()
print("New client connected:", client_address)
handle_client(client_socket)

#Client
import socket

def send_word(server_address, server_port, word):


client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((server_address, server_port))
client_socket.sendall(word.encode())
response = client_socket.recv(1024).decode()
print("Server response:", response)
client_socket.close()

if __name__ == '__main__':
server_address = "localhost"
server_port = 6060

words = ["jordan", "ajloun", "irbid", "amman"]

for word in words:


send_word(server_address, server_port, word)

You might also like