无法通过套接字发送字节:消息太长

时间:2018-11-29 01:38:00

标签: python sockets ubuntu-14.04

使用以下命令解决: Python Socket Receive Large Amount of Data

我有服务器和客户端。我尝试通过套接字从客户端到服务器发送字节。但是,当它实际上尝试发送字节时,它失败并显示错误:socket.error errno 90 message too long。有什么办法可以克服这个错误?

客户:

from socket import socket, AF_INET, SOCK_DGRAM
import pyscreenshot as ImageGrab

SERVER = 'xxxx.xxxx.xxxx.xxxx'
PORT = 5000

mySocket = socket(AF_INET, SOCK_DGRAM)

while True:
    im = ImageGrab.grab(bbox=(0,27,320,266))
    im.save('test.png')
    myfile = open('test.png')
    bytes = myfile.read()
    mySocket.sendto(bytes, (SERVER, PORT))
    myfile.close()

服务器:

from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import cv2
from PIL import Image
import io

PORT_NUMBER = 5000
SIZE = 150000

hostName = gethostbyname( '0.0.0.0' )

mySocket = socket(AF_INET, SOCK_DGRAM)
mySocket.bind((hostName, PORT_NUMBER))

while True:
    (data, addr) = mySocket.recvfrom(SIZE)
    image = Image.open(io.BytesIO(data))
    image.save('test.png')
    imga = cv2.imread('test.png', 0)
sys.exit()

所以现在我使用TCP连接。

新客户:

from socket import socket, AF_INET, SOCK_STREAM
import pyscreenshot as ImageGrab

SERVER = 'xxxx.xxxx.xxxx.xxxx'
PORT = 5000

mySocket = socket(AF_INET, SOCK_STREAM)
mySocket.connect((SERVER, PORT))

while True:
    im = ImageGrab.grab(bbox=(0,27,320,266))
    im.save('test.png')
    myfile = open('test.png')
    bytes = myfile.read()
    mySocket.sendall(bytes)
    myfile.close()

新服务器:

from socket import socket, gethostbyname, AF_INET, SOCK_STREAM
import cv2
from PIL import Image
import io
import time

PORT_NUMBER = 5000
SIZE = 150000

hostName = gethostbyname( '0.0.0.0' )

mySocket = socket(AF_INET, SOCK_STREAM)
mySocket.bind((hostName, PORT_NUMBER))
mySocket.listen(1)
conn, addr = mySocket.accept()

print("Test server listening on port {0}\n".format(PORT_NUMBER))

while True:
    data = conn.recv(SIZE)
    last_time = time.time()
    image = Image.open(io.BytesIO(data))
    image.save('test.png')
    imga = cv2.imread('test.png', 0)
    print('loop took {} seconds'.format(time.time() - last_time))
sys.exit()

因此,当我尝试同时运行两者时,出现以下错误: (重要:当我通过套接字发送少量字节时,它可以成功发送,但是当字节很大时,会发生以下错误

C:\Users\Tal\test\Scripts\python.exe 

C:/Users/Tal/PycharmProjects/test/tester.py
Test server listening on port 5000

Traceback (most recent call last):
  File "C:\Users\Tal\test\lib\site-packages\PIL\ImageFile.py", line 221, in load
    s = read(self.decodermaxblock)
  File "C:\Users\Tal\test\lib\site-packages\PIL\PngImagePlugin.py", line 621, in load_read
    cid, pos, length = self.png.read()
  File "C:\Users\Tal\test\lib\site-packages\PIL\PngImagePlugin.py", line 115, in read
    length = i32(s)
  File "C:\Users\Tal\test\lib\site-packages\PIL\_binary.py", line 77, in i32be
    return unpack_from(">I", c, o)[0]
struct.error: unpack_from requires a buffer of at least 4 bytes

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Tal/PycharmProjects/test/tester.py", line 23, in <module>
    image.save('test.png')
  File "C:\Users\Tal\test\lib\site-packages\PIL\Image.py", line 1935, in save
    self.load()
  File "C:\Users\Tal\test\lib\site-packages\PIL\ImageFile.py", line 227, in load
    raise IOError("image file is truncated")
OSError: image file is truncated

Process finished with exit code 1

0 个答案:

没有答案