For what reasons would connect() fail?

This is a discussion on For what reasons would connect() fail? within the C++ Programming forums, part of the General Programming Boards category; For what reasons would connect() fail? Attached is the full source. Below is where connect fails. Code: SOCKET openSocket(const char ...

  1. #1
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281

    For what reasons would connect() fail?

    For what reasons would connect() fail?
    Attached is the full source. Below is where connect fails.

    Code:
    SOCKET openSocket(const char *IP, int port){
       int ret;
    
       struct sockaddr_in my_addr;
       my_addr.sin_family = AF_INET;         // host byte order
       my_addr.sin_port = htons(port);       // short, network byte order
       if (inet_addr(IP) == (unsigned)-1)    // error checking
          return 0;
       my_addr.sin_addr.s_addr =
          inet_addr(IP);                     // set the address
       memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
    
       SOCKET sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // do some error checking!
       if (sockfd == -1) error("Error in sockfd!","sockfd");
       ret = connect(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
       if (ret = -1) error("Error in connect!","connect");
       return sockfd;
    }
    Thanks!
    ~Inquirer
    Attached Files Attached Files
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 03:15 AM
  2. connect() function, strange error
    By Mr_Miguel in forum C Programming
    Replies: 1
    Last Post: 12-12-2006, 06:51 PM
  3. Reasons as to why fread would fail
    By Happy_Reaper in forum C Programming
    Replies: 4
    Last Post: 04-08-2006, 12:41 PM
  4. connect timeout
    By X PaYnE X in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-14-2005, 10:30 PM
  5. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM

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