Rectangle 27 2

regardless on how many writes was on client side of tcp socket and what were sizes of messages you generally don't have control on sizes of data chunks you'll get on serever's 'data' handler (tcp only guarantees they are in order). You need to frame your data manually (simplest way is to prefix with frame size and buffer incoming data)

I decided to try and get my python code to talk with the socket.io server, rather than the raw server. Thanks for pointing me in the right direction

Sometimes garbled results sending base64 encoded images over TCP socke...

python sockets node.js tcp websocket
Rectangle 27 0

The data you received is not junk. It is the send data. But it is send in bytes. So, at the receiving end, you need to convert the bytes in to string before processing the data. Read this for better understanding of sending and receiving data in socket programming and read this for methods used in current version of opencv.

import cv2

The following is a working code though it's communication is simplified.

#!/usr/bin/python
import socket
import cv2
import numpy

def recvall(sock, count):
    buf = b''
    while count:
        newbuf = sock.recv(count)
        if not newbuf: return None
        buf += newbuf
        count -= len(newbuf)
    return buf

TCP_IP = 'localhost'
TCP_PORT = 5001

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(True)
conn, addr = s.accept()

length = recvall(conn,16)
stringData = recvall(conn, int(length))
data = numpy.fromstring(stringData, dtype='uint8')
s.close()

decimg=cv2.imdecode(data,1)
cv2.imshow('SERVER',decimg)
cv2.waitKey(0)
cv2.destroyAllWindows()

python - Image send via TCP - Stack Overflow

python opencv tcp raspberry-pi
Rectangle 27 0

regardless on how many writes was on client side of tcp socket and what were sizes of messages you generally don't have control on sizes of data chunks you'll get on serever's 'data' handler (tcp only guarantees they are in order). You need to frame your data manually (simplest way is to prefix with frame size and buffer incoming data)

I decided to try and get my python code to talk with the socket.io server, rather than the raw server. Thanks for pointing me in the right direction

Sometimes garbled results sending base64 encoded images over TCP socke...

python sockets node.js tcp websocket
Rectangle 27 0

By default the Python function open opens file in text mode, meaning it will handle all input/output as text, while an image is decidedly binary.

A file in text mode will do thing like translating newline sequences (which are different on different systems). That means the data you read will be corrupted.

To open a file in binary mode, then append 'b' to the mode flags, like e.g.

f = open("ornekresim.png", "rb")  # <-- Note the 'b' in the mode

However, with your code this leads to another problem, namely that you can't use readline anymore. Not that it made much sense anyway, reading binary data as lines since there are no "lines" in binary data.

You have to use the read function instead.

sending images using python tcp socket - Stack Overflow

python-2.7 tcp network-programming python-sockets
Rectangle 27 0

Since you're dealing with images, which are binary files, you need to open the files in binary mode.

open(filename, 'rb')
open()

The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that dont treat binary and text files differently, where it serves as documentation.)

Since your server is running on Windows, as you read the file, Python is converting every \r\n it sees to \n. For text files, this is nice: You can write platform-independent code that only deals with \n characters. For binary files, this completely corrupts your data. That's why it's important to use 'b' when dealing with binary files, but also important to leave it off when dealing with text files.

Also, as TCP is a stream protocol, it's better to stream the data into the socket in smaller pieces. This avoids the need to read an entire file into memory, which will keep your memory usage down. Like this:

with open(filename, 'rb') as f:
    while True:
        data = f.read(4096)
        if len(data) == 0:
            break
        tcpCliSock.send(data)

Is there a reason NOT to ALWAYS use binary mode? Will that create a issue for any text?

Python server is sending truncated images over TCP - Stack Overflow

python tcp server
Rectangle 27 0

import socket

if __name__ == "__main__":
    sock = socket.socket()
    sock.connect(("localhost", 50839))

    with open("data.bin", "rb") as fd:
        buf = fd.read(1024)
        while (buf):
            sock.send(buf)
            buf = fd.read(1024)
    sock.close()

For practical reasons, you can treat str objects (the result of fd.read) as raw data, you don't need any further crazy encoding. Just iterate the file and send over the socket. Test by running this server which just echoes to stdout with python server.py > data2.bin:

import socket
import sys

if __name__ == "__main__":
    sock = socket.socket()
    sock.bind(("localhost", 50839))
    sock.listen(1)

    client, address = sock.accept()

    buf = client.recv(1024)
    while (buf):
        sys.stdout.write(buf)
        buf = client.recv(1024)

    client.close()
    sock.close()

A checksum shows the binary data is sent correctly:

% md5 data.bin data2.bin      
MD5 (data.bin) = 8b3280072275badf3e53a6f7aae0b8be
MD5 (data2.bin) = 8b3280072275badf3e53a6f7aae0b8be

Your C# server should be able to accept this data as is. If it doesn't work, it's because your server is expecting something in particular, not just raw data.

I am trying your method of sending the data, I do have to have a number sent right before I send the data, but I took that part out just to get this data sending. I have the c# server that stores the image data into a memorystream right before receiving the next set of 1024 bytes. But I am still getting a format error in the image class, my whole reason for finding a different way of encoding the data.

I updated the original question to contain the server code

Whats the purpose of "counter" in the C# server code? It's not used anywhere. All in all, the code seems analogous to the Python server I posted, so it's not immediately obvious what's wrong. Maybe there's a bug on the way you write the file? Have you tried writing to stdout instead and testing like I did to confirm the right data is sent over the wire?

using python to send an image over TCP to c# server - Stack Overflow

c# python image encoding tcp