Home > Back-end >  TCP transport large files
TCP transport large files

Time:10-12

Today I had written a TCP transport document procedures, with a small file and image tried, works, when transferring large files, programs are somehow died, a great god answers,
The receiver code is as follows:
 
The SOCKET sockSrv=SOCKET (AF_INET, SOCK_STREAM, 0);
If (sockSrv==INVALID_SOCKET)
{
cout<" Failed the socket & lt;" return 0;
}
//do not bind.. The dynamic
SOCKADDR_IN addrSvr;
AddrSvr. Sin_family=AF_INET;
AddrSvr. Sin_port=htons (8001);
AddrSvr. Sin_addr. S_un. S_addr=inet_addr (IP);

If (connect (sockSrv, (SOCKADDR *) & amp; AddrSvr, sizeof (addrSvr))==1)
{
cout<" TCP connection failure!!!!! "& lt; return 0;
}

//the receive
Char buff [200].
Char filename [256].
Int nRecv=1;

Memset (filename, 0, sizeof (filename));
NRecv=recv (sockSrv, filename, sizeof (filename), 0).

cout<" Receive files: "& lt;
The FILE * fp.
Fp=fopen (filename, "wb");
If (fp==NULL)
{
cout<" Failed to create file "& lt; return 0;
}

While (1)
{
Memset (buff, 0, sizeof (buff));
NRecv=1;
NRecv=recv (sockSrv, buff, sizeof (buff) - 1, 0).

If (nRecv & lt;=0)
break;
coutFwrite (buff, nRecv, 1, fp);


}
cout<" Receive complete!!!!! "& lt;
The fclose (fp);
Closesocket (sockSrv);

The sender code is as follows:
 
The SOCKET sockSrv=SOCKET (AF_INET, SOCK_STREAM, 0);
If (sockSrv==INVALID_SOCKET)
{
cout<" Failed the socket & lt;" return 0;
}
//do not bind.. The dynamic
SOCKADDR_IN addrSvr addrClient;
Int len=sizeof (addrClient);
AddrSvr. Sin_family=AF_INET;
AddrSvr. Sin_port=htons (8001);
AddrSvr. Sin_addr. S_un. S_addr=INADDR_ANY;
//binding IP and port
If (bind (sockSrv, (sockaddr *) & amp; AddrSvr, sizeof (addrSvr))==SOCKET_ERROR)
{
cout <"TCP binding IP and port failure!" return 0;
}

If (listen (sockSrv, 1)==SOCKET_ERROR)
{
cout <"TCP listener port failure!" return 0;
}

//the receive
Char buff [200].
Char filename [256].
Int nRead;
The SOCKET sockConn;

Cin> The filename.

SockConn=accept (sockSrv, (SOCKADDR *) & amp; AddrClient, & amp; Len);

Int err=send (sockConn, filename, strlen (filename), 0).
cout
The FILE * fp.
Fp=fopen (filename, "rb");

While (1)
{
If (feof (fp))
break;
NRead=1;
Memset (buff, 0, sizeof (buff));
NRead=fread (buff, 1, sizeof (buff) - 1, fp);

coutNRead=send (sockConn, buff, nRead, 0);

}

cout<" Send complete!!!!! "& lt; The fclose (fp);
Closesocket (sockConn);
Closesocket (sockSrv);

CodePudding user response:

The buffer is too small?

CodePudding user response:

Is the receiving end died or the sender died or were dead, what documents began to die, all want to be clear,

CodePudding user response:

Two are dead, but the file size is also changing

CodePudding user response:

How to don't TCPClient, write their own to see how much each transmission K?

CodePudding user response:

This has nothing to do with your program should be, may be the operating system to read and write the buffer caused while your program is not responding, but the thread or continue to run, I think when you use the file read by the seek function relatively fixed size buffers, and then by several times to send and receive acne will be safe,

CodePudding user response:

Oh files can be successful but program died

CodePudding user response:

reference 5 floor zdrone reply:
this has nothing to do with your program should be, may be the operating system to read and write the buffer caused while your program is not responding, but the thread or continue to run, I think when you use the file read by the seek function relatively fixed size buffers, and then by several times to send and receive acne will be safe,

Thank you should be the problem

CodePudding user response:

Program is not dead, is the main thread blocked, open another thread is responsible for the transmission, the main thread maintain interface, will not "die"
  • Related