C ++ network programming volume 1 note -2

xiaoxiao2021-03-06  65

C network programming volume 1 note -2

The ACE_SOCK_ACCEPTOR class plays a "passive connection" and can be understood as the server.

This class is a connection to the client through the Factory mode, and initializes the ACE_SOCK_STREAM object after the connection is established. The OPEN method of the ACE_SOCK_ACCEPTOR class is called the underlying socket (), a list () function in the correct order.

An example, implementation:

1. Listen to the 80-port and accept the customer connection;

2. Read the path name in the GET request and send the file to the customer.

3. Turn off the connection.

4. Go back step 1.

#include "ace / auto_ptr.h" #include "ace / inet_addr.h" #include "ace / sock_acceptor.h"

#include "ace / sock_stream.h" #include "ace / MEM_MAP.H"

// Return A Dynamically Allocated path name buffer, extern char * get_url_pathname (ace_sock_stream *);

INT main () {ace_inet_addr server_addr;

ACE_SOCK_ACCEPTOR ACCEPTOR; ACE_SOCK_STREAM Peer; if (Server_addr.set (80) == -1) Return 1; if (Acceptor.Open (Server_ADDR) == -1) Return 1; for (;;) {IF (Acceptor.Accept ( Peer) == -1) Return 1; Peer-> Disable (ACE_NONBLOCK); // ENSURE Blocking

Send_n

Auto_ptr

Pathname = get_url_pathname (peer); // acquire the file name of the user request

ACE_MEM_MAP MAPPED_FILE (Pathname.get ());

I f (PEER.SEND_N (mapped_file.addr (),

Mapped_file.size ()) == -1) Return 1;

Peer.Close ();

}

Return Acceptor.Close () = = -1? 1: 0;

}

ACE_MEM_MAP implements the "Memory Mapping" file mechanism. Provide a unified interface for the implementation of different platforms.

When a large number of customers are requested, this looped web server has a lot of shortcomings, there may be multiple users waiting state, and the later content will solve this problem.

The next time the ACE_INET_ADDR class is next.

to be continued...

转载请注明原文地址:https://www.9cbs.com/read-116967.html

New Post(0)