Reashirch
Reashirch
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...")
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}")
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:
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!