本代码是python实现的socket实例代码,包括客户端和服务端。 server.py为服务端。 client.py为客户端。
服务端代码server.py:
'''
by whg
2018-5-23
'''
import socket
#server address and port
address = ('127.0.0.1', 6000)
print('Waiting for client.')
# TCP
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(address)
server.listen(5)
client,addr = server.accept()
data = client.recv(100)
print(client," send:",data)
client.sendall(b'I have accepted.')
client.close()
server.close()
客户端代码client.py:
'''
by whg
2018-5-23
'''
import socket
#server address and port
address = ('127.0.0.1',6000)
#create socket
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(address)
#send message
client.sendall(b'hello, i am wang.')
#recv server'message
data = client.rec