资源简介
Linux高性能服务器编程(高清PDF)+项目源码(完整)
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_EVENT_NUMBER 1024
static int pipefd[2];
int setnonblocking( int fd )
{
int old_option = fcntl( fd F_GETFL );
int new_option = old_option | O_NONBLOCK;
fcntl( fd F_SETFL new_option );
return old_option;
}
void addfd( int epollfd int fd )
{
epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN | EPOLLET;
epoll_ctl( epollfd EPOLL_CTL_ADD fd &event );
setnonblocking( fd );
}
void sig_handler( int sig )
{
int save_errno = errno;
int msg = sig;
send( pipefd[1] ( char* )&msg 1 0 );
errno = save_errno;
}
void addsig( int sig )
{
struct sigaction sa;
memset( &sa ‘\0‘ sizeof( sa ) );
sa.sa_handler = sig_handler;
sa.sa_flags |= SA_RESTART;
sigfillset( &sa.sa_mask );
assert( sigaction( sig &sa NULL ) != -1 );
}
int main( int argc char* argv[] )
{
if( argc <= 2 )
{
printf( “usage: %s ip_address port_number\n“ basename( argv[0] ) );
return 1;
}
const char* ip = argv[1];
int port = atoi( argv[2] );
int ret = 0;
struct sockaddr_in address;
bzero( &address sizeof( address ) );
address.sin_family = AF_INET;
inet_pton( AF_INET ip &address.sin_addr );
address.sin_port = htons( port );
int listenfd = socket( PF_INET SOCK_STREAM 0 );
assert( listenfd >= 0 );
//int nReuseAddr = 1;
//setsockopt( listenfd SOL_SOCKET SO_REUSEADDR &nReuseAddr sizeof( nReuseAddr ) );
ret = bind( listenfd ( struct sockaddr* )&address sizeof( address ) );
if( ret == -1 )
{
printf( “errno is %d\n“ errno );
return 1;
}
//assert( ret != -1 );
ret = listen( listenfd 5 );
assert( ret != -1 );
epoll_event events[ MAX_EVENT_NUMBER ];
int epollfd = epoll_create( 5 );
assert( epollfd != -1 );
addfd( epollfd listenfd );
ret = socketpair( PF_UNIX SOCK_STREAM 0 pipefd );
assert( ret != -1 );
setnonblocking( pipefd[1] );
addfd( epollfd pipefd[0] );
// add all the interesting signals here
addsig( SIGHUP );
addsig( SIGCHLD );
addsig( SIGTERM );
addsig( SIGINT );
bool stop_server = false;
while( !stop_server )
{
int number = epoll_wait( epollfd events MAX_EVENT_NUMBER -1 );
if ( ( number < 0 ) && ( errno != EINTR ) )
{
printf( “epoll failure\n“ );
break;
}
for ( int i = 0; i < number; i++ )
{
int sockfd = events[i].data.fd;
if( sockfd == listenfd )
{
struct sockaddr_in client_address;
socklen_t client_addrlength = sizeof( client_address );
相关资源
- 剖析Linux系统下基于NUMA构建的服务
- linux SPI设备注册和驱动小结
- 周立功PCI CAN卡LINUX驱动ubuntu16.04内核
- 实现Windows与Linux两系统间自由切换
- 在双引导Linux系统上实现OS自动切换
- 如何删除Linux系统后找回Windows的启动
- Linux命令详解,循序渐进Linux
- Linux系统文件命令精通指南(下)
- Linux系统命令及Shell脚本实践指南
- RK3308 LINUX开发者指南(1).pdf
- S3C4510 开发板中uCLinux系统开发
- arm cortex m0 rtl code
- 嵌入式操作系统的解析
- Linux点阵字库和字库生成器.rar
- Xcode 12.3(16F156)安装包.zip
- Linux-UNIX系统编程手册上、下册中文版
- windows 64位系统下安装Code Warrior6.3方法
- 基于ARM的电子相册源码含动态库可运
- PSFTP.EXE 工具
- 如何实现Linux与windows文件互传
- Linux内核函数Start_kernel()的功能
- Zxing-Code_128一维码
- CODE128A 字体
- Code 128 字体
- INBarcodeOCR条码识别组件,识别率及速
- code39条形码字体
- 一只老鸟的嵌入式ARM学习心得
- cximage的linux版本源码
- yaf-2.1.17.tgz
- IBM eServer xSeries 445 EXP400在Linux下的双
评论
共有 条评论