【整理】Linux Socket网络编程_TCP编程(2)_多进程版本

原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: 【整理】Linux Socket网络编程_TCP编程(2)_多进程版本

环境:

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_process_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-process-socket-server ${SOURCE_FILES})

    #add link library
    #target_link_libraries(quant_ctp_XTrader ${LIBRARIES})

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);
	}
	if (signo == SIGCHLD) {
		printf("child process deaded...\n");
		wait(0);
	}
}

/*输出连接上来的客户端的相关信息*/
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");
			}
		}
	}
}

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);
	}
	if (signal(SIGCHLD, sig_handler) == SIG_ERR) {
		perror("signal sigchld 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);
	while (1)
	{
		int fd = accept(sockfd, (struct sockaddr *)&clientaddr, &clientaddr_len);
		if (fd < 0) {
			perror("accept error");
			continue;
		}
		/*步骤5:启动子进程去调用IO函数(read/write)和连接的客户端进行双向通信*/
		pid_t pid = fork();
		if (pid < 0) {
			continue;
		}
		else if (pid == 0) { //子进程
			out_addr(&clientaddr);
			do_service(fd);
			/*步骤6:关闭socket*/
			close(fd);
			break;
		}
		else { // 父进程
			close(fd);
		}	
	}

	return 0;
}

编译:

cmake .
make

客户端:

CMakeLists.txt:

    cmake_minimum_required(VERSION 2.8)
    #project name
    project(multi_process_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-process-socket-client ${SOURCE_FILES})

    #add link library
    #target_link_libraries(quant_ctp_XTrader ${LIBRARIES})

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>

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

运行效果:

QQ截图20160831141044

原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: 【整理】Linux Socket网络编程_TCP编程(2)_多进程版本

文章的脚注信息由WordPress的wp-posturl插件自动生成



|2|left
打赏

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: