두 대의 컴퓨터가 있습니다. 내 코드를 테스트하고 있습니다. 하나는 정상적으로 작동하고 다른 하나는 문제가 있으며 그 이유를 알 수 없습니다.sendto : 네트워크에 연결할 수 없습니다.
저는 프로젝트의 네트워킹 부분에 개체 (C++)를 사용하고 있습니다. 서버 쪽에서 나는 이렇게한다 : (명확성을 위해 오류 검사를 제거했다)
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;
}
이 모든 것이 작동한다. 그런 다음이 생성자를 사용하여 객체를 만듭니다.
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;
}
내 문제는 하나의 시스템에서 모든 것이 정상적으로 작동한다는 것입니다. 다른 하나는 서버의 gsend() 함수를 호출 할 때까지 모든 것이 잘 작동합니다. "오류 : 네트워크에 연결할 수 없습니다."라는 메시지가 나타납니다. gercend()를 호출하기 전에 먼저 gercv()를 호출합니다. 누구든지 나를 도울 수 있습니까? 나는 정말로 감사 할 것입니다.
솔루션은
이 서버가 나는 초기 SOCKADDR 구조를 설정하는 방법을 좋아하지 않는 것으로 나타났다. 나는이 일을했다 :
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;
이 같이 있었어야 할 때
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
사람이 이것을 설명 할 수 있습니까?
이 오류가 발생한 프로그램이 아니라고 확인했다고 가정합니다. google.com 또는 보내려는 기계에 ping 할 수 있는지 확인 했습니까?코드 문제보다 서버 문제와 비슷합니다. – Glen
문제가있는 시스템에 ssh가있어서 문제가되지 않습니다. – devin