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

Reashirch

Uploaded by

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

Reashirch

Uploaded by

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

python

Copy code
import socket

def start_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('127.0.0.1', 12345))
server_socket.listen(1)
print("Server is listening on port 12345...")

client_socket, addr = server_socket.accept()


print(f"Connection from {addr} has been established!")

while True:
message = client_socket.recv(1024).decode('utf-8')
if message.lower() == 'exit':
print("Client has exited the chat.")
break
print(f"Client: {message}")

response = input("You: ")


client_socket.send(response.encode('utf-8'))

client_socket.close()
server_socket.close()

if __name__ == "__main__":
start_server()
Client Code (client.py)

python
Copy code
import socket

def start_client():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('127.0.0.1', 12345))

while True:
message = input("You: ")
client_socket.send(message.encode('utf-8'))

if message.lower() == 'exit':
print("Exiting chat.")
break

response = client_socket.recv(1024).decode('utf-8')
print(f"Server: {response}")

client_socket.close()

if __name__ == "__main__":
start_client()
How to Run
Save the Server Code: Create a file named server.py and copy the server code into
it.

Save the Client Code: Create another file named client.py and copy the client code
into it.
Run the Server:

Open a terminal and run the server code:


Copy code
python server.py
Run the Client:

Open another terminal and run the client code:


Copy code
python client.py
Chat: Type messages in the client terminal and see them displayed in the server
terminal. Type "exit" to end the chat.

This is a basic example, and you can expand it further with features like error
handling, multiple clients, or a graphical user interface. Let me know if you have
any questions or need further modifications!

You might also like