直接换下ip就能运行
service.py
# -*-coding:utf-8 -*-
'''
@desc: 服务端,先开启服务端,等待客户端连接
'''
import socket
def main():
# 1.创建套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2.绑定端口
addr = ("x.x.x.x", 9999)
tcp_server_socket.bind(addr)
print("绑定成功自身的ip与port")
# 3.监听链接
tcp_server_socket.listen(128)
# 4.接收别人的连接
# client_socket用来为这个客户端服务
client_socket, client_addr = tcp_server_socket.accept()
print("获取成功")
# 5.接收对方发送的数据
recv_data = client_socket.recv(1024)
print("接收到的数据:%s" % recv_data)
# 6.给对方发送数据
client_socket.send("hahaha".encode("utf-8"))
# 7.关闭套接字
client_socket.close()
tcp_server_socket.close()
if __name__ == "__main__":
main()
client.py
# -*-coding:utf-8 -*-
'''
@desc: 客户端,连接服务端,发送数据
'''
import socket
import time
def main():
# 1.创建套接字socket
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2.连接服务器
dest_ip = "x.x.x.x"
dest_port = 9999
dest_addr = (dest_ip, dest_port)
tcp_socket.connect(dest_addr)
# 3. 接收/发送数据
send_data = raw_input("请输入要发送的数据:")
tcp_socket.send(send_data)
# 接收服务器发送的数据
recv_data = tcp_socket.recv(1024)
print(recv_data.decode("utf-8"))
# 4. 关闭套接字socket
tcp_socket.close()
if __name__ == "__main__":
main()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)