Click to See Complete Forum and Search --> : Help With File Tranferring Function in C


hayyan85
August 20th, 2009, 03:24 PM
Hi , I used to program sockets using the winsock component in Visual Basic which was extremely easy ... anyways I have started socket programming in C (not so easy :( ) and I am stuck on the file transfer function.The code which I used (given below for server and client) receives and transfers all type of files except .txt File. I dont Know Why.. Thats the problem I would really appreciate it.. if any of you guys could let me know why the code is not working when I select a text file . Thanks again.


Process: The client is trying to send a file named telnet.exe to the server


Client Code:
--------------------------------------------------------------------------------------------------------------------
#define CHUNK 256
char buff[CHUNK];
int bytes_read;
FILE *fp = fopen("c:\\telnet.exe", "rb");
while(!feof(fp))
{
if ( (bytes_read = fread(&buff, sizeof(char), CHUNK , fp)) > 0 )
{
//SEND FILE
send(myclient, buff, bytes_read, 0);
}
}
return -1 ;
-------------------------------------------------------------------------------------------------------------------

Server Code: (The Receining Part is in a different thread)


DWORD WINAPI File_Receive(LPVOID LPDATA)
{
char *myptr;
myptr = mybuff; //No need for this but still I am dumb


cout << "Data Receive - Thread Started" << "\n";
FILE *fp1 = fopen("c:\\TelnetCopy.exe", "wb");
while(1)
{
long myno = recv(client,mybuff,sizeof(mybuff),0);
//cout << myno;
if (myno != -1 && myno > 0)
{
//add a null to received File
// mybuff[1023]='\0';
fwrite(myptr, sizeof(char), recv, fp1);
//cout << mybuff;
///cin.get();
}

}

}
-------------------------------------------------------------------------------------------------------------------
could you plz tell me why doesnt my server receive a text file while it can receive all other files I would really appreciate it. Thank You

hayyan85
August 22nd, 2009, 09:48 AM
Just solved my problem ... Thanks again