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...??? |
Quote:
Do you have selinux or any firewall(s) running on the boxes? Are these 'real' systems, or are they virtual machines? |
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???? |
Quote:
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. |
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; } |
Quote:
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. |
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).... |
Quote:
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. |
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. |