使用socket实现聊天室,附Python代码
时间: 2023-11-30 14:04:30 浏览: 72
好的,这是一个简单的基于Python的聊天室实现代码:
Server端代码:
```python
import socket
HOST = ''
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
conn.close()
```
Client端代码:
```python
import socket
HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
s.close()
print('Received', repr(data))
```
需要注意的是,这段代码只能实现单一的客户端和服务器之间的通信,如果你想要实现多客户端聊天室的话,需要做一些修改。
相关问题
使用socket实现聊天室并显示详细代码
好的,下面是一个简单的聊天室的实现代码,使用 Python 的 socket 模块:
服务端代码:
```python
import socket
import threading
# 设置服务器信息
HOST = '127.0.0.1'
PORT = 8000
# 创建 socket 对象
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 绑定地址和端口
server_socket.bind((HOST, PORT))
# 开始监听
server_socket.listen()
# 客户端列表
clients = []
# 处理客户端连接的函数
def handle_client(client_socket, addr):
print(f'New client {addr} connected')
clients.append(client_socket)
while True:
try:
data = client_socket.recv(1024)
except Exception as e:
print(f'Client {addr} disconnected')
clients.remove(client_socket)
break
if not data:
print(f'Client {addr} disconnected')
clients.remove(client_socket)
break
message = data.decode('utf-8')
print(f'Received message from {addr}: {message}')
# 将消息广播给其它客户端
for c in clients:
if c != client_socket:
c.sendall(data)
# 接受客户端连接的循环
while True:
client_socket, addr = server_socket.accept()
# 处理客户端连接的线程
client_thread = threading.Thread(target=handle_client, args=(client_socket, addr))
client_thread.start()
```
客户端代码:
```python
import socket
import threading
# 设置服务器信息
HOST = '127.0.0.1'
PORT = 8000
# 创建 socket 对象
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 连接服务器
client_socket.connect((HOST, PORT))
# 处理接收消息的函数
def handle_receive():
while True:
data = client_socket.recv(1024)
if not data:
break
message = data.decode('utf-8')
print(message)
# 启动接收消息的线程
receive_thread = threading.Thread(target=handle_receive)
receive_thread.start()
# 处理发送消息的循环
while True:
message = input('Enter message: ')
if not message:
break
data = message.encode('utf-8')
client_socket.sendall(data)
# 关闭连接
client_socket.close()
```
运行服务端代码后,可以启动多个客户端连接到服务器进行聊天。每当一个客户端发送消息时,服务器会将这条消息广播给其它所有客户端。
python基于socket实现简单的聊天室 代码
以下是一个基于Python socket实现的简单聊天室示例代码:
server.py:
```python
import socket
import threading
# 客户端列表
clients = []
def broadcast(msg):
# 向所有客户端广播消息
for client in clients:
client.send(msg)
def handle_client(client):
try:
while True:
# 接收客户端消息
msg = client.recv(1024)
if not msg:
break
# 广播消息
broadcast(msg)
except:
# 客户端异常断开连接
clients.remove(client)
client.close()
def start_server():
# 创建socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 绑定IP和端口
server_socket.bind(('127.0.0.1', 8888))
# 开始监听
server_socket.listen(5)
print('server start...')
while True:
# 接受客户端连接
client_socket, client_address = server_socket.accept()
print(f'{client_address} connected.')
# 添加客户端
clients.append(client_socket)
# 处理客户端消息
threading.Thread(target=handle_client, args=(client_socket,)).start()
if __name__ == '__main__':
start_server()
```
client.py:
```python
import socket
import threading
def receive(sock):
while True:
# 接收服务器消息
msg = sock.recv(1024).decode('utf-8')
print(msg)
def send(sock):
while True:
# 发送消息到服务器
msg = input()
sock.send(msg.encode('utf-8'))
def start_client():
# 创建socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 连接服务器
client_socket.connect(('127.0.0.1', 8888))
# 接收欢迎消息
print(client_socket.recv(1024).decode('utf-8'))
# 启动接收消息线程
threading.Thread(target=receive, args=(client_socket,)).start()
# 启动发送消息线程
threading.Thread(target=send, args=(client_socket,)).start()
if __name__ == '__main__':
start_client()
```
使用方法:
1.运行server.py启动服务器;
2.运行多个client.py启动客户端;
3.客户端可以输入任意消息,所有客户端都会收到该消息。
阅读全文