0% found this document useful (0 votes)
27 views4 pages

728 Assgn3.

The document describes a TCP socket program where a client and server can exchange messages. The client sends a string to the server, which echoes back characters from either the even or odd indices depending on the length of the string. This continues until the client sends 'bye' to close the connection.

Uploaded by

rohit Kumar
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)
27 views4 pages

728 Assgn3.

The document describes a TCP socket program where a client and server can exchange messages. The client sends a string to the server, which echoes back characters from either the even or odd indices depending on the length of the string. This continues until the client sends 'bye' to close the connection.

Uploaded by

rohit Kumar
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/ 4

ASSIGNMENT–03

NAME-ROHIT KUMAR
REG.NO.- 728
ROLL NO.-CSE/21068
SUBJECT CODE- CSC611
EMAIL:- cse21068@iiitkalyani.ac.in

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 code:-
import socket

port=50000
host="127.0.0.1"

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


server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((host, port))
print("socket binded to %s" %(port))

server.listen(2)
print("Socket is listening...")

# Accepting/Establishing connection from client.


conn, addr = server.accept()
print('Got connection from', addr)

while True:
recieved_data = conn.recv(2048)
print("Message from client: ",recieved_data.decode())
if recieved_data.decode()=='quit':

1
break

print("Connection closed from client")

#Close the connection with the client


conn.close()

Client code:-
import socket

port=50000
portClient=8000
host="127.0.0.1"

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


client.bind((host, portClient))
client.connect((host, port))
while True:
data = input("Enter your message: ")
client.send(data.encode())
if data=='quit':
break

print("Connection closed from server")


client.close()

Output:-

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 code:-
3. import socket
4. def get_echo_positions(message):
5. length = len(message)

2
6. return [i for i in range(length) if (i % 2 == 1 and length % 2 ==
0) or (i % 2 == 0 and length % 2 == 1)]
7.
8. # Create a socket object
9. server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
10. server_address = ('localhost', 12345)
11. server_socket.bind(server_address)
12. server_socket.listen(1)
13.
14. print('Server is listening on {}:{}'.format(*server_address))
15.
16. while True:
17. client_socket, client_address = server_socket.accept()
18. print('Connection established from {}:{}'.format(*client_address))
19.
20. while True:
21. data = client_socket.recv(1024).decode('utf-8')
22.
23. if data.lower() == 'bye':
24. print('Client disconnected. Exiting...')
25. break
26. echo_positions = get_echo_positions(data)
27.
28. echoed_characters = ''.join([data[i] for i in echo_positions])
29.
30.
31. client_socket.sendall(echoed_characters.encode('utf-8'))
32. print('Echoed characters:', echoed_characters)
33.
34.
35. client_socket.close()
36.
37. server_socket.close()
38.

Client code:-
import socket

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

server_address = ('localhost', 12345)


client_socket.connect(server_address)
print('Connected to {}:{}'.format(*server_address))

while True:

3
message = input('Enter a message (type "bye" to exit): ')

client_socket.sendall(message.encode('utf-8'))

if message.lower() == 'bye':
print('Closing connection. Goodbye!')
break

echoed_characters = client_socket.recv(1024).decode('utf-8')
print('Server echoed back:', echoed_characters)

# Close the connection


client_socket.close()

Output:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy