⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 libs-server.c

📁 Language, Script, and Encoding Identification with String Kernel Classifiers
💻 C
字号:
/*** Copyright (C) 2006 Thai Computational Linguistics Laboratory (TCL)** National Institute of Information and Communications Technology (NICT)** Canasai Kruengkrai <canasai xx gmail yy com, where xx=at and yy=dot>**** This file is part of the `libs' library.**** This library is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.**** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the** GNU General Public License for more details.**** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <unistd.h>#include <sys/types.h>   // definitions of a number of data types used in system calls#include <sys/socket.h>  // definitions of structures needed for sockets#include <netinet/in.h>  // constants and structures needed for internet domain addresses#include <sys/signal.h>#include "../svm/libsvm-string-2.71/svm.h"#include "libs-client-server.h"#define TIMEOUT 10svm_model *model;/**   *  There is a separate instance of this function for each connection. *  It handles all communication once a connnection has been established. */void do_prediction( int sock ){    int i, n;    char string[MAX_LINE_LEN];    // Initialize the buffer, and then read from the socket    bzero( string, MAX_LINE_LEN );    alarm( TIMEOUT );    n = read( sock, string, MAX_LINE_LEN-1 );    if( n <= 0 )         error(" ERROR: reading from socket" );    //--------------------------- SVM start --------------------------------    fprintf( stderr, "# libs-server gets: `%s'\n", string );    int str_length = strlen( string );    svm_node *x = Malloc( svm_node, str_length + 1 );  // add 1 for the termination index    for( i = 0; i < str_length; i++ )    {        x[i].index = i;        x[i].value = ( unsigned char )string[i];     }    x[i++].index = -1;    double v = svm_predict( model, x );    //double v = dagsvm_predict( model, x );    int index = ( int )v;    fprintf( stderr, "# libs-server predicts: (%d, %s)\n\n", index, label[index] );    //free( x );    //--------------------------- SVM end ----------------------------------    // Write a short message to the client    n = write( sock, label[index], strlen( label[index] ) );    if( n < 0 )       error( "ERROR writing to socket" );}int main( int argc, char *argv[] ){    int sockfd, newsockfd, portno, pid;    socklen_t clilen;    struct sockaddr_in serv_addr, cli_addr;    zombie_handler();    if( argc < 4 )     {        fprintf( stderr, "Usage: %s port model_file label_file\n", argv[0] );        exit( 1 );    }    // Create a new socket    fprintf( stderr, "# create a new socket..." );    if( ( sockfd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )       error( "ERROR opening socket" );    // Deal with "Address already in use"    int yes = 1;    if( setsockopt( sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof( int ) ) < 0 )        error( "setsockopt" );    fprintf( stderr, "success\n" );    // Set all values in a buffer to zero    bzero( ( char * )&serv_addr, sizeof( serv_addr ) );    // Set the port number on which the server will listen for connections    portno = atoi( argv[1] );    fprintf( stderr, "# set the port number [%d]...", portno );    // Set serv_addr which is a structure containing an internet address    serv_addr.sin_family = AF_INET;    serv_addr.sin_addr.s_addr = INADDR_ANY;    serv_addr.sin_port = htons( portno );    // Bind a socket to an address    if( bind( sockfd, ( struct sockaddr * )&serv_addr, sizeof( serv_addr ) ) < 0 )         error( "ERROR on binding" );    fprintf( stderr, "success\n" );    //--------------------------------- SVM start ----------------------------------    fprintf( stderr, "# initialize the svm model [%s]...", argv[2] );    if( ( model = svm_load_model( argv[2] ) ) == 0 )    {        fprintf( stderr, "ERROR: can't open model file %s\n", argv[2] );        exit( 1 );    }     load_label( argv[3] );    fprintf( stderr, "success\n" );    //--------------------------------- SVM end -------------------------------------    // Allow the process to listen on the socket for connections    fprintf( stderr, "# listen on the port [%d] for connections...\n\n", portno );    listen( sockfd, 5 );     // Block the process until a client connects to the server    clilen = sizeof( cli_addr );    while( 1 )     {        newsockfd = accept( sockfd, ( struct sockaddr * )&cli_addr, &clilen );        if( newsockfd < 0 )             error( "ERROR on accept" );        // Create a new process        pid = fork();        if( pid < 0 )            error( "ERROR on fork" );        if( pid == 0 )          {           //  The child process will close sockfd and call dostuff           close( sockfd );           do_prediction( newsockfd );           exit( 0 );        }         else         {           // The parent process closes newsockfd           close( newsockfd );        }    }     return( 0 ); }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -