Click here to Skip to main content
15,885,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I may have asked the same question but in c++ not too long ago, sorry if I'm a bit noisy.

so I got two function to send and receive large data from tcpsockets in ruby but I need help debugging.

the problem is I tested these methods on an image file and it tends to be dark or pixitlated on the bottom when written on the other side. how can I fix this issue?

What I have tried:

Send Function:
Ruby
def split_to_chunks(string, bytesize)
	arr = []
	until string.empty?
		arr << string.slice!(0..bytesize)
	end
	return arr
end

def sockSend(sock, message)
	
	chunk_size = 1024

	messageChunks = split_to_chunks(message, chunk_size)

	messageChunks.each { |chunk|

		sleep(0.3)
		sock.puts chunk

	}

	sock.puts "<END_OF_SOCKET_DATA>"

end
note:
there a functions called "split_to_chunks" that just splits a string to chunks, I included it above.

Receive Function:
Ruby
def sockRead(sock)

	full_result = ""
	chunk = ""
	chunk_size = 1024

	while true do
		chunk = sock.recv(chunk_size)
		if chunk.chomp == "<END_OF_SOCKET_DATA>" then
			break
		end
		sleep(0.2)
		full_result = full_result + chunk
	end
	return full_result.chomp

end


thanks in advance.
Posted
Comments
Richard MacCutchan 6-Mar-21 12:32pm    
Are you sure that using string type is correct for an image file? And where do you send the "<END_OF_SOCKET_DATA>" message?
Member 14915174 6-Mar-21 13:15pm    
Hello it's me again do you know by any chance if the "socket.puts" function alone sends full (large) data given to it. thanks again.
Richard MacCutchan 7-Mar-21 3:42am    
I have been trying to find a definitive answer in the documentation but it is not easy. I did find a comment which said that puts adds a newline character (\n) to the end of the message. That would certainly affect the received data. I would suggest you use one of the raw byte send commands in future. As far as I can see they are send or sendmsg.
Member 14915174 7-Mar-21 8:28am    
thanks for searching :), I think this might have worked I can transfer image files I will look into larger files later on my own thanks for your help.
Richard MacCutchan 7-Mar-21 11:31am    
If you can successfully transfer an image file then you should be able to transfer anything. After all a file is just a stream of bytes, there is nothing inherently special about it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900