The so-called network , It's a lot of computers connected to each other . We're going to learn network programming , It's actually programming to communicate between two computers .
such as , The browser uses network communication , So that our computer can communicate with the web server , Get what we need to browse .
Another example ,qq So we can communicate with good friends . These are all used in network programming .
Communication between two computers , that How is data transferred between two computers ? yes , It's transmitted over a network cable , It is indicated by the positive and negative of electricity In binary 0 1
, Everyone knows that in computers , All the data ( image , voice , written words ) They're all stored in binary form , So you can transfer any data over the network cable .
How do we program data transmission ? Can it be used c Does language control the positive and negative poles of electricity ? of course
We don't need to , We only need to use the operating system to provide us with a set of network programming api Function , what api function
In fact, it is a function , We just have to learn to use these functions , Then it can be used to write a variety of network programs , There's no need for us to do the same thing over and over again .
Next, let's talk about this set of network programming api Function .
The first concept is socket , You can click Baidu Encyclopedia to have a look , He also attached a small example , It's better to have a detailed look , And try to understand him , This is very helpful for later study .
How to use this set of programming functions ?
The program is divided into server and client ( The server is equivalent to our website server , The client is our browser )
On the server side , We need to follow these steps roughly :
1. initialization socket library . 2. Binding native address and port .( Server specific ) 3. Listening port , Waiting for client connection . 4. When there is a client connection , Processing , But then continue to listen or end the program .
5. Exit the program , close socket, Termination right socket Use of Library .
On the client side , We need to follow these steps :
1. initialization socket library . 2. Set the address and port information of the remote host , And connect . 3. Wait for the response from the server 4. When the server responds , Processing .
5. Exit the program , close socket, Termination right socket Use of Library
The following is an excerpt from a picture drawn by netizens :
Here are the server-side and client-side code :
* Server : #include <stdio.h> #include <winsock2.h> #pragma comment(lib,
"ws2_32.lib") // There is no semicolon ending here , For introduction winsock library int main(void) { int len = 0; WSADATA wd;
int ret = 0; SOCKET s,c; char sendBuf[1000]="", recvBuf[1000]=""; SOCKADDR_IN
saddr, caddr; ret = WSAStartup(MAKEWORD(2,2),&wd); /*1. Initialization operation */ if(ret != 0) {
return 0; } if(HIBYTE(wd.wVersion)!=2 || LOBYTE(wd.wVersion)!=2) {
printf(" initialization failed "); WSACleanup(); return 1; } s = socket(AF_INET, SOCK_STREAM, 0);
/*2. Create server socket*/ saddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); /*3. Set server information */
saddr.sin_family = AF_INET; /* Protocol type */ saddr.sin_port = htons(8888); bind(s,
(SOCKADDR *)&saddr, sizeof(SOCKADDR)); /*4. Bind to local port */ listen(s,5); /*5. Listening port */
len = sizeof(SOCKADDR); while(1) { /*6. Waiting for client connection , It's blocking here , Until a client connection arrives .*/ c =
accept(s, (SOCKADDR*)&caddr, &len); sprintf(sendBuf, " Welcome Alex Blog , Your IP:%s\n",
inet_ntoa(caddr.sin_addr)); send(c, sendBuf, strlen(sendBuf)+1, 0);
/*7. Send data to client */ recv(c, recvBuf, 1000, 0); /*8. Accept client's return */ printf("%s\n",
recvBuf); /*9. Print out the data sent by the client */ closesocket(c); /*10. If you no longer contact this client , Just shut it down */ }
/* If there are conditions to exit the loop , There's still a need to clear right socket Use of Library */ /* WSACleanup();*/ return 0; }
* Client code #include <stdio.h> #include <winsock2.h> int main(void) { WSADATA wd;
int ret = 0; SOCKET c; char recvBuf[1000]="", sendBuf[1000]=""; SOCKADDR_IN
saddr; ret = WSAStartup(MAKEWORD(2,2),&wd); /*1. Initialization operation */ if(ret != 0) { return
0; } if(HIBYTE(wd.wVersion)!=2 || LOBYTE(wd.wVersion)!=2) { printf(" initialization failed ");
WSACleanup(); return 1; } c = socket(AF_INET, SOCK_STREAM, 0);
/*2. Create client socket*/ saddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
/*3. Define the server information to connect to */ saddr.sin_family = AF_INET; saddr.sin_port = htons(8888);
connect(c, (SOCKADDR*)&saddr, sizeof(SOCKADDR)); /*4. Connection server */ recv(c, recvBuf,
1000, 0); printf(" Data from the server :%s\n", recvBuf); sprintf(sendBuf, " Hello, server !!!");
send(c, sendBuf, strlen(sendBuf)+1, 0); closesocket(c); WSACleanup(); return 0;
}
last , Run the generated server first , Then execute the client !
The implementation effect is as follows :
Technology