Problem in a socket program

This is a discussion on Problem in a socket program within the Linux Programming forums, part of the Platform Specific Boards category; Hi, With the program below, i am trying to connect to a web host. But there is a problem; the ...

  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
    32,854
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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-06-2009, 12:05 AM
  2. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 03:02 PM
  3. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 01:09 AM
  4. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 03:38 PM
  5. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 07:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21