服务端代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import socket
s = socket.socket()
host = socket.gethostname()
port = 12345
s.bind((host, port))
s.listen(5)
while True:
c,addr = s.accept()
print '连接地址:', addr
c.send('welcom to cmcc')
c.close()
客户端代码:
# -*- coding: UTF-8 -*-
import socket
s=socket.socket()
host='47.107.89.109'
port=12345
s.connect((host,port))
print(s.recv(1024))
s.close()
先执行服务器端代码,让该机的12345端口监听服务器请求,如下:
然后执行客户端,客户端执行后,服务器端会发送'welcom to cmcc'给客户端,并且终止tcp连接: