TCP/IP network programming I/O Stream separation semi-closed (1)

Source: Internet
Author: User

TCP/IP network programming I/O Stream separation semi-closed (1)

Theoretical Basis

Stream: After fopen is called to open a file, file read/write operations will create a stream, and Socket network communication will also create a stream. A stream is a bridge for data sending and receiving purposes, it is actually the flow of exponential data. We can understand it as the path for sending and receiving data.

I/O Stream separation: it refers to the separate processing of data sending and receiving streams, which is controlled by two different objects rather than one object. We have discussed two I/O Stream separation methods before. The first one is to call the fork function to create a sub-process, and the parent process is responsible for receiving data, the sub-process is responsible for sending data (learning note _ 11 ). Second: Two fdopen function calls are used to create the Read mode FILE pointer and write mode FILE pointer (based on Linux programming _ 1 ).

-Advantages of I/O Stream separation:

The first method of separation: 1. Separate Input and Output processes to reduce implementation difficulty (simple and easy to maintain ). 2. Output operations irrelevant to the input can increase the speed (Block function ).

Method 2: 1. Reduce implementation difficulty 2. Convert to FILE pointer FILE operations by read mode and write mode 3. the I/O buffer improves the buffer performance.

Close the separation stream in the fdopen form

Fdopen converts a socket to a FILE pointer and can operate the socket like a FILE operation. However, there is a problem with the exit and socket, that is, the server needs to be half-closed for security, in the previous chapter, fdopen is directly followed by fclose, which is actually insecure, because fclose not only closes the file stream, but also closes the socket. This principle is the same as the socket semi-close mechanism previously mentioned. How can a FILE stream be semi-closed? In fact, we can copy the original FILE descriptor before creating the FILE pointer, so that both the original FILE descriptor and the copy FILE descriptor reference the same socket, then close one of them will not destroy the socket, implement the semi-closed environment, and then call the shutdown semi-closed socket. As follows:

 

Method for copying file descriptors:

Int dup (int fildes); // copy the file descriptor fileds

Int dup2 (int fildes, int fildes2); // copy the file descriptor fildes and specify the descriptor as fildes2

Instance code

//// Main. cpp // hello_server // Created by app05 on 15-10-13. // Copyright (c) 2015 app05. All rights reserved. // # include # define BUF_SIZE 1024 void error_handling (char * message); int main (int argc, const char * argv []) {int serv_sock, clnt_sock; FILE * readfp; FILE * writefp; struct sockaddr_in serv_adr, clnt_adr; socklen_t clnt_adr_sz; char buf [BUF_SIZE] = {0,}; if (Argc! = 2) {printf ("Usage: % s \ n", argv [0]); exit (1) ;}serv_sock = socket (PF_INET, SOCK_STREAM, 0 ); if (serv_sock =-1) error_handling ("socket () error"); memset (& serv_adr, 0, sizeof (serv_adr); serv_adr.sin_family = AF_INET; dependencies = htonl (INADDR_ANY); serv_adr.sin_port = htons (atoi (argv [1]); if (bind (serv_sock, (struct sockaddr *) & serv_adr, sizeof (serv_adr )) =-1) error_handling ("bind () error "); If (listen (serv_sock, 5) =-1) error_handling ("listen () error"); clnt_adr_sz = sizeof (clnt_adr); clnt_sock = accept (serv_sock, (struct sockaddr *) & clnt_adr, & clnt_adr_sz); // converts a socket to a FILE * pointer (I/O Stream separation ), then, you can perform operations on the socket like readfp = fdopen (clnt_sock, "r"); writefp = fdopen (dup (clnt_sock), "w") Like file operations "); // dup copies the file descriptor and indicates the integer is not equal // send the message fputs to the client ("from server: Hi ~ Client? \ N ", writefp); fputs (" I love all of the world \ n ", writefp); fputs (" You are awesome! \ N ", writefp); fflush (writefp); shutdown (fileno (writefp), SHUT_WR); // close the socket output stream fclose (writefp); // close the file stream writefp, at the same time, the corresponding socket will be closed to send EOF (the dup copy is closed, and the socket has a reference, so the socket will not be destroyed, so the environment will be semi-closed) // receive the final exit message fgets (buf, sizeof (buf), readfp); fputs (buf, stdout); fclose (readfp); // close the file stream readfp, close the socket at the same time (the fclose must be closed after the file is opened, so you cannot close the socket half with shutdown) return 0;} void error_handling (char * message) {fputs (message, stderr ); fputc ('\ n', stderr); exit (1 );}


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.