[R] R socket communication

From: pau carre <pau.carre_at_gmail.com>
Date: Mon 06 Feb 2006 - 04:50:59 EST

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.


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")
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@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 Received on Mon Feb 06 06:08:35 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:24 EST