LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Socket: ERROR connecting: No route to host (https://www.linuxquestions.org/questions/linux-networking-3/socket-error-connecting-no-route-to-host-912583/)

Maitrikkshah 11-09-2011 12:06 AM

Socket: ERROR connecting: No route to host
 
Hi!!! I do socket programming. I have two programs.

client.c and server.c..
I have run server.c on one machine which has ip address 10.1.3.6 and client.c on another machine with ip address 10.1.3.14...
Both are on the same network.
I can ping to each other.

Bt when I tried to connect client and server I got the following error.

[root@pgcse14 ]# gcc client.c
[root@pgcse14 ]# ./a.out 10.1.3.6 6061
ERROR connecting: No route to host

Please can anyone give me the solution...???

TB0ne 11-09-2011 09:35 AM

Quote:

Originally Posted by Maitrikkshah (Post 4519463)
Hi!!! I do socket programming. I have two programs.

client.c and server.c..
I have run server.c on one machine which has ip address 10.1.3.6 and client.c on another machine with ip address 10.1.3.14...
Both are on the same network. I can ping to each other.

Bt when I tried to connect client and server I got the following error.

[root@pgcse14 ]# gcc client.c
[root@pgcse14 ]# ./a.out 10.1.3.6 6061
ERROR connecting: No route to host
Please can anyone give me the solution...???

If the machines can ping each other, then they're on the network, up and working fine. So either you've got bugs in your software (which we can't help with, since you didn't post it), or you've got a firewall enabled, and it's blocking the ports.

Do you have selinux or any firewall(s) running on the boxes? Are these 'real' systems, or are they virtual machines?

Maitrikkshah 11-11-2011 04:10 AM

Thanks for your rply.

Both PCs are real machines. And firewall is also disabled.
Though m getting the same error. What should i do now????

TB0ne 11-11-2011 08:36 AM

Quote:

Originally Posted by Maitrikkshah (Post 4521374)
Thanks for your rply.

Both PCs are real machines. And firewall is also disabled. Though m getting the same error. What should i do now????

Spell out your words.

And as I said in my first reply, if you don't have a firewall or any other type of barrier, then you've got a bug in your code. So, debug it. You don't post your code, so we can't help, but if you can communicate between both machines now, EXCEPT with your code, then the problem is in your code.

Maitrikkshah 11-13-2011 10:51 PM

This is my program.... I have debugged this code. and it did not generate any error.
client.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

void error(const char *msg)
{
perror(msg);
exit(0);
}

int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
printf("sockfd is %d :",sockfd);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");

printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
close(sockfd);
return 0;
}


And server.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

void error(const char *msg)
{
perror(msg);
exit(1);
}

int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
close(newsockfd);
close(sockfd);
return 0;
}

TB0ne 11-14-2011 08:28 AM

Quote:

Originally Posted by Maitrikkshah (Post 4523319)
This is my program.... I have debugged this code. and it did not generate any error.

Ok....but because the code didn't generate an error at compile time, does not mean there aren't any problems in it. The STRUCTURE/FUNCTIONS of the program may not work.

Also, you have a client and a SERVER piece of software, right?? You need to examine both of them. Again, since networking services are working between both servers, this problem isn't in the Linux side of things, but in YOUR SOFTWARE. Put some tests into your software, some logging, something to tell you what's happening, where, and when. Do it on BOTH sides, and see what you're getting, and fix it from there.

Maitrikkshah 11-14-2011 10:34 PM

Okay you are right. But when I run same program client.c and server.c on the single machine(not on different machines) both client and server connect each other successfully. And I got the true output.

But problem generates only when I run them on different machines(PCs)....

TB0ne 11-15-2011 08:19 AM

Quote:

Originally Posted by Maitrikkshah (Post 4524204)
Okay you are right. But when I run same program client.c and server.c on the single machine(not on different machines) both client and server connect each other successfully. And I got the true output.

But problem generates only when I run them on different machines(PCs)....

A single machine will probably use the loopback address, and the network packets will not even leave your system...so yes, it's probably likely the program will run on a single machine.

Regardless...YOUR program doesn't work for its intended purpose. Linux has nothing to do with this, providing you've ensured there are no firewalls/iptables kinds of rules in place, and that you are SURE that the routers/switches in between the two aren't doing any sort of filtering/blocking on their own.

PUNJABIking 02-27-2012 04:58 AM

Hi i am also doing similar programming and getting the same error, could you please tell if you able to get rid of this problem from your program.


All times are GMT -5. The time now is 06:57 AM.