Stream file shutdown sequence issues during socket programming

Source: Internet
Author: User

Program Fragment:

int sock_fd; File *sock_fpo, *sock_fpi, *pipe_fp;sock_fd = accept (Sock_id, NULL, NULL);         if  (sock_fd == -1 )              oops ("accept");         if  (Sock_fpi = fdopen (sock_fd,  "R")  == null)              oops ("fdopen reading");         if  (fgets (DIRNAME, BUFSIZ - 5, SOCK_FPI)  == null)             oops ("Reading dirname");         if  ((Sock_fpo = fdopen (sock_fd,  "w"))  ==  null)             oops ("fdopen writing") ;         if  ((Pipe_fp = popen (command,  "R"))  == NULL)              oops ("Popen");         while  ((C = getc (PIPE_FP))  != eof)              PUTC (C, SOCK_FPO);         pclose (PIPE_FP);         pclose (SOCK_FPO);         pclose (SOCK_FPI);

This program fragment is a server socket fragment that was found during debugging. When I will:

Pclose (SOCK_FPO); Pclose (SOCK_FPI);

Change the order to:

Pclose (SOCK_FPI); Pclose (SOCK_FPO);

The data cannot be written to the socket correctly and passed to the client.

Summarize:

because: PIPE_FP, Sock_fpo, SOCK_FPI's open order is SOCK_FPI,SOCK_FPO,PIPE_FP.

Be sure to close the descriptor in reverse order, opening it first and then closing it. namely: PIPE_FP, Sock_fpo, SOCK_FPI.

Stream file shutdown sequence issues during socket programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.