Thread: Problem in a socket program

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    17

    Post Problem in a socket program

    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)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > printf("abcd");
    Follow it with
    fflush(stdout);

    Normally, stdout is buffered, and is only automatically flushed when a '\n' is sent.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    17
    Thank you, i got it working now. But this doesn't happen it Windows, does it? I've never came accross this...

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by phal View Post
    Thank you, i got it working now. But this doesn't happen it Windows, does it? I've never came accross this...
    Welcome to platform-dependent behavior

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-05-2009, 11:05 PM
  2. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  3. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  4. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  5. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM