EDN Admin
Well-known member
hi,
im having a problem in sending a bitmap image using winsock, problem is sometimes im not receiving the whole image and sometimes only a blank image.
i dont know if its a problem in sending the file or receiving it cuz im totally new to winsock
here is my code:
<pre style="font-size:12px; line-height:normal
[/code]
void _send(char* fileTosend)<br/>
{<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere FILE *file;<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere char *buffer;<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere unsigned long fileLen;<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere file = fopen(fileTosend, "rb");<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere if (!file)<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere {<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere printf("%srn", "File not found");<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere }<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere fseek(file, 0, SEEK_END);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere fileLen=ftell(file);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere fseek(file, 0, SEEK_SET);<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere buffer = new char[fileLen];<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere fread(buffer, fileLen, 1, file);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere char size[MAX_PATH];<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere sprintf(size, "%i", fileLen);<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere fclose(file);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere send(sConnect, size, MAX_PATH, 0); //send file size<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere <br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere send(sConnect, buffer, fileLen, 0); //send binary<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere free(buffer);<br/>
}<br/>
<br/>
void _recv(char* fileToRcv) <br/>
{<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere int dwSize;<br/>
char* buffer = new char[1024];<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere if(recv(sConnect, (char*)buffer, 1024, 0)) //receive file size<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere {<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere dwSize = atoi((const char *)buffer);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere printf("File Size: %irn", dwSize);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere }<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere char* ibuffer = new char[dwSize];<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere if(recv(sConnect, (char*)ibuffer, dwSize, 0))//recv binary<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere {<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere FILE* pfile;<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere pfile = fopen(fileToRcv, "wb");<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere fwrite((const char*)ibuffer, 1, dwSize, pfile);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere fclose(pfile);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere }<br/>
}
<pre style="font-size:12px; line-height:normal
[/code]
ok so i tried on receiving the file...
<pre style="font-size:12px; line-height:normal
[/code]
when(1)<br/>
<br/>
{<br/>
<br/>
byte_rcv = recv(sConnect, char* )ibuffer, dwSize, 0)<br/>
fwrite((const char*)ibuffer, 1, dwSize, pfile);<br/>
if(byte_rcv == 0)<br/>
<br/>
break;<br/>
<br/>
}
<pre style="font-size:12px; line-height:normal
[/code]
not working for me and so as many code snippets on the web that i found (frustrated),,
i also noticed that when after i didnt received the whole image of the picture, there are these ascii text left in my program screen and my program will then stop working
so could it also be that it received all the binary data but just didnt write it all in the file ?
hope someone can help me..thanks in advance
<br/>
<br/>
<br/>
View the full article
im having a problem in sending a bitmap image using winsock, problem is sometimes im not receiving the whole image and sometimes only a blank image.
i dont know if its a problem in sending the file or receiving it cuz im totally new to winsock
here is my code:
<pre style="font-size:12px; line-height:normal
Code:
void _send(char* fileTosend)<br/>
{<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere FILE *file;<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere char *buffer;<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere unsigned long fileLen;<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere file = fopen(fileTosend, "rb");<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere if (!file)<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere {<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere printf("%srn", "File not found");<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere }<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere fseek(file, 0, SEEK_END);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere fileLen=ftell(file);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere fseek(file, 0, SEEK_SET);<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere buffer = new char[fileLen];<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere fread(buffer, fileLen, 1, file);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere char size[MAX_PATH];<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere sprintf(size, "%i", fileLen);<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere fclose(file);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere send(sConnect, size, MAX_PATH, 0); //send file size<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere <br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere send(sConnect, buffer, fileLen, 0); //send binary<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere free(buffer);<br/>
}<br/>
<br/>
void _recv(char* fileToRcv) <br/>
{<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere int dwSize;<br/>
char* buffer = new char[1024];<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere if(recv(sConnect, (char*)buffer, 1024, 0)) //receive file size<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere {<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere dwSize = atoi((const char *)buffer);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere printf("File Size: %irn", dwSize);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere }<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere char* ibuffer = new char[dwSize];<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere if(recv(sConnect, (char*)ibuffer, dwSize, 0))//recv binary<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere {<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere FILE* pfile;<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere pfile = fopen(fileToRcv, "wb");<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere fwrite((const char*)ibuffer, 1, dwSize, pfile);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere fclose(pfile);<br/>
<span class="x_x_x_Apple-tab-span" style="white-spacere }<br/>
}
<pre style="font-size:12px; line-height:normal
Code:
ok so i tried on receiving the file...
<pre style="font-size:12px; line-height:normal
Code:
int byte_rcv = 0;
when(1)<br/>
<br/>
{<br/>
<br/>
byte_rcv = recv(sConnect, char* )ibuffer, dwSize, 0)<br/>
fwrite((const char*)ibuffer, 1, dwSize, pfile);<br/>
if(byte_rcv == 0)<br/>
<br/>
break;<br/>
<br/>
}
<pre style="font-size:12px; line-height:normal
Code:
fclose(pfile);
not working for me and so as many code snippets on the web that i found (frustrated),,
i also noticed that when after i didnt received the whole image of the picture, there are these ascii text left in my program screen and my program will then stop working
so could it also be that it received all the binary data but just didnt write it all in the file ?
hope someone can help me..thanks in advance
<br/>
<br/>
<br/>
View the full article