linux socket的select函数例子
使用select函数可以以非阻塞的方式和多个socket通信。程序只是演示select函数的使用,功能非常简单,即使某个连接关闭以后也不会修改当前连接数,连接数达到最大值后会终止程序。
1. 程序使用了一个数组fd_A,通信开始后把需要通信的多个socket描述符都放入此数组。
2. 首先生成一个叫sock_fd的socket描述符,用于监听端口。
3. 将sock_fd和数组fd_A中不为0的描述符放入select将检查的集合fdsr。
4. 处理fdsr中可以接收数据的连接。如果是sock_fd,表明有新连接加入,将新加入连接的socket描述符放置到fd_A。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MYPORT 1234 // the port users will be connecting to
#define BACKLOG 5 // how many pending connections queue will hold
#define BUF_SIZE 200
int fd_A[BACKLOG]; // accepted connection fd
int conn_amount; // current connection amount
void showclient()
{
int i;
printf("client amount: %d\n", conn_amount);
for (i = 0; i < BACKLOG; i++) {
printf("[%d]:%d ", i, fd_A[i]);
}
printf("\n\n");
}
int main(void)
{
int sock_fd, new_fd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in server_addr; // server address information
相关文档:
如果你正在为你的linux
操作系统寻找免费而又够酷够炫的图形设计软件,你就来对地方了!这一次,我们将为你介绍在linux
操作系统7个应用广泛而且免费的3D图形设计软件。
1、Blender
【点击下载
Blender
】
Blender是一个自由、开源的3D模型创作 ......
首先申明,我是菜鸟.菜到什么程度,大家看看下面的代码.
代码要求:取得1000次单独创建进程的时间.
[code:1:fdac913669]#include
#include
#include
#include
#include
strut timeval {
long tv_sec; /* 秒数 */
long tv_usec; /* 微秒数  ......
How to change the hostname of a Linux system
Normally we will set the hostname of a system during the installation process. Many peoples don’t care about this, and don’t change the hostname even if for example this was set to something really stupid by the datacenter that installed the ......
一 操作系统分类
http://www.csee.wvu.edu/~jdm/classes/cs258/OScat/
二 操作系统和时间线
http://en.wikipedia.org/wiki/Timeline_of_operating_systems
三 操作系统和公司
http://en.wikipedia.org/wiki/List_of_operating_systems
四 各种操作系统比较
http://en.wikipedia.org/wiki/Comparison_of_operat ......
一、Makefile介绍
Makefile是用于自动编译和链接的,一个工程有很多文件组成,每一个文件的改变都会导致工程的重新链接,但是不是所有的文件都需要重新编译,Makefile中纪录有文件的信息,在make时会决定在链接的时候需要重新编译哪些文件。
Makefile的宗旨就是:让编译器知道要编译一个文件需要依赖其他的 ......