linux socket系统调用的错误码及含义

linux socket系统调用socket,accept,bind,listen,connect,send,recv等函数出错时,errno的值及其含义对应如下:

EAGAIN: Resource temporarily unavailable
EWOULDBLOCK: Resource temporarily unavailable
EBADF: Bad file descriptor
ECONNREFUSED: Connection refused
EFAULT: Bad address
EINTR: Interrupted system call
EINVAL: Invalid argument
ENOMEM: Cannot allocate memory
ENOTCONN: Transport endpoint is not connected
ENOTSOCK: Socket operation on non-socket
EACCES: Permission denied
ECONNRESET: Connection reset by peer
EDESTADDRREQ: Destination address required
EMSGSIZE: Message too long
ENOBUFS: No buffer space available
EOPNOTSUPP: Operation not supported
EPIPE: Broken pipe
EPERM: Operation not permitted
EADDRINUSE: Address already in use
EAFNOSUPPORT: Address family not supported by protocol
EALREADY: Operation already in progress
EINPROGRESS: Operation now in progress
EISCONN: Transport endpoint is already connected
ENETUNREACH: Network is unreachable
ETIMEDOUT: Connection timed out
EMFILE: Too many open files
ENFILE: Too many open files in system
EPROTO: Protocol error
EADDRINUSE: Address already in use
EADDRNOTAVAIL: Cannot assign requested address
ELOOP: Too many levels of symbolic links
ENAMETOOLONG: File name too long
ENOENT: No such file or directory
ENOTDIR: Not a directory
EROFS: Read-only file system
EPROTONOSUPPORT: Protocol not supported

Continue reading

为Node.js的express框架加入session的memcache存储支持

需要的依赖:connect-memcached

var express = require('express');
var app = express.createServer();
var mcStore = require('connect-memcached')(express);
app.use(express.cookieParser());//必须,用于解析cookie
var mc = new mcStore({hosts: "localhost:11211"});
app.use(
	express.session(
			{
				key: "zensessid", //这里必须为全小写,否则每次都会构造一个新的session id和session给用户。这可能是express的一个bug,原理可能是express根据该key转小写后的字符串去查找客户端cookie里的该键值对。
				secret: "zensess", //加密字符串
				store:mc
			}
		)
);
app.get('/',function(req,res){
	req.session.web = 'http://z-en.com';
});

Node.js连接Buffer对象的正确方法

万能的适应各种编码而且正确的拼接Buffer对象的方法是什么呢?我们从Node.js在github上的源码中找出这样一段正确读取文件,并连接buffer对象的方法

var buffers = [];
var nread = 0;
readStream.on('data', function (chunk) {
    buffers.push(chunk);
    nread += chunk.length;
});
readStream.on('end', function () {
    var buffer = null;
    switch(buffers.length) {
        case 0: buffer = new Buffer(0);
            break;
        case 1: buffer = buffers[0];
            break;
        default:
            buffer = new Buffer(nread);
            for (var i = 0, pos = 0, l = buffers.length; i < l; i++) {
                var chunk = buffers[i];
                chunk.copy(buffer, pos);
                pos += chunk.length;
            }
        break;
    }
});

Continue reading

Windows的信号(Signals)

// Signal types
#define SIGINT 2 // interrupt(Ctrl+C中断)
#define SIGILL 4 // illegal instruction – invalid function image(非法指令)
#define SIGFPE 8 // floating point exception(浮点异常)
#define SIGSEGV 11 // segment violation(段错误)
#define SIGTERM 5 // Software termination signal from kill(Kill发出的软件终止)
#define SIGBREAK 21 //Ctrl-Break sequence(Ctrl+Break中断)
#define SIGABRT 22 // abnormal termination triggered by abort call(Abort)