#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <iostream>
using namespace std;
const char g_host[] = "192.168.1.250";
unsigned short g_port = 10086;
const int g_ciMaxFd = 2000;
#define STRERR strerror(errno)
int main()
{
// signal(SIGINT, SIG_IGN);
epoll_event* pEvent = NULL;
int iFlags = 0;
int epfd = 0;
epoll_event event = {0};
sockaddr_in cliAddr = {0};
socklen_t cliAddrLen = sizeof(sockaddr_in);
int sockCli = 0;
int iRet = 0;
char szBuf[1024] = {0};
int sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
cout << "socket failed: " << STRERR << endl;
return -1;
}
int iReuseAddr = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &iReuseAddr, sizeof(iReuseAddr));
sockaddr_in hostAddr = {0};
hostAddr.sin_family = AF_INET;
hostAddr.sin_addr.s_addr = inet_addr(g_host);
hostAddr.sin_port = htons(g_port);
if (-1 == bind(sock, (sockaddr*)&hostAddr, sizeof(hostAddr)))
{
cout << "bind failed: " << STRERR << endl;
goto clear;
}
if (-1 == listen(sock, 1024))
{
cout << "listen failed: " << STRERR << endl;
goto clear;
}
iFlags = fcntl(sock, F_GETFL);
iFlags |= O_NONBLOCK;
iFlags |= O_NDELAY;
if (-1 == fcntl(sock, F_SETFL, iFlags))
{
cout << "fcntl failed: " << STRERR << endl;
goto clear;
}
pEvent = (epoll_event*)malloc(g_ciMaxFd*sizeof(epoll_event));
if (!pEvent)
{
cout << "epoll_event malloc failed" << endl;
goto clear;
}
epfd = epoll_create(g_ciMaxFd);
if (-1 == epfd)
{
cout << "epoll_create failed: " << STRERR << endl;
goto clear;
}
event.data.fd = sock;
event.events = EPOLLIN | EPOLLRDHUP | EPOLLET;
if (-1 == epoll_ctl(epfd, EPOLL_CTL_ADD, sock, &event))
{
cout << "epoll_ctl failed: " << STRERR << endl;
goto clear;
}
while (1)
{
iRet = epoll_wait(epfd, pEvent, g_ciMaxFd, 10);
if (-1 == iRet)
{
cout << "epoll_wait failed: " << STRERR << endl;
goto clear;
}
else if (0 == iRet)
{
continue;
}
for (int i = 0; i < iRet; ++i)
{
// 处理客户端断开事件
if (pEvent[i].events & EPOLLRDHUP)
{
if (-1 == epoll_ctl(epfd, EPOLL_CTL_DEL, pEvent[i].data.fd, NULL))
{
cout << "epoll_ctl failed: " << STRERR << endl;
goto clear;
}
close(pEvent->data.fd);
cout << "cli: " << inet_ntoa(cliAddr.sin_addr) << ":" << ntohs(cliAddr.sin_port)
<< " disconn..." << endl;
continue;
}
// input 事件
if (pEvent[i].events & EPOLLIN)
{
// 连接
if (pEvent[i].data.fd == sock)
{
sockCli = accept(sock, (sockaddr*)&cliAddr, &cliAddrLen);
if (sockCli == -1)
{
cout << "accept failed: " << STRERR << endl;
goto clear;
}
if (sockCli > g_ciMaxFd)
{
cout << "sock num: " << sockCli << " reach max fd num(" << g_ciMaxFd << ")"
<< ", close!" << endl;
close(sockCli);
}
iFlags = fcntl(sockCli, F_GETFL);
iFlags |= O_NONBLOCK;
iFlags |= O_NDELAY;
if (-1 == fcntl(sockCli, F_SETFL, iFlags))
{
cout << "fcntl failed: " << STRERR << endl;
goto clear;
}
event.data.fd = sockCli;
event.events = EPOLLIN | EPOLLRDHUP | EPOLLET;
if (-1 == epoll_ctl(epfd, EPOLL_CTL_ADD, sockCli, &event))
{
cout << "epoll_ctl failed: " << STRERR << endl;
goto clear;
}
cout << "cli: " << inet_ntoa(cliAddr.sin_addr) << " : " << ntohs(cliAddr.sin_port)
<< " conn..." << endl;
}
// 客户端消息
else
{
int iRecv = 0;
while (iRecv < (int)sizeof(szBuf))
{
iRet = recv(pEvent[i].data.fd, szBuf+iRecv, sizeof(szBuf)-iRecv, 0);
if (iRet == 0)
{
cout << "recv iRet= " << iRet << endl;
break;
}
else if (iRet < 0)
{
if (errno == EAGAIN)
{
// 读完
break;
}
else
{
cout << "recv failed, iRet= " << iRet << endl;
if (-1 == epoll_ctl(epfd, EPOLL_CTL_DEL, pEvent[i].data.fd, NULL))
{
cout << "epoll_ctl failed: " << STRERR << endl;
// goto clear;
}
close(pEvent[i].data.fd);
break;
}
}
iRecv += iRet;
if (iRecv == sizeof(szBuf))
{
cout.write(szBuf, iRecv);
cout << endl;
iRecv = 0;
}
}
cout.write(szBuf, iRecv);
cout << endl;
}
// output 事件
// if (pEvent[i].events & EPO
}
}
}
clear:
if (sock != -1)
{
close(sock);
}
if (pEvent)
{
free(pEvent);
}
if (epfd != -1)
{
close(epfd);
}
return 0;
}
<?php
// Set time limit to indefinite execution
set_time_limit (0);
ini_set('error_reporting', E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
$host = '192.168.1.250';
$port = 10086;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
$timeout = array('sec'=>1, 'usec'=>0);
if (!socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, $timeout))
{
echo 'socket_set_option failed';
exit;
}
if (!socket_connect($sock, $host, $port))
{
echo 'socket_connect failed';
exit;
}
echo 'conn to ser '."$host:$port<br>";
$client_msg = "client coming!";
while (1)
{
$send_len = socket_send($sock, $client_msg, strlen($client_msg), 0);
if (!$send_len)
{
echo 'socket_send failed';
socket_close($sock);
exit;
}
echo 'send '.$client_msg."<br>";
flush();
sleep(1);
}
?>