Hi,
With the program below, i am trying to connect to a web host. But there is a problem; the line that prints "abcd" shows up when i enter a correct IP address, otherwise it doesn't. Shouldn't it run before the connect() statement, inependent of the result of the connect() ? Or is that something that i don't know about Linux programming? I am waiting for your help.
Thanks...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
int main(){
char ipstr[32];
int s,n = 0;
struct sockaddr_in con_addr;
printf("abcd"); // This string doesn't show up if the IP address(ipstr) isn't valid.
sprintf(ipstr, "64.233.183.1");
if((s = socket(PF_INET,SOCK_STREAM,0)) == -1){
perror("socket()");
return 0;
}
con_addr.sin_family = AF_INET;
con_addr.sin_port = htons(80);
con_addr.sin_addr.s_addr = inet_addr(ipstr);
memset( con_addr.sin_zero, '\0', sizeof( con_addr.sin_zero ) );
if (connect(s, (struct sockaddr*) &con_addr, sizeof(con_addr) ) < 0) {
perror("connect()");
return 0;
}
printf("connect() succeed.\n");
close(s);
n++;
return 0;
}
(It's running under Ubuntu 7.04)