R socket communication

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

R socket communication

pau carre
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;
}

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: R socket communication

Philippe Grosjean
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;
> }
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: R socket communication

pau carre
In reply to this post by pau carre
Hello, thank you for your help but I need to use the standard R
packages as I am developing an academic project and I the use of
external R packages is not allowed.
Pau.

2006/2/5, Philippe Grosjean <[hidden email]>:

> 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;
> > }
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >
> >
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html