c語言多進程tcp服務器示例
server.h
#ifndef SERVER_H
#define SERVER_H
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <assert.h>
#include <sys/epoll.h>
#include <signal.h>
#include <fcntl.h>
#include "bussiness.h"
#define LISTENTQ 5
#define DEF_PORT 6000
#define MAX_EPOLL_SIZE 10
#define DEF_PROCESS_NUM 5
int create_tcpsvr(char *ip, int port);
void fill_sockaddr(struct sockaddr_in *addr,char *ip, int port);
void comm_to_client(int sockfd);
void epoll_business(int epollfd, int listenfd, struct epoll_event ev);
int init_epoll(int listenfd);
void create_process(int sockfd, int i);
#endif
server.c
#include "server.h"
/*
* Create a TCP Server
* Return socketfd
*/
int create_tcpsvr(char *ip,int port)
{
int sockfd;
struct sockaddr_in addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == EOF){
perror("create_tcpsvr(),socket()");
exit(EXIT_FAILURE);
}
fill_sockaddr(&addr,ip,port);
if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == EOF){
perror("create_tcpsvr(),bind()");
exit(EXIT_FAILURE);
}
if (listen(sockfd,LISTENTQ) == EOF) {
perror("create_tcpsvr(),bind()");
exit(EXIT_FAILURE);
}
return sockfd;
}
/**
* Set TCP server's address
*/
void fill_sockaddr(struct sockaddr_in *addr, char *ip, int port)
{
assert(addr);
addr -> sin_family = AF_INET;
addr -> sin_port = htons(port?port:DEF_PORT);
if (ip == NULL) {
addr -> sin_addr.s_addr = htonl(INADDR_ANY);
} else if((addr -> sin_addr.s_addr = inet_addr(ip)) == EOF) {
perror("fill_sockaddr(),inet_addr()");
exit(EXIT_FAILURE);
}
}
/*
* Communicate to client
*/
void comm_to_clt(int listenfd)
{
printf("TCP SERVER IS WAITING!\n");
struct epoll_event events[MAX_EPOLL_SIZE];
int fd_num;
int epollfd = init_epoll(listenfd);
while (1) {
fd_num = epoll_wait(epollfd,events,MAX_EPOLL_SIZE,-1);
if (fd_num == EOF) {
perror("comm_to_clt(),epoll_wait()");
continue;
}
while (--fd_num >= 0) {
epoll_business(epollfd, listenfd, events[fd_num]);
}
}
}
/*
* Initlization epoll
*/
int init_epoll(int listenfd)
{
struct epoll_event ev;
int epollfd;
if((epollfd = epoll_create(MAX_EPOLL_SIZE)) == EOF) {
perror("init_epoll(),epoll_create()");
exit(EXIT_FAILURE);
}
ev.events = EPOLLIN;
ev.data.fd = listenfd;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD,listenfd, &ev)) {
perror("init_epoll(),epoll_ctl()");
exit(EXIT_FAILURE);
}
return epollfd;
}
/*
* Respond for io change
*/
void epoll_business(int epollfd, int listenfd, struct epoll_event ev)
{
struct epoll_event ev_n;
if (ev.data.fd == listenfd) {
int clt_fd = accept(listenfd, 0, 0);
if (clt_fd == EOF) {
perror("epoll_business(), accept()");
return;
}
fcntl(clt_fd, F_SETFL, O_NONBLOCK);
ev_n.events = EPOLLIN|EPOLLET;
ev_n.data.fd = clt_fd;
if (epoll_ctl(epollfd,EPOLL_CTL_ADD, clt_fd, &ev_n) == EOF) {
perror("epoll_business(), epoll_ctl()");
return;
}
}else {
main_service(ev.data.fd);
if (epoll_ctl(epollfd,EPOLL_CTL_DEL, ev.data.fd, &ev) == EOF) {
perror("epoll_business(), epoll_ctl()");
}
close(ev.data.fd);
}
}
/*
* Create some process
*/
void create_process(int sockfd, int i)
{
/*
* Avoid zombie process
*/
signal(SIGCHLD,SIG_IGN);
while (i--) {
if (fork() == 0) {
comm_to_clt(sockfd);
}
}
while(1){
sleep(100);
}
}
int main(int argc, char *argv[])
{
int sockfd = create_tcpsvr(0, 0);
create_process(sockfd,DEF_PROCESS_NUM);
exit(EXIT_SUCCESS);
}
bussiness.h
#ifndef BUSSINESS_H
#define BUSSINESS_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#define BUFF_SIZE 4096
void main_service(int sockfd);
int write_sock(int sockfd, char *buff, size_t length);
int read_sock(int sockfd, char *buff, size_t llength);
#endif
bussiness.c
#include "bussiness.h"
void main_service(int sockfd)
{
char buff[BUFF_SIZE];
size_t buff_len;
if ((buff_len = read_sock(sockfd, buff, BUFF_SIZE)) == EOF) {
perror("main_service(),read_sock()");
return;
} else {
//業(yè)務邏輯從這里開始
printf("%s", buff);
}
}
/*
* Recive messages
* Return the length
*/
int read_sock(int sockfd, char *buff, size_t length)
{
assert(buff);
return read(sockfd, buff, length);
}
/*
* Send messages
* Return the length
*/
int write_sock(int sockfd, char *buff,size_t length)
{
assert(buff);
return write(sockfd, buff, length);
}
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10C#中split用法實例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改