文章目录
1.socket()函数
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
1.1参数
domain
AF_INET IPv4 Internet protocols ip(7)
AF_INET6 IPv6 Internet protocols ipv6(7)
AF_UNIX, AF_LOCAL Local communication unix(7)
AF_NETLINK Kernel user interface device netlink(7) //内核和用户空间的通信,做设备驱动时会用到
AF_PACKET Low level packet interface packet(7) //原始套接字编程方法
type
SOCK_STREAM: 流式套接字 唯一对应于TCP
SOCK_DGRAM: 数据报套接字,唯一对应着UDP
SOCK_RAW: 原始套接字
protocol
一般填0,原始套接字编程时需填充
1.2返回值
RETURN VALUE
On success, a file descriptor for the new socket is returned.
On error, -1 is returned, and errno is set appropriately.
成功时返回文件描述符,出错时返回为-1
2.bind() 函数
SYNOPSIS
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
2.1参数
sockfd: 通过socket()函数拿到的fd
addr: struct sockaddr的结构体变量的地址
addrlen: 地址长度
struct sockaddr {
sa_family_t sa_family; //2字节
char sa_data[14]; //14字节
}
struct sockaddr_in {
sa_family_t sin_family; /* address family: AF_INET */ //2字节
in_port_t sin_port; /* port in network byte order */ //2字节
struct in_addr sin_addr; /* internet address */ //4字节
};
/* Internet address. */
struct in_addr {
uint32_t s_addr; /* address in network byte order */
};
网络TCP编程API
2.2返回值
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
如果是IPV6的编程,要使用struct sockddr_in6结构体(详细情况请参考man 7 ipv6),通常更通用的方法可以通过struct sockaddr_storage来编程
3.listen()函数
SYNOPSIS
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int listen(int sockfd, int backlog);
3.1参数
sockfd: 通过socket()函数拿到的fd
backlog:同时允许几路客户端和服务器进行正在连接的过程(正在三次握手)
一般填5, 测试得知,ARM最大为8
内核中服务器的套接字fd会维护2个链表:
1.正在三次握手的的客户端链表(数量=2backlog+1)
2.已经建立好连接的客户端链表(已经完成3次握手分配好了newfd)
比如:listen(fd, 5); //表示系统允许11(=25+1)个客户端同时进行三次握手
3.2返回值
RETURN VALUE
On success, zero is returned.
On error, -1 is returned, and errno is set appropriately.
4.accept(): 阻塞等待客户端连接请求
SYNOPSIS
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
4.1参数
sockfd: 经过前面socket()创建并通过bind(),listen()设置过的fd
addr和addrlen: 获取连接过来的客户的信息
4.2 返回值:
RETURN VALUE
On success, these system calls return a file descriptor for the ac‐
cepted socket (a nonnegative integer). On error, -1 is returned, errno
is set appropriately, and addrlen is left unchanged.
5.客户端的连接函数 connect()
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
connect()函数和服务器的bind()函数写法类似:
5.1 参数
sockfd: 通过socket()函数拿到的fd
addr: struct sockaddr的结构体变量的地址
addrlen: 地址长度
5.2 返回值
RETURN VALUE
If the connection or binding succeeds, zero is returned. On error, -1
is returned, and errno is set appropriately.
6.优化
一个进程,两次阻塞
newfd = accept (fd, NULL, NULL);
ret = read (newfd, buf, BUFSIZ - 1);
/*优化1: 让服务器程序能绑定在任意的IP上 */
#if 1
sin.sin_addr.s_addr = htonl (INADDY_ANY);
#else
if (inet_pton (AF_INET, SERV_IP_ADDR, (void *) &sin.sin_addr) != 1) {
perror ("inet_pton");
exit (1);
}
#endif
/*优化2:通过程序获取刚建立连接的socket的客户端的IP地址和端口号 */
#if 0
newfd = accept (fd, NULL, NULL);
if (newfd < 0) {
perror ("accept");
exit (1);
}
#else
/*优化2:通过程序获取刚建立连接的socket的客户端的IP地址和端口号 */
struct sockaddr_in cin;
socklen_t addrlen = sizeof (cin);
if ((newfd = accept (fd, (struct sockaddr *) &cin, &addrlen)) < 0) {
perror ("accept");
exit (1);
}
char ipv4_addr[16];
if (!inet_ntop (AF_INET, (void *) &cin.sin_addr, ipv4_addr, sizeof (cin))) {
perror ("inet_ntop");
exit (1);
}
printf ("Clinet(%s:%d) is connected!\n", ipv4_addr, ntons (cin.sin_port));
#endif
//net.h
#ifndef __MAKEU_NET_H__
#define __MAKEU_NET_H__
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#define SERV_PORT 5001
#define SERV_IP_ADDR "192.168.7.246"
#define BACKLOG 5
#define QUIT_STR "quit"
#endif
//server.c
#include "net.h"
int main (void)
{
int fd = -1;
struct sockaddr_in sin;
/* 1. 创建socket fd */
if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
perror ("socket");
exit (1);
}
/*2. 绑定 */
/*2.1 填充struct sockaddr_in结构体变量 */
bzero (&sin, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (SERV_PORT); //网络字节序的端口号
/*优化1: 让服务器程序能绑定在任意的IP上 */
#if 1
sin.sin_addr.s_addr = htonl (INADDY_ANY);
#else
if (inet_pton (AF_INET, SERV_IP_ADDR, (void *) &sin.sin_addr) != 1) {
perror ("inet_pton");
exit (1);
}
#endif
/*2.2 绑定 */
if (bind (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
perror ("bind");
exit (1);
}
/*3. 调用listen()把主动套接字变成被动套接字 */
if (listen (fd, BACKLOG) < 0) {
perror ("listen");
exit (1);
}
printf ("Server starting....OK!\n");
int newfd = -1;
/*4. 阻塞等待客户端连接请求 */
#if 0
newfd = accept (fd, NULL, NULL);
if (newfd < 0) {
perror ("accept");
exit (1);
}
#else
/*优化2:通过程序获取刚建立连接的socket的客户端的IP地址和端口号 */
struct sockaddr_in cin;
socklen_t addrlen = sizeof (cin);
if ((newfd = accept (fd, (struct sockaddr *) &cin, &addrlen)) < 0) {
perror ("accept");
exit (1);
}
char ipv4_addr[16];
if (!inet_ntop (AF_INET, (void *) &cin.sin_addr, ipv4_addr, sizeof (cin))) {
perror ("inet_ntop");
exit (1);
}
printf ("Clinet(%s:%d) is connected!\n", ipv4_addr, ntons (cin.sin_port));
#endif
/*5. 读写 */
//..和newfd进行数据读写
int ret = -1;
char buf[BUFSIZ];
while (1) {
bzero (buf, BUFSIZ);
do {
ret = read (newfd, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read");
exit (1);
}
if (!ret) { //对方已经关闭
break;
}
printf ("Receive data: %s\n", buf);
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client is exiting!\n");
break;
}
}
close (newfd);
close (fd);
return 0;
}
//client.c
#include "net.h"
int main (void)
{
int fd = -1;
struct sockaddr_in sin;
/* 1. 创建socket fd */
if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
perror ("socket");
exit (1);
}
/*2.连接服务器 */
/*2.1 填充struct sockaddr_in结构体变量 */
bzero (&sin, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (SERV_PORT); //网络字节序的端口号
#if 0
sin.sin_addr.s_addr = inet_addr (SERV_IP_ADDR);
#else
if (inet_pton (AF_INET, SERV_IP_ADDR, (void *) &sin.sin_addr) != 1) {
perror ("inet_pton");
exit (1);
}
#endif
if (connect (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
perror ("connect");
exit (1);
}
printf ("Client staring...OK!\n");
/*3. 读写数据 */
char buf[BUFSIZ];
int ret = -1;
while (1) {
bzero (buf, BUFSIZ);
if (fgets (buf, BUFSIZ - 1, stdin) == NULL) {
continue;
}
do {
ret = write (fd, buf, strlen (buf));
} while (ret < 0 && EINTR == errno);
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client is exiting!\n");
break;
}
}
/*4.关闭套接字 */
close (fd);
}