环境:
Centos 6.5+gcc (GCC) 4.8.2+kernel 2.6.32+cmake version 2.8.12.2
目标:完成Linux下TCP通信基础版本,可以实现客户端对服务器端的通信。
服务器端:
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8) #project name project(multi_thread_socket_demo) set(CMAKE_CXX_COMPILER "g++") #set compiler for c++ language set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -lrt -lpthread -D_GLIBCXX_USE_NANOSLEEP") #source files(can add source files manually) #set(SOURCE_FILES main.cpp ThostFtdcMdApi.h ThostFtdcTraderApi.h ThostFtdcUserApiDataType.h ThostFtdcUserApiStruct.h MdSpi.cpp MdSpi.h TdSpi.cpp TdSpi.h CTP_Manager.cpp CTP_Manager.h Login.h Order.cpp Order.h User.cpp User.h Utils.cpp Utils.h DBManager.h DBManager.cpp Trader.h Trader.cpp FutureAccount.h FutureAccount.cpp Strategy.h Strategy.cpp Algorithm.h Algorithm.cpp) #source directory(add source files automatically) aux_source_directory(. SOURCE_FILES) find_package(Threads) #set extern libraries #set(LIBRARIES libthostmduserapi.so libthosttraderapi.so libmongoclient.so libboost_thread.so libboost_system.so libboost_regex.so) #add execute file add_executable(multi-thread-socket-server ${SOURCE_FILES}) #add link library target_link_libraries(multi-thread-socket-server ${CMAKE_THREAD_LIBS_INIT})
socket_server.c:
#include <netdb.h> #include <sys/socket.h> #include <unistd.h> #include <string.h> #include <stdio.h> #include <memory.h> #include <signal.h> #include <time.h> #include <pthread.h> #include <errno.h> #include "msg.h" #define MAXCONNECTIONS 100 int sockfd; void sig_handler(int signo) { if (signo == SIGINT) { printf("server close\n"); close(sockfd); exit(1); } } /*输出连接上来的客户端的相关信息*/ void out_addr(struct sockaddr_in *clientaddr) { //将端口从网络字节序转换成主机字节序 int port = ntohs(clientaddr->sin_port); char ip[16]; memset(ip, 0, sizeof(ip)); //将ip地址从网络字节序转换成点分十进制 inet_ntop(AF_INET, &clientaddr->sin_addr.s_addr, ip, sizeof(ip)); printf("client: %s(%d) connected\n", ip, port); } /*输出服务器端时间*/ void do_service(int fd) { /*和客户端进行读写操作(双向通信)*/ char buff[512]; while (1) { memset(buff, 0, sizeof(buff)); printf("start read and write...\n"); size_t size; if ((size = read_msg(fd, buff, sizeof(buff))) < 0) { perror("protocal error"); break; } else if (size == 0) { break; } else { printf("%s\n", buff); if (write_msg(fd, buff, sizeof(buff)) < 0) { if (errno == EPIPE) { break; } perror("protocal error"); } } } } void out_fd(int fd) { struct sockaddr_in addr; socklen_t len = sizeof(addr); //从fd中获取连接的客户端相关信息 if (getpeername(fd, (struct sockaddr *)&addr, &len) < 0) { perror("getpeername error"); return; } char ip[16]; memset(ip, 0, sizeof(ip)); int port = ntohs(addr.sin_port); inet_ntop(AF_INET, &addr.sin_addr.s_addr, ip, sizeof(ip)); printf("%16s(%5d) closed!\n", ip, port); } void *th_fn(void *arg) { int fd = (int)arg; do_service(fd); out_fd(fd); close(fd); return (void *)0; } int main(int argc, char *argv[]) { if (argc < 2) { printf("usage: %s #port\n", argv[0]); exit(1); } if (signal(SIGINT, sig_handler) == SIG_ERR) { perror("signal sigint error"); exit(1); } /*步骤1:创建socket(套接字)*/ sockfd = socket(AF_INET, SOCK_STREAM, 0); /*步骤2:将socket和地址(包括ip,port)进行绑定*/ struct sockaddr_in serveraddr; memset(&serveraddr, 0, sizeof(serveraddr)); /*向地址中填入ip,port,internet地址簇类型*/ serveraddr.sin_family = AF_INET; //ipv4 serveraddr.sin_port = htons(atoi(argv[1])); //port serveraddr.sin_addr.s_addr = INADDR_ANY; //接收所有网卡地址 if (bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) { perror("bind error"); exit(1); } /*步骤3:调用listen函数启动监听(指定port监听) 通知系统去接受来自客户端的连接请求 第二个参数:指定队列的长度*/ if (listen(sockfd, MAXCONNECTIONS) < 0) { perror("listen error"); exit(1); } /*步骤4:调用accept函数从队列中获得一个客户端的连接请求, 并返回新的socket描述符*/ struct sockaddr_in clientaddr; socklen_t clientaddr_len = sizeof(clientaddr); /*设置线程的分离属性*/ pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); while (1) { int fd = accept(sockfd, NULL, NULL); if (fd < 0) { perror("accept error"); continue; } /*步骤5:启动子线程去调用IO函数(read/write)和连接的客户端进行双向通信*/ pthread_t th; int err; /*以分离状态启动子线程*/ if ((err = pthread_create(&th, &attr, th_fn, (void *)fd)) != 0) { perror("pthread create error"); } pthread_attr_destroy(&attr); } return 0; }
编译方式:
cmake . make
客户端:
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8) #project name project(multi_thread_socket_demo) set(CMAKE_CXX_COMPILER "g++") #set compiler for c++ language set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -pthread -lrt -D_GLIBCXX_USE_NANOSLEEP") #source files(can add source files manually) #set(SOURCE_FILES main.cpp ThostFtdcMdApi.h ThostFtdcTraderApi.h ThostFtdcUserApiDataType.h ThostFtdcUserApiStruct.h MdSpi.cpp MdSpi.h TdSpi.cpp TdSpi.h CTP_Manager.cpp CTP_Manager.h Login.h Order.cpp Order.h User.cpp User.h Utils.cpp Utils.h DBManager.h DBManager.cpp Trader.h Trader.cpp FutureAccount.h FutureAccount.cpp Strategy.h Strategy.cpp Algorithm.h Algorithm.cpp) #source directory(add source files automatically) aux_source_directory(. SOURCE_FILES) #set extern libraries #set(LIBRARIES libthostmduserapi.so libthosttraderapi.so libmongoclient.so libboost_thread.so libboost_system.so libboost_regex.so) #add execute file add_executable(multi-thread-socket-client ${SOURCE_FILES}) #add link library #target_link_libraries(quant_ctp_XTrader ${LIBRARIES})
socket_client.c:
#include <netdb.h> #include <sys/socket.h> #include <unistd.h> #include <string.h> #include <stdio.h> #include <memory.h> #include <signal.h> #include <time.h> int main(int argc, char *argv[]) { if (argc < 3) { printf("usage: %s ip port\n", argv[0]); exit(1); } /*步骤1:创建socket*/ int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket error"); exit(1); } struct sockaddr_in serveraddr; memset(&serveraddr, 0, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons(atoi(argv[2])); /*将ip地址转换成网络字节序*/ inet_pton(AF_INET, argv[1], &serveraddr.sin_addr.s_addr); /*步骤2:客户端调用connect函数连接到服务器端*/ if (connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) { perror("connect error"); exit(1); } /*步骤3:调用IO函数(read/write)和服务器端进行双向通信*/ char buffer[512]; size_t size; char *prompt = "->"; while (1) { memset(buffer, 0, sizeof(buffer)); write(STDOUT_FILENO, prompt, 2); size = read(STDIN_FILENO, buffer, sizeof(buffer)); if (size < 0) continue; buffer[size - 1] = '\0'; if (write_msg(sockfd, buffer, sizeof(buffer)) < 0) { perror("write msg error"); continue; } else { if (read_msg(sockfd, buffer, sizeof(buffer)) < 0) { perror("read msg error"); continue; } else { printf("%s\n", buffer); } } } /*步骤4:关闭socket*/ close(sockfd); return 0; }
公共代码部分(自定义协议):
msg.h:
#ifndef __MSG_H__ #define __MSG_H__ #include <sys/types.h> typedef struct { //协议头部 char head[13]; char checknum; //校验码 //协议体部 char buff[512]; //数据 } Msg; /************************************************************************/ /* 发送一个基于自定义协议的message * 发送的数据存放在buff中*/ /************************************************************************/ extern int write_msg(int sockfd, char *buff, size_t len); /************************************************************************/ /* 读取一个基于自定义协议的message * 读取的数据存放在buff中*/ /************************************************************************/ extern int read_msg(int sockfd, char *buff, size_t len); #endif
msg.c:
#include "msg.h" #include <unistd.h> #include <string.h> #include <memory.h> #include <sys/types.h> /*计算校验码*/ static unsigned char msg_check(Msg *message) { unsigned char s = 0; int i; for (i = 0; i < sizeof(message->head); i++) { s += message->head[i]; } for (i = 0; i < sizeof(message->buff); i++) { s += message->buff[i]; } return s; } /************************************************************************/ /* 发送一个基于自定义协议的message * 发送的数据存放在buff中*/ /************************************************************************/ int write_msg(int sockfd, char *buff, size_t len) { Msg message; memset(&message, 0, sizeof(message)); strcpy(message.head, "gmqh_sh_2016"); memcpy(message.buff, buff, len); message.checknum = msg_check(&message); if (write(sockfd, &message, sizeof(message)) != sizeof(message)) { return -1; } } /************************************************************************/ /* 读取一个基于自定义协议的message * 读取的数据存放在buff中*/ /************************************************************************/ int read_msg(int sockfd, char *buff, size_t len) { Msg message; memset(&message, 0, sizeof(message)); size_t size; if ((size = read(sockfd, &message, sizeof(message))) < 0) { return -1; } else if (size == 0) { return 0; } //进行校验码验证,判断接收到message是否完整 unsigned char s = msg_check(&message); if ((s == (unsigned char)message.checknum) && (!strcmp("gmqh_sh_2016", message.head))) { memcpy(buff, message.buff, len); return sizeof(message); } return -1; }
编译方式:
cmake . make
运行效果:
文章的脚注信息由WordPress的wp-posturl插件自动生成