1. Network to develop two design patterns
2. The hierarchical network model
3. Ethernet protocol
4. IP protocol
After you enter the URL, domain name resolution by DNS servers to resolve domain name to obtain the IP address.
Each data through a routing node, survival time is eight minus 1 when reduced to 0, the data is discarded routing node, to prevent refuse has been passed in the data network.
5. udp protocol
6. tcp protocol
7. tcp-ip four-layer model Encapsulation
8. udp and explain tcp
Using TCP agreement, such as the server each time you send 1M data, how much data each time the client received all right, like every income 1K; with UDP agreement, such as the server each time you send 10K of data, then the client every time 10K can only receive data.
9. What is a socket
10. Socket Memory Model
11. The network byte order conversion
The little-endian computer data storage, and network transmission requires big-endian data storage method (routing nodes want to get an IP address and mac address while unpacking, need to be stored in big endian), so the need for conversion.
IP addresses are 32-bit, 16-bit Port is the port number, so they are converted to different functions.
12. ip address conversion function
Inet_pton function: Parameter af and generally representative of AF_INET IPv4, src is an incoming string, dst is spread parameters;
Inet_ntop function: Parameter af and generally representative of AF_INET IPv4, src parameters are passed, dst is the outgoing string, size is an incoming parameter indicates the size of the string.
13. sockaddr data structure
Sin_family which is usually set AF_INET, on behalf of IPv4. sin_port on behalf of the port number. sin_addr . s_addr on behalf of the IP address.
NOTE: In use, to create a good take sockaddr_in address structure variables, then strongly turn type (struct sockaddr *).
14. The network socket functions
15. CS flowchart Model
16. server to achieve
Results of the:
17. server and client communications
server-side implementation
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <arpa/inet.h>
#include <ctype.h>
int main(int argc, const char* argv[])
{
// 创建监听的套接字
int lfd = socket(AF_INET, SOCK_STREAM, 0);
if(lfd == -1)
{
perror("socket error");
exit(1);
}
// lfd 和本地的IP port绑定
struct sockaddr_in server;
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET; // 地址族协议 - ipv4
server.sin_port = htons(8888);
server.sin_addr.s_addr = htonl(INADDR_ANY);
int ret = bind(lfd, (struct sockaddr*)&server, sizeof(server));
if(ret == -1)
{
perror("bind error");
exit(1);
}
// 设置监听
ret = listen(lfd, 20);
if(ret == -1)
{
perror("listen error");
exit(1);
}
// 等待并接收连接请求
struct sockaddr_in client;
socklen_t len = sizeof(client);
int cfd = accept(lfd, (struct sockaddr*)&client, &len);
if(cfd == -1)
{
perror("accept error");
exit(1);
}
printf(" accept successful !!!\n");
char ipbuf[64] = {0};
printf("client IP: %s, port: %d\n",
inet_ntop(AF_INET, &client.sin_addr.s_addr, ipbuf, sizeof(ipbuf)),
ntohs(client.sin_port));
// 一直通信
while(1)
{
// 先接收数据
char buf[1024] = {0};
int len = read(cfd, buf, sizeof(buf));
if(len == -1)
{
perror("read error");
exit(1);
}
else if(len == 0)
{
printf(" 客户端已经断开了连接 \n");
close(cfd);
break;
}
else
{
printf("recv buf: %s\n", buf);
// 转换 - 小写 - 大写
for(int i=0; i<len; ++i)
{
buf[i] = toupper(buf[i]);
}
printf("send buf: %s\n", buf);
write(cfd, buf, len);
}
}
close(lfd);
return 0;
}
client-side implementation
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <arpa/inet.h>
int main(int argc, const char* argv[])
{
if(argc < 2)
{
printf("eg: ./a.out port\n");
exit(1);
}
int port = atoi(argv[1]);
// 创建套接字
int fd = socket(AF_INET, SOCK_STREAM, 0);
// 连接服务器
struct sockaddr_in serv;
memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_port = htons(port);
// oserv.sin_addr.s_addr = htonl();
inet_pton(AF_INET, "127.0.0.1", &serv.sin_addr.s_addr);
connect(fd, (struct sockaddr*)&serv, sizeof(serv) );
// 通信
while(1)
{
// 发送数据
char buf[1024];
printf("请输入要发送的字符串: \n");
fgets(buf, sizeof(buf), stdin);
write(fd, buf, strlen(buf));
// 等待接收数据
int len = read(fd, buf, sizeof(buf));
if(len == -1)
{
perror("read error");
exit(1);
}
else if(len == 0)
{
printf("服务器端关闭了连接\n");
break;
}
else
{
printf("recv buf: %s\n", buf);
}
}
close(fd);
return 0;
}
The results: first start the server end, and then start the client.
client side:
server side: