這個職位是跟進 Call to the 'userConnect()' method skips可變正在改變出於某種原因
更正我的最後一篇,它實際上是進入userConnect()方法,但一些如何SETUP_ERROR標誌正在被改變。正如你可以在我的userConnect()中看到我改變了SETUP_ERROR的地方,我清楚地打印了錯誤信息。在'getMyIPAddress()'方法中,現在'cout < <',SETUP_ERROR爲0並進入userConnect()中的IF循環並且工作正常。當我刪除它時,不知怎的,SETUP_ERROR被改爲1並打印「SETUP_ERROR」。
不知道爲什麼SETUP_ERROR被改爲1,我知道'cout'與此無關,但它可能是什麼。打敗我。
getMyIPAdress():
void getMyIPAddress (char* command, char* port) {
struct hostent *he;
struct in_addr ipv4addr;
char dnsIP[] = "8.8.8.8"; // Google DNS IP
char dnsPort[] = "53" ; // Google DNS TCP Port
// Converting IP to struct in_addr type
inet_pton(AF_INET, dnsIP, &ipv4addr);
// getting host details from IP address
he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
cout << "bazinga!" << endl;
// Connecting to google DNS to get IP address of this process
userConnect (he->h_name, dnsPort, port, command);
}
userConnect():
void userConnect (char* sIP, char* sPort, string my_port, char* command)
{
int numbytes;
char buf[MAXDATASIZE];
char rbuf[MAXDATASIZE];
int rv;
char s[INET6_ADDRSTRLEN];
struct addrinfo *servinfo,*p,hints;
int sockfd;
int SETUP_ERROR;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
// Getting address related information
// flagging invalid ip addresses
if ((rv = getaddrinfo(sIP, sPort, &hints, &servinfo)) != 0) {
cout << "getaddrinfo: " << endl << gai_strerror(rv) << endl;
cout << " Invalid IP Address! " << endl;
SETUP_ERROR = 1;
}else
{
int count;
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("client: connect");
continue;
}
break;
}
// flagging invalid port numbers
if (p == NULL) {
cout << stderr << "client: failed to connect" << endl;
cout << "Invalid Port Number! " << endl;
SETUP_ERROR = 1;
}
}
if (SETUP_ERROR != 1) {
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s); // gettin printable ip
freeaddrinfo(servinfo); // don't need anymore
int len;
// Functionalities based on input commands
if (strcmp(command,"REGISTER")==0) {
// client adding server as default first connection
conn_head = insert(conn_head , server_IP, sPort, sockfd-OFFSET);
servfd = sockfd;
len = my_port.length();
// sending client's listening port number to server
if (send (sockfd, &len, sizeof(len), 0) == -1)
perror("send packet length");
if (send(sockfd, my_port.c_str(), len, 0) == -1)
perror("send");
// adding socket fd to master list
addFdToMaster(sockfd);
}else if(strcmp(command, "MYIP")==0) {
/* Next 10 line code snippet is taken from a blog online! Cant find it anymore to post reference ! :) */
//get local socket info::
struct sockaddr_in local_addr;
socklen_t addr_len = sizeof(local_addr);
if (getsockname(sockfd, (struct sockaddr*)&local_addr, &addr_len) < 0) {
perror("getsockname");
}
/* get peer ip addr */
char my_ip[INET_ADDRSTRLEN];
if (inet_ntop(local_addr.sin_family, &(local_addr.sin_addr), myip, sizeof(myip)) == NULL) {
perror("inet_ntop");
}
else
cout << "HOST IP Address:: " << myip << endl;
}
}else
cout << "SETUP_ERROR" << endl;
}
userConnect()的'my_port'參數是一個'string',但是你傳入一個'char *'。這是不對的。 – user1118321
@ user1118321 - 'std :: string'具有'char *'的隱式轉換,所以這不是問題。 –
@ ^^^:正如我所說的,代碼工作得很好。 SOCKET_ERROR變量get被改變。 – user3329166