import socket
#服务器端
sock = socket.socket()
HOST = '127.0.0.1' #本机IP
PORT = 2049
INFO = (HOST, PORT)
sock.bind(INFO) #绑定主机和端口号到套接字上
sock.listen(8) #连接最大为8个
try:
while True:
client_socket, address = sock.accept() #接受TCP客户端的连接
print address
while True:
received_data = client_socket.recv(50) #接受的字节数
if not received_data:
break
client_socket.sendall("%s" %received_data)
print received_data
client_socket.close()
except Exception as e:
raise e
finally:
sock.close()
import socket
#socket通信客户端
client = socket.socket()
HOST = '127.0.0.1' #本机IP
PORT = 2049
INFO = (HOST, PORT)
client.connect(INFO)
#client.send("hello")
while 1:
myinput = raw_input()
client.send(myinput)
data = client.recv(1024)
if data:
print data
else:
break
client.close()
阅读(834) | 评论(0) | 转发(0) |