socket() No Route to host ??


 
Thread Tools Search this Thread
Top Forums Programming socket() No Route to host ??
# 1  
Old 04-15-2010
socket() No Route to host ??

Hello,

My program should connect to a server. here's the code :

Code:
int main(int argc, char *argv[]){
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock == -1){
        perror("socket()");
        exit(errno);
        }
    printf("retour socket = %d",sock);

    struct sockaddr *ad;
    ad = (struct sockaddr *)malloc(sizeof(struct sockaddr));
    ad->sa_len = strlen("172.16.192.1");
    ad->sa_family = AF_INET;
    strlcpy(ad->sa_data, "172.16.192.1", strlen("172.16.192.1"));

    int ret = connect(sock, (struct sockaddr *)&ad, sizeof(ad));
    printf("retour connect = %d",ret);
    if(ret == -1){
        perror("socket()");
        exit(errno);
        }
    return 0;
    }

the program answers :
socket(): No route to host

or socket() connection timeout

in both cases socket() returns the integer 3 and connect() returns -1

I must say i have no issue with ping traceroute or even telnet.
so i really dont understant what's happening.

thank you Smilie
# 2  
Old 04-15-2010
It expects a "struct sockaddr_in", not a string! Therefore it doesn't understand your address. It also expects a port number to connect to, not just an IP address.

Code:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

...

struct sockaddr_in addr;

addr.sin_family=AF_INET;
// for example, port 80 is HTTP
addr.sin_port=htons(80);
addr.sin_addr=inet_addr("172.16.192.1");
memset(addr.sin_zero, 0, sizeof(addr.sin_zero));

int ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

"Network error: No route to host" While connecting guest RHEL4 using putty

Hi, I have installed RHEL4 using vmware workstation.. Host OS: Windows XP Guest OS: RHEL4 Pls refer step 17 & 18 in below link... ORACLE-BASE - Red Hat Enterprise Linux 4 and Centos 4 Installation 1) If i choose to assign IP automatically (using DHCP) means, i am able to connect RHEL4... (3 Replies)
Discussion started by: thomasraj87
3 Replies

2. UNIX for Dummies Questions & Answers

No route to host - mail error

A linux box is supposed to emailing the results of backups to the windows exchange server, but nothing arrives. it never has, as the muppet who set up the Windows domain knew nothing about linux. I know only slightly more than that... pretty sure sendmail is the daemon running to handle mail. it... (0 Replies)
Discussion started by: Noewon
0 Replies

3. IP Networking

IP forwarding Route/Host ?

I have a unix machine that operates some CNC machines, I need to hook up my windows machine through core ftp to load programs onto the box. The card i need to route to for the FTP on unix is tu1 I need to check the routing tables on the unix box to see which IP address will automatically... (2 Replies)
Discussion started by: moticulus
2 Replies

4. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

5. AIX

problem with host route

Hi, I have a system with network interfaces en0 and en1 en0: physical ip: 1.1.1.10 virtual ip1: 192.168.100.11 virtual ip2: 192.168.100.12 en1: physical ip: 1.1.1.20 virtual ip1: 192.168.100.20 virtual ip2: 192.168.100.21 default gateway ist 192.168.100.254 when I open a... (12 Replies)
Discussion started by: funksen
12 Replies

6. Red Hat

No route to host

I get below error when using telnet and ssh ?? Why ? the ip address of linux server is 10.155.25.22 =tstgcota ?? # netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.75.0 0.0.0.0 255.255.255.0 U 0 0... (6 Replies)
Discussion started by: chuikingman
6 Replies

7. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

8. Programming

Error: No Route to host...urgent {socket() connection}

hello, I am doing Socket programming.. when I am establishing a socket connection using TCP protocol ...I am getting Error :: No route to host. at the client side during connect() call...........that it is returning -1. So I thing problem lies here......but what to do now... So for just... (6 Replies)
Discussion started by: arunchaudhary19
6 Replies

9. UNIX for Dummies Questions & Answers

no route to host

I not able to ping outside the internet. i am able to ping my 192. ip address. when i try to ping a 68. ip address i get no route to host. what should i do to correct this issue. I have sco openserver 5.05 (6 Replies)
Discussion started by: scoman2
6 Replies

10. UNIX for Dummies Questions & Answers

add route gives target: Host name lookup failure

We need to add our remote office to our linux routing table. Our internal office ip addresses are all in the range of 198.9.200.x with an subnet mask of 255.255.255.0 the remote office has ip addresses in the range of 192.168.0.0 and also a subnet mask of 255.255.255.0 when i use the... (3 Replies)
Discussion started by: progressdll
3 Replies
Login or Register to Ask a Question