Reputation: 21
This is just a simple chat program wherein echoclient sends data to echoserver. In the example code given to us, why is it that the client wouldn't connect if I use an ip-address xxx.xxx.xxx.xxx (where xxx ranges from 0-255, for example 123.124.12.1) but it would if I input random numbers such as 12312 or 23423? In this case, when I print the network ordered ip, it gives
Network ordered ip(client side): 00000000 Network ordered ip(server side): 127.0.0.1.
I read about 127.0.0.1 being the loopback ip address for localhost. But my question is, why is it that the client only connects with that address?
When I run ./echoclient 12312
,
the client connects and the program functions as it should. But if I run./echoclient 123.123.12.1
, the client doesn't connect.
..or I'm connecting the wrong way?
Here's the code:
echoserver.c
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char **argv)
{
int listenfd,connfd;
struct sockaddr_in servaddr,cliaddr;
socklen_t len = sizeof(cliaddr);
char cli_ip[32];
char mesg[1024];
listenfd = socket(PF_INET,SOCK_STREAM,0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(54322);
if ( bind( listenfd, (struct sockaddr*) &servaddr, sizeof(servaddr) ) < 0 ){
perror(NULL);
exit(-1);
}
//not present in udp server
listen(listenfd,2);
while(1){
//not present in udp server
connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&len);
inet_ntop(AF_INET,(struct in_addr *) &cliaddr.sin_addr, cli_ip, sizeof(cli_ip) );
printf("Client %s connected. \n",cli_ip);
while(1){
memset(mesg,0,sizeof(mesg));
if( recvfrom(connfd,mesg,sizeof(mesg),0,(const struct sockaddr *)&cliaddr,&len) > 0 ){
printf("From %s port %d: %s",cli_ip,ntohs(cliaddr.sin_port),mesg);
}
else {
printf("Client %s disconnected. \n",cli_ip);
break;
}
}
close(connfd);
}
close(listenfd);
return 0;
}
echoclient.c
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
socklen_t len = sizeof(servaddr);
char mesg[1024];
if(argc!=2){
printf("Usage: %s <ip_addr>\n",argv[0]);
exit(1);
}
sockfd = socket(PF_INET,SOCK_STREAM,0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(54322);
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
//printf("Network ordered ip: %08x\n",servaddr.sin_addr.s_addr);
//not present in udp clients
connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
while(1){
fgets(mesg,sizeof(mesg),stdin);
sendto(sockfd,mesg,strlen(mesg),0,(const struct sockaddr *)&servaddr,len);
}
close(sockfd);
return 0;
}
Thank you in advance!
Upvotes: 2
Views: 601
Reputation: 229234
When you input an invalid address, e.g. 12312
, inet_pton fails. And as you've zeroed out the address in the servaddr
, the ip address is 0.0.0.0. 0.0.0.0 will be interpreted as "this host". And your server is set to listen on any address on the host it's running.
And if you run the server on the same host as the client, then they can make a connection, as your client connects to the host you run the client on.
If you input a valid address, e.g. 123.123.12.1, then it's naturally not going to successfully connecting to that host - unless you run your server on that host or something else happens to listen on port 54322 on that host.
(0.0.0.0 will often be routed to the loopback address on the host - you can try e.g. ping 0.0.0.0
to see which address that responds)
Upvotes: 2