Hello.
I have two machines I'm testing my code on, one works fine, the other I'm having some problems and I don't know why it is.
I'm using an object (C++) for the networking part of my project. On the server side, I do this: (error checking removed for clarity)
res = getaddrinfo(NULL, port, &hints, &server)) < 0
for(p=server; p!=NULL; p=p->ai_next){
fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if(fd<0){
continue;
}
if(bind(fd, p->ai_addr, p->ai_addrlen)<0){
close(fd);
continue;
}
break;
}
This all works. I then make an object with this constructor
net::net(int fd, struct sockaddr *other, socklen_t *other_len){
int counter;
this->fd = fd;
if(other != NULL){
this->other.sa_family = other->sa_family;
for(counter=0;counter<13;counter++)
this->other.sa_data[counter]=other->sa_data[counter];
}
else
cerr << "Networking error" << endl;
this->other_len = *other_len;
}
void net::gsend(string s){
if(sendto(this->fd, s.c_str(), s.size()+1, 0, &(this->other), this->other_len)<0){
cerr << "Error Sending, " << s << endl;
cerr << strerror(errno) << endl;
}
return;
}
string net::grecv(){
stringstream ss;
string s;
char buf[BUFSIZE];
buf[BUFSIZE-1] = '\0';
if(recvfrom(this->fd, buf, BUFSIZE-1, 0, &(this->other), &(this->other_len))<0){
cerr << "Error Recieving\n";
cerr << strerror(errno) << endl;
}
// convert to c++ string and if there are multiple trailing ';' remove them
ss << buf;
s=ss.str();
while(s.find(";;", s.size()-2) != string::npos)
s.erase(s.size()-1,1);
return s;
}
So my problem is, is that on one machine, everything works fine. On another, everything works fine until I call my server's gsend() function. In which I get a "Error: Network Unreachable." I call gercv() first before calling gsend() too. Can anyone help me? I would really appreciate it.
SOLUTION
It turns out that the server didn't like the way I set up the initial sockaddr structure. I was doing this:
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = IPPROTO_UDP;
When it should have been like this
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
Can anyone explain this?