服务端见点击打开链接
socket的异常
socket的TCP和UDP客户端模型
TCP的客户端
import socket#ip:127.0.0.1,port 3214
HOST='' #warning:Ip addr can only be dropped in server!
PORT=3214#AF_INET IPV4
#SOCK_DGRAM UDP
s=socket.socket()#try to receive data from server & send data to server
#Pay attention to try statement .Client need connect to server in tcp,so you may catch exceptions,
#while in udp you just receive and send data!
try:s.connect((HOST,PORT))data=u'Hello TCP Server!'while data:#encode data from utf-8 to bytess.sendall(data.encode('utf-8'))recv_data=s.recv(1024)#decode data from bytes to utf-8print 'Receive from Server:\n',recv_data.decode('utf-8')data=raw_input('Please input an info:\n')
except socket.error as err:print err
finally:s.close()
UDP的客户端
#coding:utf-8
import socketHOST='127.0.0.1'
PORT=3214s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)data='Hello UDP server!'while data:s.sendto(data.encode('utf-8'),(HOST,PORT))if data == 'bye':breakrecv_data,addr=s.recvfrom(1024)print 'Receive from Server:\n',recv_data.decode('utf-8')data=raw_input('Please input an info:\n')
s.close()