Home > other >  Writing process pool had a problem
Writing process pool had a problem

Time:09-20

In the writing process encountered a problem when I pool


By the client to download large files from the server, because the service client and network speed different reasons

Client to use cycle recv receive large files.

If you don't use cycle recv, the client will hang up, so the service side because the send after disconnection, at the end of the system with SIGPIPE signal process

The end of the process


The question is: why is the client may hang up... Always can't find the answer online


CodePudding user response:

Protocol type
Typedef struct//established between the client and the server data structure of file transfer, prevent sticking package
{
Int trans_lenth;
Char buf [1000].
} trans_t;



Send a file from the server code
# include "process_pool. H"

Int trans_file (int client_fd, char * FILENAME)
{
Trans_t trans;

//send a file name
//needs to be converted to network byte order
Trans. Trans_lenth=strlen (FILENAME);
Strcpy (trans. Buf, FILENAME);
Int ret=send (client_fd, & amp; Trans, 4 + trans. Trans_lenth, 0);
ERROR_CHECK (1, ret, "send");

Int trans_fd=open (FILENAME, O_RDONLY);
ERROR_CHECK (1, trans_fd, "open");
//read the disconnect, the system will send the child to sigpipe signal, we ignore it
Signal (SIGPIPE, SIG_IGN);
While ((trans. Trans_lenth=read (trans_fd, trans. Buf, sizeof (trans. Buf))))
{
If (trans. Trans_lenth & gt; 0)
{
Ret=send (client_fd, & amp; Trans, 4 + trans. Trans_lenth, 0);
//if (1==ret)
//{
//goto the end;
//}
} the else
{
Send (client_fd, & amp; Trans, 4 + trans. Trans_lenth, 0);
break;
}
}
//end:
Close (trans_fd);
Close (client_fd);
return 0;
}

The client receives the file code
# include "process_pool. H"
Int main (int arg c, char * * argv)
{
ARGS_CHECK (3, arg c);
//AF_INET said this server is the ipv4 type
//SOCK_STREAM said the server follows the TCP protocol
Int socketFd=socket (AF_INET SOCK_STREAM, 0).
ERROR_CHECK (socketFd, 1, "socket");
Struct sockaddr_in client_addr_info;
Bzero (& amp; Client_addr_info, sizeof (client_addr_info));
Client_addr_info. Sin_family=AF_INET;
Client_addr_info. Sin_addr. S_addr=inet_addr (argv [1]).
Client_addr_info. Sin_port=htons (atoi (argv [2]));

Int ret=connect (socketFd, (struct sockaddr *) & amp; Client_addr_info, sizeof (client_addr_info));
ERROR_CHECK (ret, 1, "connect");

Int lenth.
Char buf [1000]={0};
//get the server name, buf need to be initialized before use
The recv (socketFd, & amp; Lenth, sizeof (int), 0);
The recv (socketFd, buf, lenth, 0);

Int down_fd=open (buf, O_CREAT | O_WRONLY, 0666);
ERROR_CHECK (1, down_fd, "open");
While (1)
{

The recv (socketFd, & amp; Lenth, sizeof (int), 0);
If (0==lenth)
{
Printf (" the download is complete \ n ");
break;
} the else
{
The recv (socketFd, buf, lenth, 0);
Write (down_fd, buf, lenth);
}
}

Close (down_fd);
Close (socketFd);
return 0;
}
  • Related