udp server does not receive message from udp linux client

Asked

Viewed 173 times

0

Well, I was programming a simple messaging system,when I tested it on the same computer,it worked properly without any problems,but when I tested it on two different computers,?

client code

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

#define SERVER "127.0.0.1"
#define BUFFTAM 1024  //Tamanho maximo do buffer
#define PORTA 8888   //A porta na qual sera enviado os dados

void terminar(char *s)
{
    perror(s);
    exit(1);
}

void IniciarCliente()
{
    struct sockaddr_in servidorsock;/*definindo as variaveis*/
    int s, slen=sizeof(servidorsock);
    char buf[BUFFTAM];
    char mensagem[BUFFTAM];

    if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){
        terminar("socket");
    }

    memset((char *) &servidorsock, 0, sizeof(servidorsock));
    servidorsock.sin_family = AF_INET;
    servidorsock.sin_port = htons(PORTA);

    if (inet_aton(SERVER , &servidorsock.sin_addr) == 0)
    {
        fprintf(stderr, "inet_aton() failed\n");
        exit(1);
    }

    while(strcmp(buf,"fim\n")!=0){
        printf("Digite uma mensagem : ");
        fgets(mensagem,BUFFTAM,stdin);

        //Envia a mensagem
        if (sendto(s, mensagem, strlen(mensagem) , 0 , (struct sockaddr *) &servidorsock, slen)==-1){
            terminar("sendto()");
        }

        //recebe uma mensagem de volta e printa ela na tela
        //limpa o buffer de dados
        memset(buf,'\0', BUFFTAM);
        printf("pacote enviado com sucesso!");
        //tenta receber dados do servidor
        if (recvfrom(s, buf, BUFFTAM, 0, (struct sockaddr *) &servidorsock, (socklen_t *)&slen) == -1){
            terminar("recvfrom()");
        }
        printf("pacote recebido com sucesso!");
        //printa os dados recebidos
        printf("%s\n",buf);
    }

    close(s);
}

int main()
{
    IniciarCliente();
    return 0;
}

server code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#define BUFFTAM 1024
#define PORTA 8888

void terminar(char *s)
{
    perror(s);
    exit(1);
}

void IniciarServer(){
    struct sockaddr_in servidorsock, cliente;
    int s, slen = sizeof(cliente);

    char buf[BUFFTAM];

    //cria um scoket udp
    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){
        terminar("socket");
    }

    // zera a estrutura
    memset((char *) &servidorsock, 0, sizeof(servidorsock));

    servidorsock.sin_family = AF_INET;
    servidorsock.sin_port = htons(PORTA);
    servidorsock.sin_addr.s_addr = htonl(INADDR_ANY);

    //faz o bind do socket com a porta
    if( bind(s , (struct sockaddr*)&servidorsock, sizeof(servidorsock) ) == -1){
        terminar("bind");
    }

    //espera por dados
    while(strcmp(buf,"fim\n")!=0){
        printf("Esperando por dados\n");
        fflush(stdout);
        memset(buf,'\0',BUFFTAM);

        //tenta receber algum dado do cliente
        if (recvfrom(s, buf, BUFFTAM, 0, (struct sockaddr *) &cliente, (socklen_t*)&slen) == -1){
            terminar("recvfrom()");
        }

        //imprime os dados recebidos
        printf("Origem do cliente %s:%d\n", inet_ntoa(cliente.sin_addr), ntohs(cliente.sin_port));
        printf("mensagem: %s\n" , buf);

        printf("Digite a mensagem que deseja enviar ao cliente : ");
        fgets(buf,BUFFTAM,stdin);

        //retorna o cliente com a mensagem
        if (sendto(s, buf, strlen(buf), 0, (struct sockaddr*) &cliente, slen) == -1){
            terminar("sendto()");
        }
    }

}


int main()
{
    IniciarServer();
    return 0;
}

I don’t know exactly what it is,

  • It’s on a local network?

  • yes,and on a local network,’ve done everything,tried to redirect the port on my ip,ja tried to change my router’s mac settings and nothing

  • how is connecting the machines?

  • by my network

  • I say you have 2 pcs that connect via cable? The cable is the correct cable?

  • no, the two connect via wireless(wireless network)

  • right tho think here

Show 3 more comments

1 answer

0

I was able to solve the problem, for whoever has the same problem I have,:

If you are using two different machines to connect, you will need to go to your router settings and try to change the dynamic IP of your router-generated machine and make it a static IP on your network,then you can use this machine as a server,and any other machine as a client,.

Tip: try to find some program that does this(change the dynamic IP of a machine to a static IP), because making your machine’s ip as static by the router is a very laborious task

If you are using a virtual machine (VM or virtual machine), which was my case, the process becomes simpler, by selecting your VM you only need to configure your network at Filing cabinet->preferences->Network,and add the client’s ip and the server’s ip (which would be the VM) and type in the port on which you would last, settings and would configure the host-only option on your VM,: https://terminaldeinformacao.com/2013/10/29/criando-conexao-host-only-no-virtualbox/ ,remembering again that you will need to set the static IP of your server in the client code so that it is possible to make the connection

Browser other questions tagged

You are not signed in. Login or sign up in order to post.