[SRILM User List] Did the network protocol change in srilm 1.7?

Andreas Stolcke stolcke at icsi.berkeley.edu
Fri Jan 25 00:21:58 PST 2013


On 1/24/2013 7:34 AM, Lars Jørgen Solberg wrote:
> Hi
>
> I have a project where I use the server functionality in srilm, which 
> worked fine until i upgraded to version 1.7.
>
> What I do can be outlined as (code snippets are in python):
>
> 1 launch a server
> $ ngram -server-port 5000 ...
>
> 2 connect to the server
> conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> conn.connect(("localhost", 5000))
>
> 3 send a bunch of ngrams separated by newlines
> conn.sendall('A A A\nB B B\nC C C\n')
>
> 4 recieve the probabilites
> reply = conn.recv(4096)
>
> This works fine in version 1.6, but in 1.7 the reply only contains one 
> probability.

The very first code implementing the LM server used stdio library calls 
to receive/send data over the network socket.
This happened to allow the behavior you want, but was never intended (or 
documented).  It is also not portable (e.g., to Windows sockets).
When the code was rewritten (before 1.7, actually) to use recv/send 
system calls this no longer worked.

However, I see why you would want to send batches of requests, so this 
seems like a useful thing to support.
I modified the code to do this, and the patch is attached.  Please let 
me know how it works for you.

Andreas

-------------- next part --------------
*** lm/src/LM.cc	4 Dec 2012 20:58:45 -0000	1.92
--- lm/src/LM.cc	25 Jan 2013 08:07:58 -0000	1.93
***************
*** 14,21 ****
--- 14,23 ----
  #include <math.h>
  #include <ctype.h>
  #include <assert.h>
+ #include <string>
  #include "TLSWrapper.h"
  #include "tserror.h"
+ #include "MStringTokUtil.h"
  
  #if !defined(_MSC_VER) && !defined(WIN32)
  #include <unistd.h>
***************
*** 1148,1165 ****
  		exit(-1);
  	    }
  
! 	    char line[REMOTELM_MAXREQUESTLEN + 1];
  	    int msgLen;
  	    unsigned protocolVersion = 1;
  
! 	    while ((msgLen = recv(client, line, sizeof(line)-1, 0)) != SOCKET_ERROR) {
  		if (msgLen == 0) break;
  
! 		line[msgLen] = '\0';
  
  		if (debug(DEBUG_PRINT_WORD_PROBS)) {
  		    dout() << "client " << clientPort << "@" << clientName 
! 			   << ": " << line;
  		}
  
  		VocabString words[maxWordsPerLine + 2];
--- 1150,1178 ----
  		exit(-1);
  	    }
  
! 	    char msgBuffer[REMOTELM_MAXREQUESTLEN + 1];
  	    int msgLen;
  	    unsigned protocolVersion = 1;
  
! 	    while ((msgLen = recv(client, msgBuffer, sizeof(msgBuffer)-1, 0)) != SOCKET_ERROR) {
  		if (msgLen == 0) break;
  
! 		msgBuffer[msgLen] = '\0';
! 		string response = "";
  
+ 		char *strtok_ptr = NULL;
+ 		char *line;
+ 		
+ 		/*
+ 		 * Break message into commands, one per line
+ 		 */
+ 		for (line = MStringTokUtil::strtok_r(msgBuffer, "\n", &strtok_ptr);
+ 		     line != 0;
+ 		     line = MStringTokUtil::strtok_r(0, "\n", &strtok_ptr))
+ 		{
  		    if (debug(DEBUG_PRINT_WORD_PROBS)) {
  			dout() << "client " << clientPort << "@" << clientName 
! 			       << ": " << line << endl;
  		    }
  
  		    VocabString words[maxWordsPerLine + 2];
***************
*** 1252,1266 ****
  			sprintf(outbuf, "%s command unknown\n", REMOTELM_ERROR);
  		    }
  
! 		    if (send(client, outbuf, strlen(outbuf), 0) == SOCKET_ERROR) {
  			cerr << "client " << clientPort << "@" << clientName
  			     << ": send: " << SOCKET_ERROR_STRING << endl;
  			exit(-1);
  		    }
  
  		    if (debug(DEBUG_PRINT_WORD_PROBS)) {
! 			dout() << outbuf;
! 		    }
  		}
  	    }
  
--- 1265,1286 ----
  			    sprintf(outbuf, "%s command unknown\n", REMOTELM_ERROR);
  			}
  
! 			/*
! 			 * Concatenate responses for all commands in the message
! 			 */
! 			response += outbuf;
! 		    }
! 
! 		}
! 
! 		if (send(client, response.c_str(), response.length(), 0) == SOCKET_ERROR) {
  		    cerr << "client " << clientPort << "@" << clientName
  			 << ": send: " << SOCKET_ERROR_STRING << endl;
  		    exit(-1);
  		}
  
  		if (debug(DEBUG_PRINT_WORD_PROBS)) {
! 		    dout() << response;
  		}
  	    }
  


More information about the SRILM-User mailing list