Пример #1
0
static int STDCALL http_server_thread(void* param)
{
	char req[1024] = {0};
	bool* running = (bool*)param;
	socket_t socket = tcpserver_create(NULL, PORT, 32);

	while(*running)
	{
		int r = socket_select_read(socket, 1000);
		if(1 == r)
		{
			struct sockaddr_in in4;
			socklen_t len = sizeof(in4);
			socket_t client = socket_accept(socket, (sockaddr*)&in4, &len);
			if(client != socket_invalid)
			{
				r = socket_recv_by_time(client, req, sizeof(req), 0, 5000);
				r = socket_send_all_by_time(client, s_reply, strlen(s_reply), 0, 5000);
				r = socket_shutdown(client, SHUT_RDWR);
				r = socket_close(client);
				printf("server side close socket\n");
			}
		}
	}

	return 0;
}
Пример #2
0
void* http_server_create(const char* ip, int port)
{
	socket_t socket;
	struct http_server_t *ctx;
	struct aio_tcp_transport_handler_t handler;

	// create server socket
	socket = tcpserver_create(ip, port, 64);
	if(socket_invalid == socket)
	{
		printf("http_server_create(%s, %d): create socket error.\n", ip, port);
		return NULL;
	}

	ctx = (struct http_server_t*)malloc(sizeof(ctx[0]));
	if(!ctx)
		return NULL;

	memset(ctx, 0, sizeof(ctx[0]));
	handler.onconnected = http_session_onconnected;
	handler.ondisconnected = http_session_ondisconnected;
	handler.onrecv = http_session_onrecv;
	handler.onsend = http_session_onsend;
	ctx->transport = aio_tcp_transport_create(socket, &handler, ctx);
	if(!ctx->transport)
	{
		printf("http_server_create(%s, %d) create aio transport error.\n", ip, port);
		http_server_destroy(ctx);
		return NULL;
	}

	return ctx;
}
Пример #3
0
void* rtsp_server_create(const char* ip, int port, struct rtsp_handler_t* handler, void* ptr)
{
	socket_t socket;
	struct rtsp_server_context_t* ctx;
	struct rtsp_transport_handler_t tcphandler;
	struct rtsp_transport_handler_t udphandler;

	tcphandler.onrecv = rtsp_server_onrecv;
	tcphandler.onsend = rtsp_server_onsend;
	udphandler.onrecv = rtsp_server_onrecv;
	udphandler.onsend = rtsp_server_onsend;

	ctx = (struct rtsp_server_context_t *)malloc(sizeof(struct rtsp_server_context_t));
	if(!ctx) return NULL;

	memset(ctx, 0, sizeof(struct rtsp_server_context_t));
	memcpy(&ctx->handler, handler, sizeof(ctx->handler));
	ctx->tcptransport = rtsp_transport_tcp();
	ctx->udptransport = rtsp_transport_udp();
	ctx->ptr = ptr;

	// tcp
	socket = tcpserver_create(ip, port, 128);
	if(socket_invalid == socket)
	{
		rtsp_server_destroy(ctx);
		return NULL;
	}
	ctx->tcp = ctx->tcptransport->create(socket, &tcphandler, ctx);
	if(!ctx->tcp)
	{
		socket_close(socket);
		rtsp_server_destroy(ctx);
		return NULL;
	}

	// udp
	socket = udpsocket_create(ip, port);
	if(socket_invalid == socket)
	{
		rtsp_server_destroy(ctx);
		return NULL;
	}
	ctx->udp = ctx->udptransport->create(socket, &udphandler, ctx);
	if(!ctx->udp)
	{
		socket_close(socket);
		rtsp_server_destroy(ctx);
		return NULL;
	}

	return ctx;
}
Пример #4
0
static int StartTcpServer(const char* ip, int port)
{
    socket_t sock = tcpserver_create(ip, port, 256);
    if(0 == sock)
    {
        printf("server listen at %s.%d error: %d\n", ip?ip:"127.0.0.1", port, socket_geterror());
        return -1;
    }

    printf("server listen at %s:%d\n", ip?ip:"localhost", port);
    s_tcp = aio_socket_create(sock, 1);
    return aio_socket_accept(s_tcp, OnAccept, NULL); // start server
}