Linux网络服务器epoll模型的socket通讯的实现(一)
准备写一个网络游戏的服务器的通讯模块,参考网上看到的一些代码,在linux下面实现一个多线程的epoll模型的socket通讯的代码,以下是第一部分多线程的切换代码:
1 #include <stdio.h>
#include <sys/types.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <signal.h>
#include <fcntl.h>
#include <map>
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std; struct conninfo{
int rfd;
int wfd;
//map<struct ipport, struct perrinfo> peer;
}; bool g_bRun; struct conninfo g_ConnInfo; void setnonblocking(int sock)
{
int opts;
opts = fcntl(sock, F_GETFL);
if(opts < )
{
printf("fcntl(sock, GETFL)");
exit();
}
opts = opts | O_NONBLOCK;
if(fcntl(sock, F_SETFL, opts) < )
{
printf("fcntl(sock, SETFL, opts)");
exit();
}
return ;
} static void sig_pro(int signum)
{
printf("sig_pro recv signal: %d\n", signum);
if(signum == SIGQUIT)
{
g_bRun = false;
}
} void* AcceptThread(void* arg)
{
printf("accpet thread\n");
return NULL;
} void* ReadThread(void* arg)
{
printf("read thread\n");
return NULL;
} int main()
{
int ret;
int fd[]; //pipe
pthread_t iAcceptThreadId;
pthread_t iReadThreadId; struct sigaction sa;
sa.sa_flags = SA_RESTART;
sa.sa_handler = sig_pro;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL); g_bRun = true;
ret = pipe(fd);
if(ret < )
{
printf("main, pipe fall, %d %s\n", ret, errno);
g_bRun = false;
return ;
} g_ConnInfo.rfd = fd[];
g_ConnInfo.wfd = fd[]; setnonblocking(g_ConnInfo.rfd); pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); ret = pthread_create(&iAcceptThreadId, &attr, AcceptThread, NULL);
if(ret != )
{
printf("main, create accept thread fail: %s\n", errno);
g_bRun = false;
close(g_ConnInfo.rfd); //关闭管道
close(g_ConnInfo.wfd);
return ;
} ret = pthread_create(&iReadThreadId, &attr, ReadThread, NULL);
if(ret != )
{
printf("main, create read thread fail: %s\n", errno);
g_bRun = false;
pthread_join(iAcceptThreadId, NULL);
close(g_ConnInfo.rfd);
close(g_ConnInfo.wfd); return ;
} while(g_bRun)
{
sleep();
} pthread_join(iAcceptThreadId, NULL); //收回线程的资源,销毁线程
pthread_join(iReadThreadId, NULL);
close(g_ConnInfo.rfd);
close(g_ConnInfo.wfd); return ;
}
Linux网络服务器epoll模型的socket通讯的实现(一)的更多相关文章
- Linux 网络 I/O 模型简介(图文)(转载)
Linux 网络 I/O 模型简介(图文)(转载) 转载:http://blog.csdn.net/anxpp/article/details/51503329 1.介绍 Linux 的内核将所有外部 ...
- Java I/O演进与Linux网络I/O模型
参考文章: 简书-浅谈Linux五种IO:http://www.jianshu.com/p/486b0965c296 一.linux基础概念 1.1 内存空间 linux系统中的使用的是虚拟存储器,即 ...
- Linux 网络编程->epoll<-LT/ET模式整理(~相逢何必曾相识~)
今天自己整理一下epoll,网上有很多经典的介绍,看了很多~收藏了很多~还是整理一下做个积累, 自己的东西好找~ 1. epoll 模型简介 epoll 是Linux I/O 多路复用接口 selec ...
- linux网络编程IO模型
同步与异步: 同步就是一个任务的完成需要依赖另外一个任务时,只有等待被依赖的任务完成后,依赖的任务才能算完成. 异步是不需要等待被依赖的任务完成,只是通知被依赖的任务要 ...
- LVS (Linux虚拟服务器)模型及算法
LVS(Linux Virtual Server)Linux虚拟服务器 LVS集群采用IP负载均衡技术和基于内容请求分发技术. 用户请求发给负载均衡调度器,由负载均衡调度器根据设定的调度算法将请求发给 ...
- --系统编程-网络-tcp客户端服务器编程模型、socket、htons、inet_ntop等各API详解、使用telnet测试基本服务器功能
PART1 基础知识 1. 字节序 网络字节序是大端字节序(低地址存放更高位的字节), 所以,对于字节序为小端的机器需要收发网络数据的场景,要对这些数据进行字节序转换. 字节序转换函数,常用的有四个: ...
- Linux 网络 I/O 模型简介(图文)
1.介绍 Linux 的内核将所有外部设备都看做一个文件来操作(一切皆文件),对一个文件的读写操作会调用内核提供的系统命令,返回一个file descriptor(fd,文件描述符).而对一个sock ...
- Linux 网络I/O模型
前言 本文是笔者的第一篇博文,在这篇文章的大部分内容基于steven大神的<Unix Network Programming>.一来是对书本内容的整理与归纳.二来也是为接下来的博文奠定基础 ...
- 系统编程-网络-tcp客户端服务器编程模型(续)、连接断开、获取连接状态场景
相关博文: 系统编程-网络-tcp客户端服务器编程模型.socket.htons.inet_ntop等各API详解.使用telnet测试基本服务器功能 接着该上篇博文,咱们继续,首先,为了内容的完整性 ...
随机推荐
- JS 函数调用
Js函数调用的方式有如下几种情况: (1)具名函数直接调用 function foo() { } foo(); (2)匿名函数通过引用来调用 fooRef = function() { } fooRe ...
- python 闭包(closure)
闭包的定义: 闭包就是一个函数,这个函数可以记住封闭作用域里的值,而不管封闭作用域是否还在内存中. 来一个例子: def happy_add(a): print 'id(a): %x' % id(a) ...
- Android 编程下 java.lang.NoClassDefFoundError: cn.jpush.android.api.JPushInterface 报错
使用了极光推送的 jar 包项目在从 SVN 中检出后,假设不又一次对 jar 包和 Bulid Path 进行配置就会抛出 java.lang.NoClassDefFoundError: cn.jp ...
- [Whole Web] [Node.js] Using npm run to launch local scripts
npm run allows you to configure scripts inside of your package.json file which can access locally in ...
- AIR 程序开发系列 之五 保存数据的几种方式
Local SharedObject 这种方法比较简单方便的保存少的数据到到设备中.你不用自己去管理这些数据,设备会自动管理他. SharedObject 在 flash.net 包中,继承自Even ...
- Android利用Looper在子线程中改变UI
MainActivity如下: package cn.testlooper; import android.app.Activity; import android.os.Bundle; import ...
- 解读eXtremeComponents代码结构--转载
原文地址:http://blog.csdn.net/lark3/article/details/1937466 大致整理了去年写的东西,罗列如下: ec是一系列提供高级显示的开源JSP定制标签,当前的 ...
- RadioGroup&RadioButton
RadioGroup提供多选一机制:属性orientation:“vertlcal” or “horizontal” @Override public void onCheckedChanged(Ra ...
- 【安卓小技巧】WebView设置在本页面打开网页,而不是启动浏览器打开
使用WebView可以巧妙的在安卓APP中嵌入HTML页面, WebView wb = (WebView) findViewById(R.id.web); //找到WebView控件 wb.setWe ...
- Mac Mysql5.7.11安装和卸载
初学者,被mysql的安装弄晕了,所以在此记录一下. 安装 去http://www.mysql.com/downloads/, 选择最下方的MySQL Community Edition,点击MySQ ...