Socket programming: Computer Network basics,
Before learning about networks, review the basics of computer networks.
Lu Xun said that there is a huge copy of the article in the world. If you have any questions, you can copy the book based on your basic knowledge.
A layered model
1. Why Layer
In order to simplify the network design complexity, communication protocols adopt a layered structure, and Protocols at different layers are mutually independent and efficient in coordination.
The structure of complex communication protocols should be hierarchical. Layered protocols can bring a lot of convenience:
The advantages of layering are:
A> good flexibility: When any layer changes, as long as the interface relationship remains unchanged between the layers, the layers above or below will not be affected.
In addition, the services provided by a layer can be modified. When the services provided by a layer are no longer needed, you can even cancel the layer for easier management.
B> each layer is independent: interfaces are standardized between layers, allowing different products to provide only one part of the functions of each layer. A layer does not need to know how its next layer is implemented,
You only need to know the services provided by the layer through the interface between layers. Since each layer only implements a relatively independent function, it is easier to implement it!
2 layers
Standard layer-7 structure and practical layer-5 Structure
The socket corresponds to the transport layer and network layer.
Introduction to socket 2
Socket is the socket. The most primitive socket is inherited from unix systems, and then Microsoft launched winsocket.
Socket programming involves some concepts such as address, port, and byte order. It is best to understand.
Here we will talk about the socket connection process,
There are roughly three major steps:
1. server listening
2 client request connection
3 Connection Confirmation
The next section describes the specific connection steps in detail.
I am a C ++ programmer. If I want to learn socket network programming, I cannot recommend a book, C ++, or socket.
The most basic task is to find a running example, which must be available on both the client and server. Run it on your machine first. If OK, check the function step by step until you can write it out without reading anything. Next we can look at the deeper things written by others on the Internet. Those fancy things are icing on the cake, and the framework will soon be ready for other things. The most current socket programming in windows. But what I need to say is that in actual work, the server basically talks about UNIX. However, the basic functions are similar. Yunpan.cn/Qt6NnzMN4jGNQ
Socket network programming, write a Helloworld Program
/* Compile client. c server. c respectively */
/* Use */
/* 1. Run server first */
/* D: \> client /? */
/* 2. D: \> client-p: 5150-s: 192.168.99.77-n: 5 */
/* Server. c */
// Module Name: Server. c
//
// Description:
// This example extends strates a simple TCP server that accepts
// Incoming client connections. Once a client connection is
// Established, a thread is spawned to read data from
// Client and echo it back (if the echo option is not
// Disabled ).
//
// Compile:
// Cl-o Server. c ws2_32.lib
//
// Command line options:
// Server [-p: x] [-I: IP] [-o]
//-P: x Port number to listen on
//-I: str Interface to listen on
//-O Receive only, don't echo the data back
//
# Include <winsock2.h>
# Include <stdio. h>
# Include <stdlib. h>
# Pragma comment (lib, "ws2_32.lib ")
# Define DEFAULT_PORT 5150.
# Define DEFAULT_BUFFER 4096.
Int iPort = DEFAULT_PORT; // Port to listen for clients on
BOOL bInterface = FALSE, // Listen on the specified interface
BRecvOnly = FALSE; // Receive data only; don't echo back
Char szAddress [128]; // Interface to listen for clients on
//
// Function: usage
//
// Description:
// Print usage information and exit
//
Void usage ()
{
Pr ...... remaining full text>