Send Binary Data via winsock

hey, guys...im trying to send a binary data via winsock...the problem is im not receiving all the binary, like when im trying to send a jpeg picture, im only getting half of the image and sometimes just blank image

if someone could help me out with my code...thank you so much

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void _send(char* fileTosend)
{
	FILE *file;
	char *buffer;
	unsigned long fileLen;

	file = fopen(fileTosend, "rb");
	if (!file)
	{
		printf("%s\r\n", "File not found");
	}

	fseek(file, 0, SEEK_END);
	fileLen=ftell(file);
	fseek(file, 0, SEEK_SET);

	buffer = new char[fileLen];

	fread(buffer, fileLen, 1, file);
	char size[MAX_PATH];
	sprintf(size, "%i", fileLen);

	fclose(file);
	send(sConnect, size, MAX_PATH, 0); //send file size
	
	send(sConnect, buffer, fileLen, 0); //send binary
	free(buffer);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void _recv(char* fileToRcv) 
{
	int dwSize;
        char* buffer = new char[1024];

	if(recv(sConnect, (char*)buffer, 1024, 0)) //receive file size
	{
		dwSize = atoi((const char *)buffer);
		printf("File Size: %i\r\n", dwSize);
	}
	char* ibuffer = new char[dwSize];

	if(recv(sConnect, (char*)ibuffer, dwSize, 0))//recv binary
	{
		FILE* pfile;
		pfile = fopen(fileToRcv, "wb");
		fwrite((const char*)ibuffer, 1, dwSize, pfile);
		fclose(pfile);
	}
}
the problem is im not receiving all the binary

That's exactly the problem. You stop receiving before all data has arrived - you have to call recv until there is no data left.
Topic archived. No new replies allowed.