[R] R socket communication

Philippe Grosjean phgrosjean at sciviews.org
Sun Feb 5 19:01:29 CET 2006


See package svSocket in the SciViews bundle. It uses a socket server 
written in tcltk and it is not blocking the command line.

Philippe Grosjean

pau carre wrote:
> Hello, I tried to code a R socket server but I did not succeed.
> The problem is that once the R socket server is created,
> I call the readLines function and then R gets blocked.
> The client seems to work fine since I tested it with a PERL server.
> I tried many combination of params in the socketConnection but
> none of them worked.
> 
> I have seen some examples where the server sends something to a
> client but I need the opposite example.
> 
> Thanks,
> Pau.
> 
> The R server
> 
> FSsocket() <- function(){
> print("Creating server on localhost")
> conn <- socketConnection(server = TRUE, port = 7890, open = "r+")
> print("Reading data")
> aa <- readLines(conn)
> print("end reading lines")
> close(conn)
> pirnt("Connection closed")
> }
> Note: I receive a "Creating server on localhost" and a "Reading data"
> in the R console, but nothing else.
> 
> The PERL client
> 
> #! /usr/bin/perl
> use strict;
> use Socket;
> 
> # initialize host and port
> my $host = shift || 'localhost';
> my $port = shift || 7890;
> 
> my $proto = getprotobyname('tcp');
> 
> my $iaddr = inet_aton($host);
> my $paddr = sockaddr_in($port, $iaddr);
> 
> socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
> connect(SOCKET, $paddr) or die "connect: $!";
> print SOCKET "Hello \n";
> close SOCKET or die "close: $!";
> 
> The PERL server
> 
> #! /usr/bin/perl -w
> 
> use strict;
> use Socket;
> 
> my $port = shift || 7890;
> my $proto = getprotobyname('tcp');
> 
> socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
> setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";
> 
> my $paddr = sockaddr_in($port, INADDR_ANY);
> 
> bind(SERVER, $paddr) or die "bind: $!";
> listen(SERVER, SOMAXCONN) or die "listen: $!";
> print "SERVER started on port $port ";
> 
> my $client_addr;
> while ($client_addr = accept(CLIENT, SERVER))
> {
> my ($client_port, $client_ip) = sockaddr_in($client_addr);
> my $client_ipnum = inet_ntoa($client_ip);
> my $client_host = gethostbyaddr($client_ip, AF_INET);
> print "Connection from: $client_host","[$client_ipnum] ";
> while(<CLIENT>){
> print "$_";
> }
> close CLIENT;
> }
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list