LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2011, 01:06 AM   #1
Maitrikkshah
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Rep: Reputation: Disabled
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...???
 
Old 11-09-2011, 10:35 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,412

Rep: Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644
Quote:
Originally Posted by Maitrikkshah View Post
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?
 
Old 11-11-2011, 05:10 AM   #3
Maitrikkshah
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
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????
 
0 members found this post helpful.
Old 11-11-2011, 09:36 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,412

Rep: Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644
Quote:
Originally Posted by Maitrikkshah View Post
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.
 
Old 11-13-2011, 11:51 PM   #5
Maitrikkshah
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
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;
}
 
0 members found this post helpful.
Old 11-14-2011, 09:28 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,412

Rep: Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644
Quote:
Originally Posted by Maitrikkshah View Post
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.
 
Old 11-14-2011, 11:34 PM   #7
Maitrikkshah
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
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)....
 
Old 11-15-2011, 09:19 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,412

Rep: Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644Reputation: 6644
Quote:
Originally Posted by Maitrikkshah View Post
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.
 
Old 02-27-2012, 05:58 AM   #9
PUNJABIking
LQ Newbie
 
Registered: Feb 2012
Posts: 1

Rep: Reputation: Disabled
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.

Last edited by PUNJABIking; 02-27-2012 at 06:00 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
No route to host - Error waranha Linux - Networking 3 02-18-2010 07:29 AM
[SOLVED] no route to host connect socket error..ping and traceroute ok mgard Linux - Kernel 1 01-17-2010 05:32 AM
sendmail error no route to host jez_pag Programming 1 04-17-2007 07:08 AM
No route to host error... alwaysrookie Programming 2 11-04-2005 01:33 PM
Error: No route to host! CRB314 Linux - Newbie 1 03-22-2004 04:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 02:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration