-2

这是服务器端代码(树莓派)

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#include <unistd.h>
#include <wiringPi.h>
#include <softPwm.h>
#include <pthread.h>

void error( char *msg ) {
  perror(  msg );
  exit(1);
}

int func( int a ) {
   return 2 * a;
}

void sendData( int sockfd, int x ) {
  int n;

  char buffer[32];
  sprintf( buffer, "%dn", x );
  if ( (n = write( sockfd, buffer, strlen(buffer) ) ) < 0 )
    error( const_cast<char *>( "ERROR writing to socket") );
  buffer[n] = '';
}

int getData( int sockfd ) {
  char buffer[32];
  int n;

  if ( (n = read(sockfd,buffer,31) ) < 0 )
    error( const_cast<char *>( "ERROR reading from socket") );
  buffer[n] = '';

  printf("buffer: %s n",buffer);



  return atoi( buffer );
}

int main(int argc, char *argv[]) {

    char buf[30];
     int sockfd, newsockfd, portno = 1219, clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     int data;

     printf( "using port #%dn", portno );

     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0)
         error( const_cast<char *>("ERROR opening socket") );
     bzero((char *) &serv_addr, sizeof(serv_addr));

     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons( portno );
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0)
       error( const_cast<char *>( "ERROR on binding" ) );
     listen(sockfd,5);
     clilen = sizeof(cli_addr);

     //--- infinite wait on a connection ---
     while ( 1 ) {
        printf( "waiting for new client.....n" );

        if ( ( newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr, (socklen_t*) &clilen) ) < 0 )
            error( const_cast<char *>("ERROR on accept") );

        recv(newsockfd,buf,31,0);
        printf( "opened new communication with clientn" );
        printf( "sockfd: %dn",sockfd );
        printf( "newsockfd: %d n ",newsockfd );

        int res = strcmp(buf,"go");
        printf("res: %dn",res);
        write(newsockfd,"message received! [ from raspi server ! ]",50);

        while ( 1 ) {

             //---- wait for a number from client ---
            data = getData( newsockfd );
            //recv(newsockfd,buf,31,0);
            // printf("%s n",buf);

             printf( "got %dn", data );
             if ( data < 0 )
                break;

             data = func( data );

             //--- send new data back ---
             printf( "sending back %dn", data );
             sendData( newsockfd, data );


        }

        close( newsockfd );

        //--- if -2 sent by client, we can quit ---
        if ( data == -2 )
          break;
     }


     return 0;
}

一旦开始使用这个 linux socket 通信程序(当客户端 mylaptop 连接到 linux 机器时),基于 socket-CAN 的程序(基于 CAN 通信开源库的 mcp 2515 ..)不起作用说“写:没有可用的缓冲空间”当我尝试在下面的图片中进行 candump 检查时

图片

我想这与我在上面代码中用于 linux 机器(raspberry,linux)和我的笔记本电脑(window)之间的套接字通信的 linux 套接字程序有关。但我不知道如何避免与“缓冲区空间”相关的问题以及导致它的原因。也许我每次得到套接字帧时都应该刷新缓冲区?任何人都知道我应该怎么做才能同时让 Socket-CAN 和 linux 套接字通信工作?

4

1 回答 1

0

尝试在调用 accept() 和 write() 函数的第一个 while 循环中初始化 clilen。

while ( 1 ) {
    printf( "waiting for new client.....n" );

    clilen = sizeof(struct sockaddr_in);
    if ( ( newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr, &clilen) ) < 0 )
        error( const_cast<char *>("ERROR on accept") );
于 2017-07-04T23:40:11.933 回答