gpt4 book ai didi

r - R中的套接字编程以接收UDP流

转载 作者:行者123 更新时间:2023-12-04 11:02:07 26 4
gpt4 key购买 nike

在 Python 中,我能够为套接字编程编写代码以通过 UDP 接收数据流。

但是,如何在 R 中使用等效代码来执行相同操作?

import socket, traceback   
host = ''
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host, port))

counter = 1500
while counter > 0:
counter -= 1
try:
message, address = s.recvfrom(8192)
message = message.decode()
data = message.split(",")
print(data)


except (KeyboardInterrupt, SystemExit):
raise
except:
traceback.print_exc()

在 R 中,我尝试使用以下代码,但没有成功。我确实意识到我需要在某处提及它是 UDP 和所有内容,但无法找到这些设置。

我需要从设备接收数据的“流”。
我需要为此使用 R-server 吗?

平台 x86_64-pc-linux-gnu
操作系统 linux-gnu
version.string R 版本 3.2.3 (2015-12-10)
RStudio 版本 1.0.44
server <- function(){
while(TRUE){
writeLines("Listening...")
con <- socketConnection(host="localhost", port = 5555, blocking=TRUE,server=TRUE, open="r+")
data <- readLines(con, 1)
print(data)
close(con)
}
}

server()

最佳答案

杰夫,

我的感觉是直接用 C/C++ 编写代码,并通过

  • R Rcpp package .

  • 使用上述方法,您可以创建一个模仿 python 功能的包,或者直接将 python 代码包装在 RCPP 包中。最后一点将在以下教程中介绍
  • Call Python from R through RCPP

  • 我希望上面的解释和下面的代码示例能帮助您指明正确的方向。

    rcpp代码示例:

    监听器
    setwd("~/dev/stackoverflow/40896072")

    # install.packages("installr")
    # require(installr)
    # install.Rtools()
    # install.packages("Rcpp")
    # # Test and Verify
    # Rcpp::evalCpp("2+2")

    Rcpp::sourceCpp( file = "./listener.cc")

    listen()

    听众.cc
    /* listener.c - a datagram socket 'server'
    * simply displays message received then dies!
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>

    #include <Rcpp.h>
    using namespace Rcpp;

    #define MYPORT 5555 /* the port users connect to */
    #define MAXBUFLEN 100

    // [[Rcpp::export]]
    int listen() {

    int sockfd;
    struct sockaddr_in my_addr; /* info for my addr i.e. server */
    struct sockaddr_in their_addr; /* client's address info */
    socklen_t addr_len;
    ssize_t numbytes;
    char buf[ MAXBUFLEN];

    printf( "Running\n");

    if( (sockfd = socket( AF_INET, SOCK_DGRAM, 0)) == -1) {
    perror( "Listener socket");
    exit( 1);
    }

    memset( &my_addr, 0, sizeof( my_addr)); /* zero struct */
    my_addr.sin_family = AF_INET; /* host byte order ... */
    my_addr.sin_port = htons( MYPORT); /* ... short, network byte order */
    my_addr.sin_addr.s_addr = INADDR_ANY; /* any of server IP addrs */
    if( bind( sockfd, (struct sockaddr *)&my_addr,
    sizeof( struct sockaddr)) == -1) {
    perror( "Listener bind");
    exit( 1);
    }
    addr_len = sizeof( struct sockaddr);

    if( (numbytes = recvfrom( sockfd, buf, MAXBUFLEN - 1, 0,
    (struct sockaddr *) &their_addr, &addr_len)) == -1) {
    perror( "Listener recvfrom");
    exit( 1);
    }

    printf( "Got packet from %s\n", inet_ntoa( their_addr.sin_addr));
    printf( "Packet is %zd bytes long\n", numbytes);
    buf[ numbytes] = '\0'; /* end of string */
    printf( "Packet contains \"%s\"\n", buf);
    close( sockfd);
    return 0;
    }

    客户端
    setwd("~/dev/stackoverflow/40896072")

    # install.packages("installr")
    # require(installr)
    # install.Rtools()
    # install.packages("Rcpp")
    # # Test and Verify
    # Rcpp::evalCpp("2+2")

    Rcpp::sourceCpp( file = "./client.cc")

    client()

    客户端.cc
    /* client.c - a datagram 'client'
    */

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h> /* for gethostbyname() */

    //#include <Rcpp.h>
    //using namespace Rcpp;

    #define PORT 5555 /* server port the client connects to */

    // [[Rcpp::export]]
    int client()
    {
    socklen_t sockfd;
    ssize_t numbytes;
    struct hostent *he;
    struct sockaddr_in their_addr; /* server address info */
    /* resolve server host name or IP address */
    if ( (he = gethostbyname("localhost")) == NULL )
    {
    perror( "Talker gethostbyname");
    exit( 1);
    }

    if ( (sockfd = socket( AF_INET, SOCK_DGRAM, 0)) == -1)
    {
    perror( "Talker socket");
    exit( 1);
    }

    memset( &their_addr,0, sizeof(their_addr) ); /* zero struct */
    their_addr.sin_family = AF_INET; /* host byte order .. */
    their_addr.sin_port = htons( PORT); /* .. short, netwk byte order */
    their_addr.sin_addr = *( (struct in_addr *)he -> h_addr);

    if( (numbytes = sendto( sockfd, "Hello", strlen("Hello"), 0,
    (struct sockaddr *) &their_addr,
    sizeof( struct sockaddr))) == -1)
    {
    perror( "Talker sendto");
    exit( 1);
    }

    printf( "Sent %zd bytes to %s\n", numbytes,
    inet_ntoa( their_addr.sin_addr));

    close( sockfd );
    return 0;
    }

    运行时输出

    enter image description here

    关于r - R中的套接字编程以接收UDP流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40896072/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com