C ++ network programming notes 1

xiaoxiao2021-03-06  63

C network programming volume 1 note -1

This program uses a web server that listens for 80 ports.

#include "ace / inet_addr.h" #include "ace / sock_connector.h" TTINCLUDE "ACE / SOCK_STREAM.H"

INT main (int Argc, char * argv []) {const char * pathname = argc> 1? Argv [l]: "index.html"; const char * server_hostname = argc> 2? Argv [2]: "ACE. ece.uci.edu "; ACE_SOCK_Connector connector; ACE_SOCK_Stream peer; ACE_INET_Addr peer_addr; if (peer_addr.set (80, server_hostname) == -1) return 1; else if (connector.connect (peer, peer_addr) == -1) Return 1;

Perr.send (....);

The ACE_SOCK_CONNECTOR class is used to actively establish a communication terminal, establish an ACE_SOCK_STREAM object after establishing and initialization. The ACE_SOCK_CONNECTOR object can only be used to establish a communication terminal, instead of using this object to send or receive data, these things should be completed by the ACE_SOCK_STREAM object.

Using this method can prevent: Use the handle to call the RECV () or send () function before a Socket object connection. This problem can only be discovered at runtime.

These issues can be resolved by using different types of objects (ACE_SOCK_CONNECTOR) and send data (ACE_SOCK_STREAM) to solve these problems and can be found in the compile period.

Use ACE_SOCK_CONNECTOR to connect to the connection using "block" or "non-blocking" or "timing" manner

Use ACE_SOCK_STREAM to transfer data using "block" or "non-blocking" or "timing".

The following is an example of a data transmission:

Char buf [bufs]; IOVEC IOV [3]; IOV [0] .iov_base = "get"; Iov [0] .iov_len = 4; // length of "get" .iov [l] .iov_base = pathname; IOV [l] .iov_len = Strlen (Pathname); IOV [2] .iov_base = "http / 1.0 / r / n / r / n", • IOV [2] .iov_len = 13; // length of "http / 1.0 / R / N / R / N "; if (Peer.sendv_n (IOV, 3) == -1) Return 1; for (SSIZE_T N; (n = peer.Recv (BUF, SIZEOF BUF))> 0;) ACE: Write_n (ACE_STDOUT, BUF, N); RETURN Peer.close () == -1? 1: 0;

Peer.sendv_n () on the UNIX / POSIX platform is implemented by Writev (), and the Winsock2 platform is implemented by wsasend ().

By using: ACE_TIME_VALUE TIMEOUT (10);

Peer.sendv_n (IOV, 3, & TIMEOUT); You can set it if the data is sent out in 10 seconds, will return -1;

A large number of design patterns are used in ACE, you can learn how to use the design mode to use the skills in the project, good :)

There are friends who have developed projects using ACE. :)

ACE library and source code can be downloaded at http://ace.ecE.uci.edu, with an ACE installation instructions

to be continued...

?

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

New Post(0)