Home > other >  TCP communications receiving data incomplete solution
TCP communications receiving data incomplete solution

Time:09-23

Write a download function in TCP, the text files downloaded 0 KB, exe file lost part of the larger cause can't run,,,

The service side
 DWORD WINAPI CreateClientThread (LPVOID lpParameter)//in this function complete received a path to the file and send the file 
{
The SOCKET socketServer=(SOCKET) lpParameter;
Char buf_msg [BUFSIZ];
Int ret=0;
Int sresult=0;
FileHelper fh.
Do
{
Memset (buf_msg, 0, BUFSIZ);
Ret=recv (socketServer buf_msg, BUFSIZ, 0).//received file name
If (ret & gt; 0)
{
If (STRCMP (buf_msg, "exit")==0)
{
Cout & lt; <"The user requests close connection" & lt; break;
}
Cout & lt; Char recName [BUFSIZ];
Strcpy (recName buf_msg);
The FILE * f=fh. Selectfile (recName);//the path_buffer got the full path to the E: \ 1. PDF, and f refers to the file
Char * name=fh. GetFileName ();//the name got the documents and suffix 1. PDF
Char sendName [BUFSIZ];
Strcpy (sendName, name);
Send (socketServer sendName, BUFSIZ, 0).//send is 1. PDF
F=fopen (recName, "rb");//file opened in binary mode
If (f==NULL)
{
Cout & lt; <"Cannot open file" & lt; The fclose (f);
system("pause");
exit(0);
}
The else
{
//transfer files
//cycle to send data until the end file
Int nCount;
//long long sum=0;
While ((nCount=fread (sendName, 1, BUFSIZ, f)) & gt; 0)
{
//cout<(the sum a +=nCount) & lt; Send (socketServer sendName, nCount, 0).
}

}
}
Else if (ret==0)
{
Cout & lt; <"Connection has been closed... "& lt; }
The else
{
Cerr & lt; <"Receive client information failure" & lt; <: : GetLastError () & lt; <"\ n";
Closesocket (socketServer);
system("pause");
return 1;

}


} while (ret & gt; 0);
Ret=shutdown (socketServer SD_SEND);
If (ret==SOCKET_ERROR)
{
Cerr & lt; <"Unable to close the socket & lt;" <: : GetLastError () & lt; <"\ n";
Closesocket (socketServer);
system("pause");
return 1;
}
//delete & amp; Fh.//recycling memory
//fh=NULL;
return 0;
}


The client
 void Client: : ConnectToServer () 
{
//connection
FileHelper fh.
Int fail=connect (socketClient, (SOCKADDR *) & amp; AddrClient, len);
If (fail)
{
Cout & lt; <"Connected to the server failed! Program will exit... "& lt; system("pause");
exit(1);
}
Cout & lt; <"Please input you want to transfer the file name:";
Cin & gt;> The Filename.
//send the address to server
Send (socketClient, Filename, BUFSIZ, 0).

//to accept the file name from the server: 1. TXT
Char revName [BUFSIZ];
The recv (socketClient, revName, BUFSIZ, 0);
Char fromname [BUFSIZ]={};
Strcpy (fromname, revName);//fromname: 1. TXT

Char * dirName=GetDirName ();//to begin to choose a place to build a folder, and then began to transmit content
Fh. CreateDir (dirName);
//cout<& lt;" Folder to create success!" ;
//in dirName directory to create a and server in the same file
Char mid [3]="\ ";
Char finame [MAX_PATH]={};
Strcat (finame dirName);
Strcat (finame, mid);
Strcat (finame fromname);
The FILE * f=fh. CreateFile (finame);

//transfer files
Char buffer [BUFSIZ]="";//file buffer
Int nCount;
While ((nCount=recv (socketClient, buffer, BUFSIZ, 0)) & gt; 0)
{
Fwrite (buffer, nCount, 1, f);
}
//Thread. Sleep (10);
//coutThe fclose (f);
}


Unexpected output screenshots

CodePudding user response:

Top here and hope to solve some bosses

CodePudding user response:

Doubt is the problem of fwrite function, general rigorous approach is to judge fwrite returns a value, some are successful to write without successful fwrite once again

CodePudding user response:

Many problems, recName uninitialized variable, send no judgment, the return value

CodePudding user response:

File format is a binary open, write is what format, can't see
  • Related