基于tcp协议的通讯录代码
时间: 2024-03-06 14:46:41 浏览: 20
好的,下面是一个简单的基于TCP协议的通讯录的代码示例,使用Python语言实现:
服务器端代码:
```python
import socket
import threading
import mysql.connector
# 与MySQL数据库建立连接
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
# 创建服务端Socket对象
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', 8888))
server_socket.listen(5)
# 处理客户端请求的函数
def handle_client(client_socket, client_address):
print(f'客户端{client_address}已连接')
while True:
# 接收客户端发送的数据
data = client_socket.recv(1024)
if not data:
break
# 解析客户端发送的请求
request = data.decode('utf-8')
request = request.split(',')
# 根据请求类型进行相应的操作
if request[0] == 'add':
# 添加联系人
name = request[1]
phone = request[2]
cursor = mydb.cursor()
sql = "INSERT INTO contacts (name, phone) VALUES (%s, %s)"
val = (name, phone)
cursor.execute(sql, val)
mydb.commit()
response = 'ok'
elif request[0] == 'delete':
# 删除联系人
id = request[1]
cursor = mydb.cursor()
sql = "DELETE FROM contacts WHERE id = %s"
val = (id,)
cursor.execute(sql, val)
mydb.commit()
response = 'ok'
elif request[0] == 'query':
# 查询联系人
cursor = mydb.cursor()
sql = "SELECT * FROM contacts"
cursor.execute(sql)
result = cursor.fetchall()
response = ''
for row in result:
response += f'{row[0]},{row[1]},{row[2]}\n'
else:
response = 'invalid request'
# 发送响应数据给客户端
client_socket.send(response.encode('utf-8'))
# 关闭客户端连接
client_socket.close()
print(f'客户端{client_address}已断开连接')
# 监听客户端连接
while True:
client_socket, client_address = server_socket.accept()
# 创建新线程处理客户端请求
client_thread = threading.Thread(target=handle_client, args=(client_socket, client_address))
client_thread.start()
```
客户端代码:
```python
import socket
# 创建客户端Socket对象
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('localhost', 8888))
# 添加联系人
def add_contact(name, phone):
request = f'add,{name},{phone}'
client_socket.send(request.encode('utf-8'))
response = client_socket.recv(1024).decode('utf-8')
if response == 'ok':
print('添加联系人成功')
else:
print('添加联系人失败')
# 删除联系人
def delete_contact(id):
request = f'delete,{id}'
client_socket.send(request.encode('utf-8'))
response = client_socket.recv(1024).decode('utf-8')
if response == 'ok':
print('删除联系人成功')
else:
print('删除联系人失败')
# 查询联系人
def query_contacts():
request = 'query'
client_socket.send(request.encode('utf-8'))
response = client_socket.recv(1024).decode('utf-8')
if response == '':
print('通讯录为空')
else:
print('联系人列表:')
contacts = response.split('\n')
for contact in contacts:
if contact != '':
id, name, phone = contact.split(',')
print(f'{id}: {name} {phone}')
# 命令行界面
while True:
command = input('请输入命令(add/delete/query/exit):')
if command == 'add':
name = input('请输入联系人姓名:')
phone = input('请输入联系人电话:')
add_contact(name, phone)
elif command == 'delete':
id = input('请输入要删除的联系人ID:')
delete_contact(id)
elif command == 'query':
query_contacts()
elif command == 'exit':
break
else:
print('无效命令')
# 关闭客户端连接
client_socket.close()
```
以上代码仅供参考,具体实现可以根据具体需求进行调整。