0

I want to create a simple UDP connection between two devices. One device should work as a server and the other devices should work as client. When I try to run above server and client code from different devices (same network), I did not get any messages back from server but when I try to run in the same computer,codes are working fine.

Thanks for your helps.

Server Code :


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h> 
#include <netinet/in.h>
#define BUFF_SIZE 1024
int main(int argc, char *argv[])
{
    int serv_socket,port_no,str_len;
    int opt = 1;
    socklen_t client_addr_size;
    struct hostent *hostp;
    char *hostaddrp;
    char buf[BUFF_SIZE];
    struct sockaddr_in server_addr, client_addr;

    if(argc < 2 )
    {
        fprintf(stderr,"Usage : %s <port> \n",argv[0]);
        exit(EXIT_FAILURE);
    }

    port_no = atoi(argv[1]);
    serv_socket = socket(PF_INET, SOCK_DGRAM, 0);
    if(serv_socket == -1)
    {
        perror("Error: opening socket()");
        exit(EXIT_FAILURE);
    }

    memset(&server_addr,0,sizeof(server_addr));
    memset(&client_addr,0,sizeof(client_addr));

    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(atoi(argv[1]));

    if(setsockopt(serv_socket,SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)) == -1)
    {
        perror("Error: setsockopt()");
        exit(EXIT_FAILURE);
    }
    if(bind(serv_socket,(struct sockaddr*)&server_addr,sizeof(server_addr)) == -1)
    {
        perror("bind() error");
    }

    
    while (1)
    {
        client_addr_size = sizeof(client_addr);
        str_len = recvfrom(serv_socket, buf, BUFF_SIZE, 0, (struct sockaddr*)&client_addr, &client_addr_size);
        hostp = gethostbyaddr((const char*)&client_addr.sin_addr.s_addr,sizeof(client_addr.sin_addr.s_addr), AF_INET);
        if(hostp == NULL)
        {
            perror("Error on gethostbyaddr");
        }
        hostaddrp = inet_ntoa(client_addr.sin_addr);
        if(hostaddrp == NULL)
        {
            perror("Error on inet_ntoa");
        }
        printf("server received datagram from %s <%s> \n",hostp->h_name,hostaddrp);
        sendto(serv_socket,buf,strlen(buf), 0,(struct sockaddr*)&client_addr,client_addr_size);

    }
    
    close(serv_socket);
    return 0 ;
}

Client Code :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

void error_handling(char *message);
#define BUFF_SIZE 1024


char client_message[] = "88\n";
int main(int argc, char *argv[])
{
    int sock;
    char message[BUFF_SIZE];
    int str_len;
    socklen_t address_size;

    struct sockaddr_in serv_addr, from_addr;

    if(argc != 3)
    {
        printf("Usage :  <IP> <port> \n");
        exit(1);
    }
    printf("IP address : %s , port : %d \n",argv[1],atoi(argv[2]));

    sock = socket(PF_INET, SOCK_DGRAM, 0);
    if(sock == -1)
    {
        error_handling("socket() error !");
    }

    memset(&serv_addr,0,sizeof(serv_addr));
    
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr(argv[1]); //IP address
    serv_addr.sin_port = htons(atoi(argv[2]));

    while (1)
    {
        address_size = sizeof(from_addr);
        sendto(sock,client_message,strlen(client_message),MSG_CONFIRM,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
        str_len = recvfrom(sock,message,BUFF_SIZE,MSG_WAITALL,(struct sockaddr*)&from_addr,&address_size);
        message[str_len] = 0;
        printf("Received message from server : %s \n",message);
        sleep(5);
    }
    close(sock);
    return 0;
}


void error_handling(char *message)
{ 
    fputs(message, stderr);
    fputc('\n', stderr);
    exit(1);
}


I try to establish a connection between two different devices but it did not worked.

I try to establish a connection from the same device , it worked

mehbstnc
  • 23
  • 2
  • Are the devices on the same private network and are you sure you got the IPs right? If so, makes sure a firewall is not dropping the traffic. – erik258 Apr 18 '23 at 12:00
  • What does "did not work" mean? Did you verify that the packets were sent? You can use Wireshark to capture network traffic and check if your packets actually left your PC. Then you can also check that the content is correct. – Gerhardh Apr 18 '23 at 12:02
  • Please [edit] your question and copy&paste the commands you use to run the programs, the resulting output and the output of `ip addr` (Linux/UNIX) or `ipconfig` (Windows) on both systems. You should check the return value of `sendto` and `recvfrom` for a possible error indication. – Bodo Apr 18 '23 at 12:03
  • @mehbstnc You might check your firewall settings. I had this exact problem myself just yesterday, because of a RHEL version that blocks all ports by default. I had to run a certain firewall command (which I don't have handy) to explicitly tell the machine it was allowed to pass UDP traffic on a certain port. – Steve Summit Apr 18 '23 at 12:23
  • 1
    @SteveSummit On further review, not so sure. I'll reopen, though I'd still recommend not using `MSG_CONFIRM`. – dbush Apr 18 '23 at 12:34
  • I guess the problem is about firewall settings. Thanks for your helps :) – mehbstnc Apr 19 '23 at 04:56

0 Answers0