-2

this is Server-side code (raspberry pi)

#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;
}

As soon as start using this linux socket communication program(when mylaptop, which is the client, gets connected to linux machine ), socket-CAN based program(CAN communication open source library based mcp 2515..) does not work saying that "write : no buffer space available" when i tried to do candump check below picture

Image

I guess it has something to do with the linux socket program i used above code for socket communication between linux machine(raspberry, linux) and my laptop(window). but i don't know how to avoid problem related 'buffer space' and what caused it. Maybe I should flush buffer every time i get socket frame? anyone have some idea what i should do for getting both Socket-CAN and linux socket communication work at the same time?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
whoami
  • 1

1 Answers1

0

Try initializing your clilen in your first while loop where the accept() and write() functions are called.

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") );