from socket import *
from threading import Thread
def GetData(udpSocket):
while True:
recv_content = udpSocket.recvfrom(1024)
print(">>>:%s + %s" % (recv_content[0], recv_content[1]))
def PostData(udpSocket, Addr):
while True:
content = input('>>>输入内容:')
udpSocket.sendto(content.encode('gb2312'), Addr)
def main():
udpSocket = socket(AF_INET, SOCK_DGRAM)
ip = input("请输入目标IP地址:")
MuBiaoDuanKou = int(input("请输入目标端口:"))
#BenDiDuanKou = int(input("请输入本地端口:"))
Addr = (ip, MuBiaoDuanKou)
#bindAddr = socket.bind('', BenDiDuanKou)
t1 = Thread(target=GetData,args=(udpSocket,))
t2 = Thread(target=PostData,args=(udpSocket,Addr))
t2.start()
t1.start()
t2.join()
t1.join()
if name == 'main':
main()
》》》》运行时报错:
Traceback (most recent call last):
File "D:\PyCharm\Py Interpreter\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "D:\PyCharm\Py Interpreter\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "E:/Py Code/test/test.py", line 7, in GetData
recv_content = udpSocket.recvfrom(X)
OSError: [WinError 10022] 提供了一个无效的参数。
你这代码贴的...真没有回答的欲望,粘贴到编辑器还要重新排格式
1 from socket import * 2 from threading import Thread 3 4 5 def GetData(udpSocket): 6 while True: 7 recv_content = udpSocket.recvfrom(1024) 8 print(">>>:%s + %s" % (recv_content[0], recv_content[1])) 9 10 11 def PostData(udpSocket, Addr): 12 while True: 13 content = input('>>>输入内容:') 14 udpSocket.sendto(content.encode('gb2312'), Addr) 15 16 17 def main(): 18 udpSocket = socket(AF_INET, SOCK_DGRAM) 19 ip = input("请输入目标IP地址:") 20 MuBiaoDuanKou = int(input("请输入目标端口:")) 21 #BenDiDuanKou = int(input("请输入本地端口:")) 22 Addr = (ip, MuBiaoDuanKou) 23 #bindAddr = socket.bind('', BenDiDuanKou) 24 t1 = Thread(target=GetData,args=(udpSocket,)) 25 t2 = Thread(target=PostData,args=(udpSocket,Addr)) 26 27 t2.start() 28 t1.start() 29 30 t2.join() 31 t1.join() 32 33 if __name__ == '__main__': 34 main()
@A-handsome-cxy:
》》》》运行时报错:
Traceback (most recent call last):
File "D:\PyCharm\Py Interpreter\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "D:\PyCharm\Py Interpreter\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "E:/Py Code/test/test.py", line 7, in GetData
recv_content = udpSocket.recvfrom(X)
OSError: [WinError 10022] 提供了一个无效的参数。
看倒数第二行,udpSocket.recvfrom(X) 你代码提供的是1024 这个参数是不对的,具体提交什么类型、什么格式的参数,看要求
@会发光: 我开始以为是传参的问题,因此我加了一个参数X,后面依然不对,我就直接写的1024
他的报错是这样的:
Traceback (most recent call last): File "D:\PyCharm\Py Interpreter\lib\threading.py", line 917, in _bootstrap_inner self.run() File "D:\PyCharm\Py Interpreter\lib\threading.py", line 865, in run self._target(*self._args, **self._kwargs) File "E:/Py Code/test/test.py", line 7, in GetData recv_content = udpSocket.recvfrom(1024) OSError: [WinError 10022] 提供了一个无效的参数。
但是我看的教程就是传的1024并且我之前写的时候也没错啊
纯文本代码!
1 from socket import * 2 from threading import Thread 3 4 5 def GetData(udpSocket): 6 while True: 7 recv_content = udpSocket.recvfrom(1024) 8 print(">>>:%s + %s" % (recv_content[0], recv_content[1])) 9 10 11 def PostData(udpSocket, Addr): 12 while True: 13 content = input('>>>输入内容:') 14 udpSocket.sendto(content.encode('gb2312'), Addr) 15 16 17 def main(): 18 udpSocket = socket(AF_INET, SOCK_DGRAM) 19 ip = input("请输入目标IP地址:") 20 MuBiaoDuanKou = int(input("请输入目标端口:")) 21 #BenDiDuanKou = int(input("请输入本地端口:")) 22 Addr = (ip, MuBiaoDuanKou) 23 #bindAddr = socket.bind('', BenDiDuanKou) 24 t1 = Thread(target=GetData,args=(udpSocket,)) 25 t2 = Thread(target=PostData,args=(udpSocket,Addr)) 26 27 t2.start() 28 t1.start() 29 30 t2.join() 31 t1.join() 32 33 if __name__ == '__main__': 34 main()
1 》》》输出: 2 3 4 Traceback (most recent call last): 5 File "D:\PyCharm\Py Interpreter\lib\threading.py", line 917, in _bootstrap_inner 6 self.run() 7 File "D:\PyCharm\Py Interpreter\lib\threading.py", line 865, in run 8 self._target(*self._args, **self._kwargs) 9 File "E:/Py Code/test/test.py", line 7, in GetData 10 recv_content = udpSocket.recvfrom(1024) 11 OSError: [WinError 10022] 提供了一个无效的参数。