file transfer using winsock

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
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:
[/code]
void _send(char* fileTosend)<br/>
{<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre FILE *file;<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre char *buffer;<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre unsigned long fileLen;<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre file = fopen(fileTosend, "rb");<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre if (!file)<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre {<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre printf("%srn", "File not found");<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre }<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre fseek(file, 0, SEEK_END);<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre fileLen=ftell(file);<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre fseek(file, 0, SEEK_SET);<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre buffer = new char[fileLen];<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre fread(buffer, fileLen, 1, file);<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre char size[MAX_PATH];<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre sprintf(size, "%i", fileLen);<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre fclose(file);<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre send(sConnect, size, MAX_PATH, 0); //send file size<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre <br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre send(sConnect, buffer, fileLen, 0); //send binary<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre free(buffer);<br/>
}<br/>
<br/>
void _recv(char* fileToRcv) <br/>
{<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre int dwSize;<br/>
char* buffer = new char[1024];<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre if(recv(sConnect, (char*)buffer, 1024, 0)) //receive file size<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre {<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre dwSize = atoi((const char *)buffer);<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre printf("File Size: %irn", dwSize);<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre }<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre char* ibuffer = new char[dwSize];<br/>
<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre if(recv(sConnect, (char*)ibuffer, dwSize, 0))//recv binary<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre {<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre FILE* pfile;<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre pfile = fopen(fileToRcv, "wb");<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre fwrite((const char*)ibuffer, 1, dwSize, pfile);<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre fclose(pfile);<br/>
<span class="x_x_x_Apple-tab-span" style="white-space:pre }<br/>
}
<pre style="font-size:12px; line-height:normal
Code:
[/code]


ok so i tried on receiving the file...
<pre style="font-size:12px; line-height:normal
Code:
int byte_rcv = 0;
[/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:
fclose(pfile);
[/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
 
Back
Top