Using sockets as IPC with localhost

I wish to use sockets to handle communication between two applications running on the same gateway. When the client try to connect to the server the connection fails. The code works on other systems but not on legato. I’ve tried to run the apps both sandboxed and unsandboxed with the same results, the connection simply fails in both cases.

Any idea what might be causing this and how to solve it? My first thought was that the sandbox prevented socket IPC but that doesn’t seem to be the case.

Is there something I need to do in order to use localhost on a legato system?

I’m adding my code below:

Server:

#include “legato.h”
#include “unistd.h”
#include “stdio.h”
#include “sys/socket.h”
#include “stdlib.h”
#include “netinet/in.h”
#include “string.h”
#define PORT 8080

COMPONENT_INIT
{
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = “Hello from server”;
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror(“socket failed”);
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror(“bind failed”);
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror(“listen”);
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr )&address,
(socklen_t
)&addrlen))<0)
{
perror(“accept”);
exit(EXIT_FAILURE);
}
read( new_socket , buffer, sizeof(buffer));
printf(“%s\n”,buffer );
send(new_socket , hello , strlen(hello) , 0 );
printf(“Hello message sent\n”);
}

Client:

#include “legato.h”
#include “unistd.h”
#include “stdio.h”
#include “sys/types.h”
#include “sys/socket.h”
#include “stdlib.h”
#include “netdb.h”
#include “netinet/in.h”
#include “string.h”
#include “arpa/inet.h”
#define PORT 8080

COMPONENT_INIT
{
int sock;
int val;
struct sockaddr_in serv_addr;
char *hello = “Hello from client”;
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf(“\n Socket creation error \n”);
}
memset(&serv_addr, ‘0’, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, “127.0.0.1”, &serv_addr.sin_addr)<=0)
{
printf(“\nInvalid address/ Address not supported \n”);
}
val = connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
printf(“%d”, val);
if (val < 0)
{
printf(“\nConnection Failed \n”);
}
send(sock , hello , strlen(hello) , 0 );
printf(“Hello message sent\n”);
read( sock , buffer, sizeof(buffer));
printf(“%s\n”,buffer );
}

Hello @marerl,

  1. Minor changes were required to get your code executed. You can refer to the modified code - sockets.zip (3.5 KB)
  2. To execute the app on local host :
    1)Build legato for localhost
    cd legato-18.06.03
    make localhost
  3. Source legato environment
    bin/legs
  4. Run ‘startlegato’ script from bin directory.
    ./bin/startlegato
  5. Go to extracted directory ‘sockets’ and build the app for localhost
    cd sockets/
    make localhost
  6. When app is successfully built, go to _build_server/localhost/app/server/staging/read-
    only/bin and execute the server binary.
    ./serversoc
  7. To stop the localhost version of legato
    cd legato-18.06.03
    bin/stoplegato
    With these changes and the procedure described above, the app is executed successfully . The client was running on a mangoh green board and server on local host.