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
相关文档:
用户可以用任何编辑程序来编写Shell程序。因为Shell程序是解释执行的,所以不需要编译成目的程序。按照Shell编程的惯例,以 bash为例,程序的第一行一般为“#!/bin/bash”,其中 # 表示该行是注释,叹号 ! 告诉Shell运行叹号之后的命令并用文档的其余部分作为输入,也就是运行/bin/bash并让/bin/bash去执行Shel ......
经过上述的几个步骤。第一个目标,代码能够编译通过基本上是不会有什么问题的。只要把握好二个修改代码的基本原则,第一。引入新的代码,而不修改原有的代码。在没有办法进行调试前修改源代码是不允许的,修改的不好就会引起最后代码运行逻辑的混乱,而且在代码能够运行之前是很难发现问题的。所以除非非常有把握,否则不要 ......
Linux I2C核心、总线与设备驱动
注:
在linux2.6.32版本中有这样的代码与注释:
struct i2c_driver {
unsigned int class;
/* Notifies the driver that a new bus has appeared or is about to be
* removed. You should avoid using this if y ......
前言:
我们在这一节将要讨论linux下文件操作的各个函数.
1.文件的创建和读写
2.文件的各个属性
3.目录文件的操作
4.管道文件
--------------------------------------------------------------------------------
1。文件的创建和读写
......
引用:http://blog.chinaunix.net/u/12207/showart_2061214.html
关于2.6.31遇到的问题
错误提示1:
drivers/built-in.o(.init.text+0x3bad): In function `con_init':
include/trace/events/kmem.h:47: undefined reference to `.L1452'
解决:
vi /usr/src/linux/drivers/char/vt.c 找到static int __init con_init ......