发送:UnicodeDecodeError: 'gb2312' codec can't decode bytes in position 2-3: illegal multibyte sequence、
接收:[Decode error - output not utf-8]
1、发送中文数据接收到的是乱码的解决
#endcoding=utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from socket import *
udpSocket=socket(AF_INET,SOCK_DGRAM)
udpSocket.bind(("",7788)) #参数是元组的形式
#使用udp发送的数据,在每一次的时候都需要写上接收方的IP和port
sendData="中国"
udpSocket.sendto(sendData.encode("gb2312"),("192.168.80.96",8080))
#recvData=udpSocket.recvfrom(1024)
#print(recvData)
解决方法:
1、在代码中添加
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
2、在发送的数据sendData
sendData.encode("gb2312") //编码
另一个问题,接收中文数据出现:[Decode error - output not utf-8]
为什么我的文件中找不到Python.sublime.build
后来又出现问题,代码编译莫名其妙出现错误,最后一行的后面出现错误,重新删掉编译后无错误,why?
问题二: udpSocket.bind(("192.168.80.145",4545)) File "<string>", line 1, in bindsocket[Decode error - output not utf-8]
分析:由于当前要绑定的端口正在使用,无法再次绑定。
因为原来的程序中采用线程一直发送消息,不会终止。在关掉网络调试助手和关掉sublime后,仍一直不断的发送数据。
解决方法:在cmd中查看 当前进程,输入:netstat -obna,发现当前的端口所在的PID号,找到进程管理器中对应的PID,发送是启动了python2来运行。将对应的进行关掉后问题解决。