• 热门专题

基于libuv库的tcp udpecho服务器和客户段测试python代码

作者:  发布日期:2014-03-05 21:05:09
  • TCP 客户端测试python代码:

    import socket
    import time
    
    def tcpClient():
        for i in range(10000):
            clisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            clisock.connect(('localhost', 7890))
            clisock.send('Hi Doudou')
            dat = clisock.recv(30)
            print dat
            clisock.close()
        print "----------------11111111------------------------" 
        clisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        clisock.connect(('localhost', 7890))
        for i in range(100000):
            clisock.send('Hi Doudou')
            dat = clisock.recv(30)
            print dat
    
    
    
    if __name__ ==  '__main__':
        start = time.time()
        tcpClient()
        print "total time ",  time.time() - start

    UDP端测试python代码:

    import socket
    host = "127.0.0.1"
    port = 7890
    
    print "python UDP echo server test"
    
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    
    s.bind((host, 2346))
    msg = ""
    for i in range(1, 100000):
        if i % 2 == 0:
            msg = "NO:%d hello libuv udp" % i
        else:
            msg = "NO:%d I am here" % i
        s.sendto(msg, (host, port))
        message, address = s.recvfrom(1234)
        print "Got data from", address, ":", message


    TCP和UDP echo服务器代码:

    #include <uv.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <string.h>
    
    
    #define TEST_PORT  7890
    
    typedef struct {
    	uv_write_t req;
    	uv_buf_t buf;
    } write_req_t;
    
    static uv_udp_t udp_sv;
    static uv_udp_t udp_cl;
    static uv_loop_t *loop;
    
    static uv_tcp_t tcp_sv;
    static uv_tcp_t tcp_cl;
    static int con_count;
    
    
    static int udp_echo_start(int port);
    static int tcp_echo_start(int port);
    static uv_buf_t udp_alloc_cb(uv_handle_t* handle, size_t suggested_size);
    static uv_buf_t tcp_alloc_cb(uv_handle_t* handle, size_t suggested_size);
    static void udp_sv_send_cb(uv_udp_send_t* req, int status);
    static void udp_close_cb(uv_handle_t* handle);
    static void on_connection(uv_stream_t* server, int status);
    static void tcp_sv_recv_cb(uv_stream_t* handle, ssize_t nread, uv_buf_t buf);
    static void after_write(uv_write_t* req, int status);
    static void on_close(uv_handle_t* peer);
    static void after_shutdown(uv_shutdown_t* req, int status);
    static void udp_sv_recv_cb(uv_udp_t* handle,
    		       ssize_t nread,
    		       uv_buf_t buf,
    		       struct sockaddr* addr,
                           unsigned flags);
    
    static uv_buf_t udp_alloc_cb(uv_handle_t* handle, size_t suggested_size)
    {
    	static char slab[65536];
    
    	assert(suggested_size <= sizeof slab);
    
    	return uv_buf_init(slab, sizeof slab);
    }
    
    static uv_buf_t tcp_alloc_cb(uv_handle_t* handle, size_t suggested_size)
    {
    	return uv_buf_init(malloc(suggested_size), suggested_size);
    }
    
    static void after_write(uv_write_t* req, int status)
    {
    	write_req_t *wr;
    	uv_err_t err;
    	wr = (write_req_t *) req;
    
    	
    	free(wr->buf.base);
    	free(wr);
    
    	if (status == 0)
    		return;
    	err = uv_last_error(loop);
    	fprintf(stderr, "uv_write_error: %s\n", uv_strerror(err));
    	if (err.code == UV_ECANCELED)
    		return;
    
    	assert(err.code == UV_EPIPE);
    }
    
    static void on_close(uv_handle_t* peer)
    {
    	free(peer);
    }
    
    
    static void after_shutdown(uv_shutdown_t* req, int status)
    {
    	uv_close((uv_handle_t*)req->handle, on_close);
    
    	free(req);
    }
    
    static void tcp_sv_recv_cb(uv_stream_t* handle, ssize_t nread, uv_buf_t buf)
    {
    	int i;
    	uv_shutdown_t *req;
    	write_req_t *wr;
    
    	if (nread < 0) {
    		/* Error or EOF */
    		free(buf.base);    // 忘记在这里释放分配的内存,程序内存泄露了,程序处理几万次读写后挂掉了!!!!
    		assert(uv_last_error(loop).code == UV_EOF);
    		req = (uv_shutdown_t*) malloc(sizeof *req);
    		uv_shutdown(req, handle, after_shutdown);
    		return;
    	}
    
    	if (nread == 0) {
    		/* Everything OK, but nothing read. */
    		return;
    	}
    
    	wr = malloc(sizeof(*wr));
    	assert(wr != NULL);
    
    	buf.len = nread;
    	wr->buf = buf;
    	
    	if (uv_write(&wr->req, handle, &wr->buf, 1, after_write)) {
    		printf("uv_write_failed\n");
    	}
    
    }
    
    static void udp_close_cb(uv_handle_t* handle)
    {
    }
    
    
    static void udp_sv_send_cb(uv_udp_send_t* req, int status)
    {
    	uv_err_t err;
    
    	if (status != 0) {
    		err = uv_last_error(loop);
    		fprintf(stderr, "uv_udp_send error:%s\n", uv_strerror(err));
    	
    	}
    	
    	assert(status == 0);
    //	uv_close((uv_handle_t*) req->handle, udp_close_cb);
    	free(req);
    }
    
    
    static void udp_sv_recv_cb(uv_udp_t* handle,
                           ssize_t nread,
                           uv_buf_t buf,
                           struct sockaddr* addr,
                           unsigned flags)
    {
    	uv_udp_send_t *req;
    	int r;
    	if (nread == 0) {
    		return;
    	}
    //	r = uv_udp_recv_stop(handle);
    //	assert(r == 0);
    	
    	req  = malloc(sizeof(*req));
    	assert(req != NULL);
    	
    	buf.len = nread;
    	r = uv_udp_send(req, handle, &buf, 1, *(struct sockaddr_in *)addr, udp_sv_send_cb);
    	assert(r == 0);
    }
    
    static int udp_echo_start(int port)
    {
    	struct sockaddr_in addr;
    	int r;
    
    	addr = uv_ip4_addr("0.0.0.0", port);
    	r = uv_udp_init(loop, &udp_sv);
    	assert(r == 0);
    
    	r = uv_udp_bind(&udp_sv, addr, 0);
    	assert(r == 0);
    	
    	r = uv_udp_recv_start(&udp_sv, udp_alloc_cb , udp_sv_recv_cb);
    	assert(r == 0);
    	
    	r = uv_udp_init(loop, &udp_cl);
    	assert(r == 0);
    	
    	
    	return 0;
    }
    
    static void on_connection(uv_stream_t* server, int status)
    {
    	uv_stream_t *stream;
    	int r;
    
    	stream = malloc(sizeof(uv_tcp_t));
    	assert(stream != NULL);
    
    	r = uv_tcp_init(loop, (uv_tcp_t*)stream);
    	assert(r == 0);
    	
    	stream->data = server;
    
    	r = uv_accept(server, stream);
    	assert(r == 0);
    
    	r = uv_read_start(stream, tcp_alloc_cb, tcp_sv_recv_cb);
    	assert(r == 0);
    }
    
    static int tcp_echo_start(int port)
    {
    	struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", port);
    	int r;
    
    	r = uv_tcp_init(loop, &tcp_sv);
    	assert(r== 0);
    
    	r = uv_tcp_bind(&tcp_sv, addr);
    	assert(r == 0);
    
    	r = uv_listen((uv_stream_t *)&tcp_sv, SOMAXCONN, on_connection);
    	assert(r == 0);
    	
    	return 0;
    }
    
    int main()
    {
    	loop = uv_default_loop();
    	
    	if (tcp_echo_start(TEST_PORT))
    		return 1;
    
    	if (udp_echo_start(TEST_PORT))
    		return 1;
    	
    	uv_run(loop, UV_RUN_DEFAULT);
    	return 0;
    }

    在写TCP部分的时候在处理客户端关闭连接时忘记释放读内存,结果程序内存泄露,处理几万个读时间后程序刮掉了,非配内存返回0。

延伸阅读:

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规