J
Justin
Sorry for the cross post. I accidently posted in comp.lang.learning.c+
+. I'm reposting here for the higher activity.
I'm working on a socket programming project for school. I created a
UDP socket. The socket works fine, my server communication works
without a hitch. However, I'm trying to print out the local IP
Address and Port for the client. When I print out the Port, it shows
up at 0. I was looking at another article, and it says you have to
call bind() first to give the Port a value. When I do this, the
communication with the server breaks down, I think the client sends
messages to itself.
Thanks for any help.
Justin
int openUDPSocket(const char* port){
struct addrinfo hints, *servinfo, *p;
int sockfd, rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
if((rv = getaddrinfo(NULL, port, &hints, &servinfo)) != 0){
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
for(p=servinfo; p != NULL; p =p->ai_next){
if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol))
== -1){
//perror("talker: socket\n");
continue;
}
break;
}
if(p==NULL){
fprintf(stderr, "talker: failed to bind socket \n");
return -2;
}
char *addr = new char[50];
struct hostent *he;
struct in_addr **addr_list;
gethostname(addr, 50);
he = gethostbyname(addr);
delete addr;
addr_list = (struct in_addr **) he->h_addr_list;
if(addr_list[0] != NULL){
addr = inet_ntoa(*addr_list[0]);
}
struct sockaddr_in sa;
socklen_t* sa_len = new socklen_t;
*sa_len = sizeof(sa);
if(getsockname(sockfd, (sockaddr*) &sa, sa_len) == -1){
printf("getsockname error\n");
return -3;
}
printf("%s: UDP Port %d and IP address %s.\n", studentName.c_str(),
(int) ntohs(sa.sin_port), addr);
delete addr;
delete he;
delete [] addr_list;
delete addr_list;
delete sa_len;
return 0;
}
+. I'm reposting here for the higher activity.
I'm working on a socket programming project for school. I created a
UDP socket. The socket works fine, my server communication works
without a hitch. However, I'm trying to print out the local IP
Address and Port for the client. When I print out the Port, it shows
up at 0. I was looking at another article, and it says you have to
call bind() first to give the Port a value. When I do this, the
communication with the server breaks down, I think the client sends
messages to itself.
Thanks for any help.
Justin
int openUDPSocket(const char* port){
struct addrinfo hints, *servinfo, *p;
int sockfd, rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
if((rv = getaddrinfo(NULL, port, &hints, &servinfo)) != 0){
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
for(p=servinfo; p != NULL; p =p->ai_next){
if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol))
== -1){
//perror("talker: socket\n");
continue;
}
break;
}
if(p==NULL){
fprintf(stderr, "talker: failed to bind socket \n");
return -2;
}
char *addr = new char[50];
struct hostent *he;
struct in_addr **addr_list;
gethostname(addr, 50);
he = gethostbyname(addr);
delete addr;
addr_list = (struct in_addr **) he->h_addr_list;
if(addr_list[0] != NULL){
addr = inet_ntoa(*addr_list[0]);
}
struct sockaddr_in sa;
socklen_t* sa_len = new socklen_t;
*sa_len = sizeof(sa);
if(getsockname(sockfd, (sockaddr*) &sa, sa_len) == -1){
printf("getsockname error\n");
return -3;
}
printf("%s: UDP Port %d and IP address %s.\n", studentName.c_str(),
(int) ntohs(sa.sin_port), addr);
delete addr;
delete he;
delete [] addr_list;
delete addr_list;
delete sa_len;
return 0;
}