I have to connect to a remote server via UDP and send a predetermined message to it in order to get a message back. I had first tried this in TCP and it worked but in UDP after I send the message and listen for the reply in recvfrom() I get nothing. Can anyone tell me what might be the problem here.
if ((bytes_sent = sendto(sockfd, UDP_MSG, strlen(UDP_MSG), 0, p->ai_addr, p->ai_addrlen)) == -1) {
perror("ClientUDP: Error sending data");
exit(1);
}
printf("Data sent %s\n", UDP_MSG);
len = sizeof(struct sockaddr_storage);
if ((bytes_recv = recvfrom(sockfd, bufudp, MAXDATASIZE-1, 0,(struct sockaddr*)&addr, &len)) == -1) {
perror("Error receiving in UDP");
exit(1);
}
printf("Bytes recv %d\n", bytes_recv);
bufudp[bytes_recv] = '\0';
printf("ClientUDP: Received\n %s \n",bufudp );
The port is entered from the keyboard using scanf() and the message is the string "HI".