博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux 网络编程之 Select
阅读量:6976 次
发布时间:2019-06-27

本文共 3126 字,大约阅读时间需要 10 分钟。

/*server*/#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include "common.h"#include
void process_cli(Client *client, char* recvbuf, int len,int count); //客户请求处理函数int main(int argc,char * argv[]){ int listenFd=0; //地址信息结构体大小 if ((listenFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { //调用socket创建用于监听客户端的socket std::cout<<"Creating socket failed."<
maxI) //数组最大元素值 { maxI = i; } FD_SET(connectFd, &allSet); //将新socket连接放入select监听集合 if (connectFd > maxFd) { maxFd = connectFd; //确认maxfd是最大描述符 } if (--nReady <= 0) { continue; //如果没有新客户端连接,继续循环 } } for (int i = 0; i <= maxI; i++) { if ( (sockfd = client[i].fd) < 0) //如果客户端描述符小于0,则没有客户端连接,检测下一个 { continue; } if (FD_ISSET(sockfd, &readSet)) { //检测此客户端socket是否有数据 int nReadLength = recv(sockfd, recvbuf, MAXDATASIZE,0); if (nReadLength == 0) { //从客户端socket读数据,等于0表示网络中断 close(sockfd); //关闭socket连接 FD_CLR(sockfd, &allSet); //从监听集合中删除此socket连接 client[i].fd = -1; //数组元素设初始值,表示没客户端连接 std::cout<<"Client Addr: "<< client[i].m_addr << " Close "<
m_addr <<" Data:"<
toString()<
fd,&sendData,sendData.size(),0);}
/* client */#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "common.h"int main(int argc, char *argv[]){ char hostname[100]; gethostname(hostname,sizeof(hostname)); int sockfd, numbytes; struct hostent *he; struct sockaddr_in serv_addr; if ((he = gethostbyname(defaultHostName)) == NULL) { std::cout<<"gethostbyname error!"<
h_addr); bzero(&(serv_addr.sin_zero), 8); if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1) { std::cout<<"connect error!"<
toString()<
/*common.h*/#ifndef COMMON_H_#define COMMON_H_const int PORT=2248;           //服务器端口const int BACKLOG=5;           //listen队列中等待的连接数const int MAXDATASIZE=1024;    //缓冲区大小const char defaultHostName[] = "127.0.0.1";#include 
#include
#include
#include
#include
#include
#include
struct Client { //客户端结构体 int fd; //客户端socket描述符 std::string m_addr;};class OctArray16 {public: uint32_t m_len; char data[28]; size_t size(){ return 32; } std::string toString(){ return std::string(data,m_len); } OctArray16(const char *str){ m_len = strlen(str); strcpy(data,str); }};std::string GetCurrentTime(){ time_t cutTime=time(0); tm * tmT=localtime(&cutTime); char buf[12]; sprintf(buf,"%d:%d:%d",tmT->tm_hour,tmT->tm_min,tmT->tm_sec); return std::string(buf);}#endif
cmake_minimum_required(VERSION 2.8)## Use the variable PROJECT_NAME for changing the target nameset( PROJECT_NAME "SelectLearn" )## Set our project nameproject(${PROJECT_NAME})## Use all the *.cpp files we found under this folder for the projectSET(CLIENT_SOURCE Client.cpp common.h)SET(SERVER_SOURCE Server.cpp common.h)## Define the executableadd_executable(server ${SERVER_SOURCE})add_executable(client ${CLIENT_SOURCE})

转载地址:http://hjupl.baihongyu.com/

你可能感兴趣的文章
OSC源创会往期图文回顾链接地址收藏
查看>>
快节奏的多人游戏同步 - 示例代码和在线演示
查看>>
【分享】Java的几个重要词语
查看>>
symfony2的配置和welcome界面问题汇总
查看>>
选IDC房时,用脚本截取丢失包和rtt的值作比对
查看>>
TCP Wrappers
查看>>
mina
查看>>
kindle
查看>>
C# MoreLinq 扩展安装
查看>>
遍历查找指定文件
查看>>
域名年龄-SEO搜索引擎优化
查看>>
Qunee for HTML5 V2.5新版本发布
查看>>
<context-param>与<init-param>的区别与作用
查看>>
php中mkdir()函数的权限问题
查看>>
奇特的约会
查看>>
radio根据name 获取选中值及判断是否被选中
查看>>
LVM 类型的 Storage Pool - 每天5分钟玩转 OpenStack(8)
查看>>
[Nginx优化]分享nginx配置文件及优化说明
查看>>
TensorFlow入门
查看>>
Apache Spark 2.2.0 中文文档 翻译活动
查看>>