So, as a socket programmer, you would recv on a loop until those 4 bytes are read. Some protocols, like HTTP, will use delimiters (in HTTP's case \r\n\r\n) to signal when a packet of data has ended. The server will know to read in that value, then continue reading until that value is satisfied. You covered that by sending the length of the file first. One of the trickier parts to socket programming is knowing how much data you will need to receive. So, when folks who have worked with file I/O move to socket I/O usually end up confused why things aren't sending or receiving correctly. If you want to write 100 bytes to a file, those 100 bytes are guaranteed to be written if the method doesn't fail. Again, it's up to you to use that return value and compute what still needs to be received.įile I/O is completely different. If you write recv(buffer with 100 bytes) because you are expecting 100 bytes, it could only grab 25 bytes, or 99 bytes, or fail out completely. It's up to you to take the return value and compute what needs to still be sent. It could send 25 bytes, or 99 bytes, or fail out completely. Just because you write send(buffer with 100 bytes) doesn't mean it's going to send 100 bytes. The one main point that should be taken away from the comments below your question is that send and recv are fickle.
#Socket winsock code#
I decided to use MSG_WAITALL because I guess that is suitable for this case, please correct my code for recieving/sending and if possible refactor it, it would be nicer if it would be with explainations, so that evrybody could learn to code better, thanks))) Int err = send(client, (char*)fsize, sizeof(int), 0) Īll values for both sides are initialised, and error handling is done well, and if I had problem then I would have said about that. Int err = recv(conn, (char*)&filesize, sizeof(filesize), 0) Įrr = recv(conn, buffer, filesize, MSG_WAITALL) Ĭlient ifstream file("a.txt", ios::binary)
#Socket winsock iso#
I want to send files over TCP sockets in C++ on Windows, all is working absolutely fine, however I can't send big files like this, I understand that TCP as any protocol has it's limitations, like I can't send more than 64KB per packet, my method works for small file sizes(tested all up to 12KB), but I would like to send LARGE files, like iso image of ubuntu or windows, which are surely bigger than 12 fully packed packets and etc.