예제 #1
0
int main(int argc, char *argv[]) {

    init_slog("hw_server", "config/log.cfg", 3, NULL);
    slog(0, SLOG_INFO, "**************************************************");
    slog(0, SLOG_INFO, "HomeWatch Server v1.0, &copy 2015, Matthew Goulart");
    slog(0, SLOG_INFO, "**************************************************");
    //Configure buffer to output immediately
    setvbuf(stdout, 0, _IONBF, 0);

    //Mysql version and initializations
    slog(0, SLOG_INFO, "MySQL client version: %s", mysql_get_client_info());

    //Initialize the FDs for the sockets
    int socketfd, clientfd;

    //Initialize the list of FDs to monitor for input
    fd_set fdMonitor;

    //Initialize timer for select()
    struct timeval select_timeout;
    select_timeout.tv_sec = 3;
    select_timeout.tv_usec = 0;

    //Initialize the thread id
    pthread_t thread;

    slog(0, SLOG_INFO, "Starting server...");
    slog(0, SLOG_INFO, "Creating listen socket...");

    //Create the listen socket
    socketfd = create_inet_server_socket("0.0.0.0", "5677", LIBSOCKET_TCP,
                                         LIBSOCKET_IPv4, 0);

    if (socketfd < 0) {
        slog(2, SLOG_ERROR, "Error creating listen socket!");
        return -1;
    } else {
        slog(0, SLOG_INFO, "Listening...");
    }

    while (1) {
        FD_ZERO(&fdMonitor);
        FD_SET(socketfd, &fdMonitor);
        FD_SET(STDIN_FILENO, &fdMonitor);
        select_timeout.tv_sec = 3;
        select_timeout.tv_usec = 0;

        select(sizeof(fdMonitor) * 8, &fdMonitor, NULL, NULL,
               &select_timeout);

        if (FD_ISSET(socketfd, &fdMonitor)) {
            clientfd = accept_inet_stream_socket(socketfd, 0, 0, 0, 0, 0, 0);
            pthread_create(&thread, NULL, async_sensor_communicate, clientfd);
        }
    }

    return 0;

}
예제 #2
0
int main(void)
{
    int cfd, sfd, ret;
    char* buf = calloc(16,1);

    ret = sfd = create_inet_server_socket("::","55555",LIBSOCKET_TCP,LIBSOCKET_IPv6,0);

    if ( ret < 0 )
    {
	perror(0);
	exit(1);
    }

    ret = cfd = accept_inet_stream_socket(sfd,0,0,0,0,0,0);

    if ( ret < 0 )
    {
	perror(0);
	exit(1);
    }

    ret = read(cfd,buf,15);

    if ( ret < 0 )
    {
	perror(0);
	exit(1);
    }

    printf("%s\n",buf);

    ret = destroy_inet_socket(sfd);

    if ( ret < 0 )
    {
	perror(0);
	exit(1);
    }

    return 0;
}