一。有网络基础的都应该知道tcp的三次握手和四次关闭的原理,大体流程以下数据库
三次握手:安全
三次握手的目的就是客户端和服务器创建链接的过程,创建链接的时候,客户端是主动打开的服务器
四次关闭,就是客户端和服务器端断开链接,这时候服务器是被动的关闭网络
这里再也不详解,有兴趣的能够本身googleTCP的相关知识socket
二。长连接和短连接tcp
长连接:长连接就是在TCP链接的基础上能够连续发送多个数据包,也就是发送数据包的时候长时间保持客户端和服务端的链接状态。若是双方没有数据要发送,那么必须经过发送检测包来保持链接状态。ui
短连接:短连接就是在TCP链接的基础上,发送一个数据包就创建一次链接,若是数据完以后接断开tcp连接,这种链接安全性比长连接高,一半用于银行等安全要求很高的系统。google
何时使用长连接和短链接?code
长连接通常用于须要操做频繁,点对点通信,视频直播就是使用长连接,因为三次握手须要时间,因此为了节省时间,对于操做频繁使用长连接发送一次数据后不用再次创建连接。orm
数据库的链接就要使用长连接,由于短链接会形成socket错误,并且频繁链接也容易形成错误。
这里有一个保持长连接的简单示例:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#define MAXBUF 1024 int main(int argc, char **argv) { int sockfd, len; struct sockaddr_in dest; char buffer[MAXBUF]; char heartbeat[20] = "hello server"; fd_set rfds; struct timeval tv; int retval, maxfd = -1; if (argc != 3) { printf("error! the right format should be : \ \n\t\t%s IP port\n\t eg:\t%s127.0.0.1 80\n", argv[0], argv[0]); exit(0); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("Socket"); exit(errno); } bzero(&dest, sizeof(dest)); dest.sin_family = AF_INET; dest.sin_port = htons(atoi(argv[2])); memset(&(dest.sin_zero), 0, 8); if (inet_aton(argv[1], (struct in_addr*)&dest.sin_addr.s_addr) == 0) { perror(argv[1]); exit(errno); } if (connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0) { perror("Connect"); exit(errno); } printf("\nReady to start chatting.\n\ Direct input messages and \n\ enter to send messages to the server\n"); while (1) { FD_ZERO(&rfds); FD_SET(0, &rfds); maxfd = 0; FD_SET(sockfd, &rfds); if (sockfd > maxfd) maxfd = sockfd; tv.tv_sec = 2; tv.tv_usec = 0; retval = select(maxfd+1, &rfds, NULL, NULL, &tv); if (retval == -1) { printf("Will exit and the select is error! %s", strerror(errno)); break; } else if (retval == 0) { //printf("No message comes, no buttons, continue to wait ...\n"); len = send(sockfd, heartbeat, strlen(heartbeat), 0); if (len < 0) { printf("Message '%s' failed to send ! \ The error code is %d, error message '%s'\n", heartbeat, errno, strerror(errno)); break; } else { printf("News: %s \t send, sent a total of %d bytes!\n", heartbeat, len); } continue; } else { if (FD_ISSET(sockfd, &rfds)) { bzero(buffer, MAXBUF+1); len = recv(sockfd, buffer, MAXBUF, 0); if (len > 0) { printf("Successfully received the message: '%s',%d bytes of data\n", buffer, len); } else { if (len < 0) printf("Failed to receive the message! \ The error code is %d, error message is '%s'\n", errno, strerror(errno)); else printf("Chat to terminate!\n"); break; } } if (FD_ISSET(0, &rfds)) { bzero(buffer, MAXBUF+1); fgets(buffer, MAXBUF, stdin); if (!strncasecmp(buffer, "quit", 4)) { printf("Own request to terminate the chat!\n"); break; } len = send(sockfd, buffer, strlen(buffer)-1, 0); if (len < 0) { printf("Message '%s' failed to send ! \ The error code is %d, error message '%s'\n", buffer, errno, strerror(errno)); break; } else { printf("News: %s \t send, sent a total of %d bytes!\n", buffer, len); } } } } close(sockfd); return 0; }