问题
I am trying to implement a server and a client that both send and receive (in different order) a datagram. I am trying on the following way but the datagram from client is not sent/received.
Here the client:
#include <sys/socket.h>
#include<netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#define DATA "Este es el mensaje ...."
int main(int argc, char *argv[])
{
int sock, lon2;
struct sockaddr_in name, name2;
struct hostent *hp;
char buf[1024];
sock=socket(PF_INET,SOCK_DGRAM,0);
if (sock<0)
{
perror("Abriendo socket de datagramas");
exit(1);
}
/*devuelve una estructura hostent para el host especificado en argv[1]*/
/*para obtener la direccion IP a partir del nombre de la maquina*/
hp=gethostbyname(argv[1]);
if (hp == 0)
{
fprintf(stderr,"%s: host desconocido",argv[1]);
exit(2);
}
/*Copiamos en la estructura name la direccion del ordenador al que */
/*vamos a conectarnos.*/
memcpy((char *)&name.sin_addr, (char *)hp->h_addr, hp->h_length);
name.sin_family = AF_INET;
name.sin_port = htons(atoi(argv[2]));
if (sendto(sock,DATA,strlen(DATA)+1,0,(struct sockaddr *)&name,sizeof(name))<0)
perror("Enviando un datagrama");
if (read(sock,buf,1024)<0)
perror("Recibiendo el datagrama");
printf("-->%s\n",buf);
close(sock);
exit(0);
}
Here the server:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#define DATA "Este es el mensaje ....bvbvbvbv"
int main()
{
int sock, length, lon2;
struct sockaddr_in name;
char buf[1024];
sock=socket(PF_INET,SOCK_DGRAM,0);
if (sock<0)
{
perror("Abriendo socket de datagramas");
exit(1);
}
/*Formato de la direccion*/
name.sin_family=AF_INET;
/*Direccion IP, INADDR_ANY -> que cuando se use bind para asociar una*/
/*direccion a un socket, la direccion IP es la de la maquina donde esta*/
/*ejecutandose el programa.*/
name.sin_addr.s_addr=htonl(INADDR_ANY);
/*0-> cuando se utilice bind(), el puerto que se va a asociar al socket es */
/*uno libre asignado por el SO.*/
name.sin_port=htons(0);
if (bind(sock,(struct sockaddr *)&name, sizeof(name))<0)
{
perror("Asociando nombre al socket");
exit(1);
}
/*Hasta despues del bind no sabremos la direccion del socket asignada.*/
/*=> usar getsockname()*/
length=sizeof(name);
/*sock = socket del que queremos saber la direccion*/
/*name = estructura en la que se va a dejar la direccion*/
/*length = tamano ocupado por la estructura.*/
if (getsockname(sock,(struct sockaddr *)&name,&length)<0)
{
perror("Averiguando el nombre del socket");
exit(1);
}
/*Imprimimos el puerto para que el emisor mande a ese puerto.*/
printf("puerto del socket -->%d\n", ntohs(name.sin_port));
if (recvfrom(sock,buf,1024,0,(struct sockaddr *)NULL,&lon2)<0)
perror("Recibiendo el datagrama");
printf("-->%s\n",buf);
if (sendto(sock,DATA,strlen(DATA)+1,0,(struct sockaddr *)&name,sizeof(name))<0)
perror("Enviando un datagrama");
close(sock);
exit(0);
}
回答1:
Your client code is fine, but you really should "bind" the client socket. (Specifying sin_port=0 is fine to let the OS select a random port number). I think some operating systems will pick a random port for each sendto if the socket is not been "binded" to a port and not actually listen on a port. I may be confusing that with something else, but in any case, calling bind for the client socket is a good thing to do. Binding to port 0 will at least make the socket listen on the same port it sends data with.
sockaddr_in localAddr = {}; // zero's out sin_port and sets sin_addr to INADDR_ANY (0)
int result;
localAddr.sin_family = PF_INET;
sock=socket(PF_INET,SOCK_DGRAM,0);
if (sock < 0) {perror("socket error"); exit(1);}
result = bind(sock, (sockaddr*)&localAddr, sizeof(localAddr));
if (result < 0) {perror("bind error"); exit(1);}
But that's not your only problem. In your server code:
if (recvfrom(sock,buf,1024,0,(struct sockaddr *)NULL,&lon2)<0)
perror("Recibiendo el datagrama");
printf("-->%s\n",buf);
if (sendto(sock,DATA,strlen(DATA)+1,0,(struct sockaddr *)&name,sizeof(name))<0)
perror("Enviando un datagrama");
You aren't actually sending back to the remote IP/port that you received data on. This will more likely work:
sockaddr_in remoteClient = {};
socklen_t remoteClientSize = sizeof(remoteClient);
if (recvfrom(sock,buf,1024,0,(struct sockaddr *)&remoteClient,&remoteClientSize)<0)
perror("Recibiendo el datagrama");
printf("-->%s\n",buf);
if (sendto(sock,DATA,strlen(DATA)+1,0,(struct sockaddr *)&remoteClient,remoteClientSize)<0)
perror("Enviando un datagrama");
来源:https://stackoverflow.com/questions/13979511/send-and-receive-in-udp-from-server-and-client-in-c