VC Socket Network communication

Source: Internet
Author: User


1. Communicate with each other, first you must know who to communicate with, that is, the name of the other person, for the computer is the host name or IP address; the second party must abide by the rules of the agreement, not one to speak English, one speaks Chinese, that communication protocol must comply with, (TCP/IP), multiple applications, each has a port number. The IP address is equivalent to a company's switchboard number, and the port number corresponds to an extension number.

2. Layered, physical layer, Data link layer, network layer, Transport layer, application layer,,,

3. Application layer protocol: TELNET,FTP,HTTP,DNS,SMTP. Transport layer: TCP/UDP; physical layer: ip,icmp.

4. TCP connection-oriented reliable transport protocol, three handshake, data validation and data retransmission mechanism. Telephone, connection established. Application in high data integrity situations.

UDP: Unreliable communication protocol without connection. Letter. High-performance ...

5. Data packaging, packaging, reach the transport layer plus the TCP protocol header, to the Network layer Plus IP protocol header.

If you post a letter, add an envelope to the next layer and unpack it.

6. TCP/IP Model: Application layer, Transport layer, network layer, network interface layer.

A port is an abstract software structure (including some data structures and IO buffers). 0-65535 (>1024)

7. Streaming sockets: Based on reliable TCP protocol; datagram sockets: Based on UDP protocol.

8. TCP server-side program flow:

Creates a socket, binds the socket to a local address and port (BIND), and sets the socket to listening mode. Prepares to receive a client request (Listen), waits for a request to arrive, accepts a connection request when the request arrives, returns a new socket (accept) corresponding to this connection, communicates with the returned socket and the client (SEND/RECV), and returns, waiting for another client request.

9. TCP Client program flow:

Create sockets, issue connection requests to the server (connect), communicate with the server (SEND/RECV), and close the socket.

When the Accept function is called, the server waits for the client to connect. The BIND function is not required by the client.

UDP server: Create socket, bind IP and port bind, wait to receive data recvfrom, close socket;

UDP client: Creates sockets, sends data sendto to the server, and closes sockets.

10.//1. Load Socket Font

Wsadata Wsadata;

WORD Version = Makeword (2, 2);//2.2

int ret =:: WSAStartup (version, &wsadata);

if (ret!= 0) {

MessageBox ("Socket initialization failed!", "Jiuzhou di Fei");

Return

}

2. Creating sockets

Socket M_hsocket =:: Socket (af_inet, sock_stream,0);

:: WSAAsyncSelect (m_hsocket,this->m_hwnd,wm_socket,fd_read| Fd_connect);//Set Socket to asynchronous mode

Final release of resources:

if (WSACleanup ()!= 0) {

TRACE ("Uninitilize error:%d\n", WSAGetLastError ());

}

Just do It!!!



Socket is equivalent to the network communication at both ends of the socket, as long as the other side of the socket and their sockets have a communication connection, both sides can send and receive data. The definition is similar to a file handle.
The original socket holds the full IP header in the packet, and the previous two sockets receive only user data. Therefore, the data can be analyzed by the original socket.


The definition of the old network address structure in_addr
Definition of a new network address structure: #define INADDR_LOOPBACK 0x7f000001
Steps for using sockets
1, start Winsock WSAStartup
2, Create sockets: (Server side and client)
M_hsocket =:: Socket (af_inet, sock_stream,0);
3. Binding of Sockets: Bind the local address to the socket you created. (Server-side and client) bind
4, Socket monitoring: (server-side) listen
5, socket waiting for the connection:: (server-side) accept
6, Socket Link: Connect two sockets to prepare for communication. (client) Connect
7, Socket send data: (server side and client) send
8, Socket Data reception: (client) recv
9. Interrupt Socket Connection: Notifies server side or client to stop receiving and sending data. (server side and client) shutdown
10, close the socket: release the resources occupied. (server side and client) closesocket


WSAGetLastError,
Blocking mode: Waits until the I/O operation completes and does not give control to the program. Socket defaults to blocking mode. Can be handled through multithreading technology.
Non-blocking mode: When performing I/O operations, the Winsock function returns and surrenders control. This pattern is more complex to use because the function returns when it is not running.
will continue to return wsaewouldblock errors. But it has a powerful function.
In order to solve this problem, I put forward some I/O operation of some I/O model, the following describes the most common three kinds:


2, select Model: 3, Wsaasynselect Model: 4, WSAEventSelect model

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.