Absolutely! Here's an example of a graphical server-client local chat using the tkinter library in Python:
Server Code:
import socket
import threading
import tkinter as tk
def receive_message():
global chat_area
while True:
try:
message = client_socket.recv(1024).decode()
chat_area.insert(tk.END, message + "\n")
except:
# An error occurred, probably client disconnected
chat_area.insert(tk.END, "Error receiving message\n")
chat_area.configure(state=tk.DISABLED)
break
def send_message(event=None):
global chat_entry, chat_area
message = chat_entry.get()
chat_entry.delete(0, tk.END)
chat_area.insert(tk.END, "Server: " + message + "\n")
client_socket.send(message.encode())
if message.lower() == "bye":
chat_area.configure(state=tk.DISABLED)
client_socket.close()
root.quit()
def start_server():
global client_socket, chat_area, chat_entry, root, server_button
host = socket.gethostname()
port = 12345
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(5)
chat_area.insert(tk.END, "Server started.\n")
client_socket, address = s.accept()
chat_area.insert(tk.END, "Connected to " + str(address) + "\n")
# Enable chat input
chat_entry.configure(state=tk.NORMAL)
server_button.configure(state=tk.DISABLED)
# Start receiving messages in a separate thread
thread = threading.Thread(target=receive_message)
thread.start()
root = tk.Tk()
root.title("Chat Server")
# Create and pack the chat area
chat_area = tk.Text(root, state=tk.DISABLED)
chat_area.pack()
# Create and pack the chat input
chat_entry = tk.Entry(root)
chat_entry.bind("<Return>", send_message)
chat_entry.pack()
# Create and pack the server start button
server_button = tk.Button(root, text="Start Server", command=start_server)
server_button.pack()
root.mainloop()
Client Code:
import socket
import threading
import tkinter as tk
def receive_message():
global chat_area
while True:
try:
message = client_socket.recv(1024).decode()
chat_area.insert(tk.END, message + "\n")
except:
# An error occurred, probably server disconnected
chat_area.insert(tk.END, "Error receiving message\n")
chat_area.configure(state=tk.DISABLED)
break
def send_message(event=None):
global chat_entry, chat_area
message = chat_entry.get()
chat_entry.delete(0, tk.END)
chat_area.insert(tk.END, "Client: " + message + "\n")
client_socket.send(message.encode())
if message.lower() == "bye":
chat_area.configure(state=tk.DISABLED)
client_socket.close()
root.quit()
def connect_server():
global client_socket, chat_area, chat_entry, root, connect_button
host = socket.gethostname()
port = 12345
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, port))
chat_area.insert(tk.END, "Connected to server.\n")
# Enable chat input
chat_entry.configure(state=tk.NORMAL)
connect_button.configure(state=tk.DISABLED)
# Start receiving messages in a separate thread
thread = threading.Thread(target=receive_message)
thread.start()
root = tk.Tk()
root.title("Chat Client")
# Create and pack the chat area
chat_area = tk.Text(root, state=tk.DISABLED)
chat_area.pack()
# Create and pack the chat input
chat_entry = tk.Entry(root)
chat_entry.bind("<Return>", send_message)
chat_entry.pack()
# Create and pack the connect button
connect_button = tk.Button(root, text="Connect to Server", command=connect_server)
connect_button.pack()
root.mainloop()
To run the graphical chat:
- Run the server code, a tkinter window will appear.
- Run the client code on a separate terminal or IDE, another tkinter window will appear.
- Start typing messages in the client window and press Enter to send.
- The server and client windows will show the sent and received messages respectively.
The graphical implementation uses tkinter to create a user interface with chat area, chat input, and a button to start the server or connect to the server. The messages are displayed in the chat area, and the chat input is used to send messages.