一.编程流程
TCP的一个编程过程是怎样的呢??
这是作为客户端和服务器(CS模式)的一个编程过程,我们要对这些要用到的参数进行学习,包括需要传什么参数,返回值是什么等等。这些函数的逻辑实现我们不关注,只关注怎么用。
二.编程需要的函数
1. socket()
函数名 | int socket(int domain,int type, int protocol) | ||||||
所需头文件 | sys/types.h sys/socket.h | ||||||
功能 | 建立socket套接字,用的IPV4还是IPV6,用的TCP,还是UDP啊? | ||||||
传入参数 |
| ||||||
返回值 | 成功时返回文件描述符,出错时返回为-1 |
2. bind()
函数名 | int bind(int sockefd,struct sockaddr *my_addr, int addrlen) | ||||||
所需头文件 | sys/socket.h sys/types.h | ||||||
功能 | 将socket与本机上的一个端口绑定,随后就可以在该端口监听服务请求 | ||||||
传入参数 |
| ||||||
返回值 | 若成功,0;若失败,则返回-1 |
注:这里有说到一个结构体,需要对该结构体进行填充。那么它到底是个什么样的结构体?
通过man手册进行查看,我们可以轻松找到定义的这个结构体。(man 2 bind)
通用结构体:
使用命令 man 7 ip,可以查看到另外一个结构体sockaddr_in。
man 7 ipv6 查看相关ipv6编程
绑定的时候是绑定通用结构体,但是实际填充的是sockaddr_in结构体。(绑定的时候强制转化成通用结构体)
如果是IPV6的编程,要使用struct sockddr_in6结构体(详细情况请参考man 7 ipv6),通常更通用的方法可以通过struct sockaddr_storage来编程
3. listen()
函数名 | int listen(int sockefd,int backlog) | ||||
所需头文件 | sys/socket.h sys/types.h | ||||
功能 | listen函数使socket处于被动监听模式,并为该socket建立一个输入数据队列,将到达的服务请求保存在此队列中,直到程序处理他们(把主动套接字变成被动套接字) | ||||
传入参数 |
| ||||
返回值 | 若成功,0;若失败,则返回-1 |
注:
4. accept()
函数名 | int accept(int sockefd,void *addr, int *addrlen) | ||||||
所需头文件 | sys/socket.h sys/types.h | ||||||
功能 | 阻塞等待客户端连接请求 | ||||||
传入参数 |
| ||||||
返回值 | 若成功返回已经建立好连接的新的newfd;若失败,则返回-1 |
5.connect()
函数名 | int connect(int sockefd,struct sockaddr *serv_addr, int addrlen) | ||||||
所需头文件 | sys/socket.h sys/types.h | ||||||
功能 | 面向连接的客户程序使用connect函数来配置socket并与远端服务器建立一个TCP连接 | ||||||
传入参数 |
| ||||||
返回值 | 若成功,0;若失败,则返回-1 |
三. TCP编程实例
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>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#define SERV_PORT 5001
#define SERV_IP_ADDR "192.168.0.129"
#define BACKLOG 5
#define QUIT_STR "quit"
#endif
1.客户端
#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);
}
2.服务器端
#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;
}
以上的实例可以清晰的理解TCP的编程过程,和我们的实际应用的程序相比还有很多需要优化的地方。