UDP socket problem

Hi!

I had a working c program for a udp socket.
For updating it to c++ I only needed to add:

#include <arpa/inet.h> // For inet_addr()

The file compiles, but when I execute it I do not receive data.
What can the problem be?

Here is my full code:

/************* UDP SERVER CODE *******************/
//Data komt binnen in volgende structuur:
//format: type(1=encoder, 2 =laserscanner),id(identity),angleleft(rad),angleright(rad),time(sec)




#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h> // For inet_addr()
#include <string>
using namespace std;

int main(){
int udpSocket, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr, clientAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size, client_addr_size;
int i;

/*Create UDP socket*/
udpSocket = socket(PF_INET, SOCK_DGRAM, 0);

/*Configure settings in address struct*/
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("192.168.0.2");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

/*Bind socket with address struct*/
bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

/*Initialize size variable to be used later on*/
addr_size = sizeof serverStorage;

while(1){
/* Try to receive any incoming UDP datagram. Address and port of
requesting client will be stored on serverStorage variable */
nBytes = recvfrom(udpSocket,buffer,1024,0,(struct sockaddr *)&serverStorage, &addr_size);

printf("Received from client:%s\n",buffer);//Buffer uitprinten als string,n =>ga naar de volgende lijn

/*Convert message received to uppercase*/
// for(i=0;i<nBytes-1;i++)
// buffer[i] = toupper(buffer[i]);

/*Send uppercase message back to client, using serverStorage as the address*/
sendto(udpSocket,buffer,nBytes,0,(struct sockaddr *)&serverStorage,addr_size);
}

return 0;
}

Thanks!
It's strange for inet not to be available.

Have you thought about just making your own replacement function, as its a rather small component.
Topic archived. No new replies allowed.