0% found this document useful (0 votes)
3 views

TCPIP_Client_Server_Communication

Uploaded by

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

TCPIP_Client_Server_Communication

Uploaded by

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

# Server Code (server.

py)

import socket

def start_server():

host = '127.0.0.1' # Localhost

port = 65432 # Port to listen on

# Create a socket object

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:

server_socket.bind((host, port))

server_socket.listen()

print("Server is listening on {}:{}".format(host, port))

conn, addr = server_socket.accept()

with conn:

print('Connected by', addr)

while True:

data = conn.recv(1024)

if not data:

break

message = data.decode()

print(f"Client Sent: {message}")

reversed_message = message[::-1]

conn.sendall(reversed_message.encode())

if __name__ == "__main__":
start_server()

# Client Code (client.py)

import socket

def start_client():

host = '127.0.0.1' # The server's hostname or IP address

port = 65432 # The port used by the server

# Create a socket object

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:

client_socket.connect((host, port))

message = input("Enter a message to send to the server: ")

client_socket.sendall(message.encode())

data = client_socket.recv(1024)

print(f"Server Reply: {data.decode()}")

if __name__ == "__main__":

start_client()

You might also like