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

Assign3 (1)

The document provides two TCP socket programming examples in Python. The first example establishes a connection between a client and server, where the client sends a message to the server, which then displays it. The second example involves a client sending a string to the server, which echoes back characters based on the string's length until the client sends 'bye'.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assign3 (1)

The document provides two TCP socket programming examples in Python. The first example establishes a connection between a client and server, where the client sends a message to the server, which then displays it. The second example involves a client sending a string to the server, which echoes back characters based on the string's length until the client sends 'bye'.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

​ Write a TCP socket program (in C/C++/Java/Python) to establish connection


between client and server. The client program will send a message to the server
and the server program will display the message.

❖​ SERVER.PY :-​

import socket

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


server_socket.bind(('localhost', 8080))
server_socket.listen(1)
print("Server is listening on port 8080...")

conn, addr = server_socket.accept()


print(f"Connected by {addr}")

message = conn.recv(1024).decode()
print(f"Message from client: {message}")

conn.close()

❖​ CLIENT.PY :-

​ ​ import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


client_socket.connect(('localhost', 8080))

message = input("Enter a message for the server: ")


client_socket.send(message.encode())
client_socket.close()
2.​ Write a TCP socket program where client sends a message (string) to server;
server echo back the characters at even position if length of the string is even
otherwise echo back the characters at odd position. This process continues until
the client sends ‘bye’.

❖​ Server.py :-

import socket

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


server_socket.bind(('localhost', 8081))
server_socket.listen(1)
print("Server is listening on port 8081...")

conn, addr = server_socket.accept()


print(f"Connected by {addr}")

while True:
message = conn.recv(1024).decode()
if message.lower() == 'bye':
print("Client disconnected.")
break

if len(message) % 2 == 0:
response= message[1::2]
else:
response= message[0::2]

conn.send(response.encode())

conn.close()

❖​ CLIENT.PY :-

import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


client_socket.connect(('localhost', 8081))

while True:
message = input("Enter a message for the server (type 'bye' to exit): ")
client_socket.send(message.encode())

if message.lower() == 'bye':
break

response = client_socket.recv(1024).decode()
print(f"Response from server: {response}")

client_socket.close()

You might also like