errno

Why can't you just check if errno is equal to ERANGE?

人盡茶涼 提交于 2020-01-10 03:57:09
问题 I've been trying to properly convert a char array to a long with strtol , check if there was an overflow or underflow and then do an int cast on the long. Along the way, I've noticed a lot of code that looks like this if ((result == LONG_MAX || result == LONG_MIN) && errno == ERANGE) { // Handle the error } Why can you not just say if(errno == ERANGE) { // Handle the error } From my understanding, if an underflow or overflow occur, errno is set to ERANGE in both cases. So is the former really

libevent中的bufferevent原理

你离开我真会死。 提交于 2020-01-10 03:27:14
以前的文章看过缓冲区buffer了,libevent用bufferevent来负责管理缓冲区与buffer读写事件。 今天就带大家看下evbuffer.c,使用bufferevent处理事件的数据,是buffer和event的综合。在最后用一个稍微综合的例子看下使用bufferevent的整个流程。   首先依旧看下bufferevent的结构。结构很清晰。源码版本1.4.14。    1 struct bufferevent { 2 struct event_base *ev_base; 3 4 //读事件 5 struct event ev_read; 6 //写事件 7 struct event ev_write; 8 //读缓冲区,输入缓冲 9 struct evbuffer *input; 10 //写缓冲区,输出缓冲 11 struct evbuffer *output; 12 13 //读水位 14 struct event_watermark wm_read; 15 //写水位 16 struct event_watermark wm_write; 17 18 //发生读触发用户设置的回调 19 evbuffercb readcb; 20 //发生写触发用户设置的回调 21 evbuffercb writecb; 22 //发生错误触发用户设置的回调 23

Linux socket.error: [Errno 99] Cannot assign requested

纵然是瞬间 提交于 2020-01-08 12:24:21
Python 使用 from pymongo import MongoClient 连接mongodb,由于没有关闭连接,导致 netstat -alnt|wc -l 达到几万个 socket.error: [Errno 99] Cannot assign requested address 网上你去搜,基本都是说bind的时候,地址已经被用了,都是胡扯。地址被用报的错误应该是: Address already in use才对 然后我看得都是英文的,说明外国人也不是想象中的那么一丝不苟, 言归正传。socket发起connect请求的时候会随机分配一个端口给你。这个分配的端口是有范围的,记录在: /proc/sys/net/ipv4/ip_local_port_range 这个文件里面(fedora 17).当你用多个进程发起过多的请求的时候,端口用完了就会报这个错误。比如我就开了4个进程,一下发起了40000个请求。 你可以做个实验试试,切换到root用户,敲一下这条命令: echo 32768 32769 > /proc/sys/net/ipv4/ip_local_port_range 这下你打开人人,微博就会发现很多图片加载不出来了。因为图片加载在浏览器里面就是并行加载的,由于你没有足够的端口数,所以图片加载都失败了。别当心,这个修改是临时的(是不是临时的我也不知道

epoll中的LT和ET读写方式

三世轮回 提交于 2020-01-07 15:11:43
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 可读可写状态 1. 下列四个条件中的任何一个满足时,一个套接口准备好读: a. 该套接口接收缓冲区中的数据字节数 大等于 套接口接收缓冲区的低潮标记,对于 TCP 和 UDP 而言,其缺省值为 1 ; b. 该连接的读这一半关闭,对这样的套接口的读操作将不阻塞并返回 0 ; c. 该套接口是一个监听套接口且已完成的连接数不为 0 ;(就是 accept 成功返回 ) d. 其上有一个套接口错误等待处理,对这样的套接口的读操作将不阻塞并返回 -1 ; 2. 下列四个条件中的任何一个满足时,一个套接口准备好写: a. 该套接口发送缓冲区中的可用空间字节数 大等于 套接口发送缓冲区的低潮标记,对于 TCP 和 UDP 而言,其缺省值为 2048 ; b. 该连接的写这一半关闭,对这样的套接口的写操作将产生 SIGPIPE 信号; c. 该套接口早先使用非阻塞式 connect 以建立连接,并且连接已经异步建立,或者 connect 以失败告终; d. 其上有一个套接口错误等待处理,对这样的套接口的写操作将不阻塞并返回 -1 ; 3.select 与非阻塞 connect 一起使用的时候有以下两个注意点:( 1 )当连接成功建立时,描述字变为可写;( 2 )当连接建立遇到错误时,描述字变为既可读又可写;

Python语法速查: 10. 异常

…衆ロ難τιáo~ 提交于 2020-01-07 08:08:20
/*--> */ /*--> */ 返回目录 本篇索引 (1) 内置异常 (2) 自定义异常 (3) 主动引发异常 (4) 捕捉异常 (5) error模块 (6) with语句 (1)内置异常 ● 异常的基类: 以下这些异常作为具体异常的基类,都不会被显式引发,但是可以使用它们捕捉某种错误。 基类名称 说明 BaseException 所有内置异常的基类,其他所有内置异常都派生自该类。 Exception 所有内置的非系统退出异常都派生自此类(即除了:SystemExit, GeneratorExit, KeyboardInterrupt之外的所有内置异常)。所有用户自定义异常也应当派生自此类。 ArithmeticError 算术运算异常的基类,包括溢出、除零等等。 LookupError 索引和键错误的基类。 BufferError 当与缓冲区相关的操作无法执行时引发,一般由用户自行继承使用。 ● 具体异常 以下异常属于经常被引发的异常。 具体异常名称 说明 以下异常直接继承自: BaseException GeneratorExit 由生成器的.close()方法引发。 KeyboardInterrupt 由键盘中断(通常为 Ctrl-C)生成。 SystemExit 程序退出,一般系统函数sys.exit()引发。 以下异常继承自: BaseException ->

linux下错误的捕获:errno和strerror的使用

守給你的承諾、 提交于 2020-01-07 06:53:49
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因。这个时候使用errno这个全局变量就相当有用了。 在程序代码中包含 #include <errno.h>,然后每次程序调用失败的时候,系统会自动用用错误代码填充errno这个全局变量,这样你只需要读errno这个全局变量就可以获得失败原因了。 例如: #include <stdio.h> #include <string.h> #include <errno.h> int main(void) { int fd; extern int errno; if((fd = open("/dev/dsp",O_WRONLY)) < 0) { printf("errno=%d\n",errno); } exit(0); } 如果dsp设备忙的话errno值将是16。 errno.h中定义的错误代码值如下: 查 看错误代码errno是调试程序的一个重要方法。当linuc C api函数发生异常时,一般会将errno变量(需include errno.h)赋一个整数值,不同的值表示不同的含义,可以通过查看该值推测出错的原因

“'errno' undeclared” when compile Linux kernel

坚强是说给别人听的谎言 提交于 2020-01-07 02:58:03
问题 I'm trying to compile and I keep getting the following error: enter image description here I've included the asm-i386/errno.h once and it didn't work. Also I've tried including linux/errno.h and it didn't work ether. What file should I include? 回答1: There is no errno variable in Linux kernel: this variable lives in user space only. If kernel function wants to report about the error and specify the error code, it encapsulates the error code into the returning value. There are 3 possibilities

Getting “Illegal Seek” error after calling accept()

家住魔仙堡 提交于 2020-01-06 02:55:08
问题 Well.. it's pretty much that, I seem to be getting a "Illegal Seek" error when checking my errno variable. The problem is that I have no idea of what that can mean. I know sockets are treated like files in unix, but I can't see how can this be related to sockets. What I'm doing exactly is: int sck = ::accept(m_socket, (struct sockaddr*)&client_address, (socklen_t*)&address_len); Then I get sck = -1 and errno = ESPIPE And the weird thing is that it happens randomly. I mean, sometimes the code

Server Socket errno 57 - Socket is not connected

痞子三分冷 提交于 2020-01-05 04:15:11
问题 For some reason i am getting error an 57 - socket is not connected. Why? Console output: GET / HTTP/1.1 Host: localhost:8080 Connection: keep-alive Cache-Control: max-age=0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8,it;q=0.6,lt;q=0.4,nl;q=0.2,ru;q=0.2 Can't

C语言中的异常处理

主宰稳场 提交于 2020-01-02 12:24:48
一 前言: 异常处理,对于做面向对象开发的开发者来说是再熟悉不过了,例如在C#中有 try { ... } catch( Exception e){...} finally{ ..... } 在C++中,我们常常会使用 try{} ... catch(){} 块来进行异常处理。 说了那么多,那么到底什么是异常处理呢? 异常处理(又称为错误处理)功能提供了处理程序运行时出现的任何意外或异常情况的方法。 异常处理一般有两种模型,一种是"终止模型",一种是"恢复模型" "终止模型": 在这种模型中,将假设错误非常关键,将以致于程序无法返回到异常发生的地方继续执行.一旦异常被抛出,就表明错误已无法挽回,也不能回来继续执行. "恢复模型": 异常处理程序的工作是修正错误,然后重新尝试调动出问题的方法,并认为的二次能成功. 对于恢复模型,通常希望异常被处理之后能继续执行程序.在这种情况下,抛出异常更像是对方法的调用--可以在Java里用这种方法进行配置,以得到类似恢复的行为.(也就是说,不是抛出异常,而是调用方法修正错误.)或者,把try块放在while循环里,这样就可以不断的进入try块,直到得到满意的结果. 二 面向对象中的异常处理 大致了解了什么是异常处理后,由于异常处理在面向对象语言中使用的比较普遍,我们就先以C++为例,做一个关于异常处理的简单例子: 问题:求两个数相除的结果。 这里