Thread: My socket attempt refuses to connect

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    114

    My socket attempt refuses to connect

    Hi I have been reading that guide about linux sockets and I have tried to make a simple program that opens a connection, sends a test message and then closes the connection. Problem is it doesn't want to connect, no errors or anything it just displays the "Could not connect" message. Here is my code:

    Code:
    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    int main(int argc, char *argv[])
    {
       int sockfd;
       int port;
       struct sockaddr_in their_address;
    
       /* They forgot to use some arguments */
       if (argc != 3)
         {
            printf("Usage: client ip port\n");
            exit(1);
         }
    
       /* Set the IP address from the argument they gave */
       if (their_address.sin_addr.s_addr = inet_addr(argv[1]) == -1)
         {
            printf("Think the I.P. address is iffy :o\n");
            exit(1);
         }
    
       /* Start the socket file descriptor */
       if (sockfd = socket(AF_INET, SOCK_STREAM, 0) == -1)
         {
            printf("Couldn't open the socket!\n");
            exit(1);
         }
    
       /* Set the port from the argument */
       sscanf(argv[2], "%d", &port);
       their_address.sin_port = htons(port);
    
       /* Sort out the rest of the structure */
       their_address.sin_family = AF_INET;
       memset(&(their_address.sin_zero), '\0', 8);
    
       /* Open the conection to the host */
       if (connect(sockfd, (struct sockaddr *)&their_address, sizeof(struct sockaddr)) == -1)
         {
            printf("Could not connect\n");
            exit(1);
         }
    
       /* Send a simple test message */
       send(sockfd, "test", 4, 0);
    
       /* Tidy up before we go */
       close(sockfd);
    }

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I see a zero init of the address but no setting of the address to something real. You have to connect to a server. What's the server's address?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if (sockfd = socket(AF_INET, SOCK_STREAM, 0) == -1)
    Watch your operator precedence - you've written
    if (sockfd = (socket(AF_INET, SOCK_STREAM, 0) == -1 ) )

    You should have
    if ( ( sockfd = socket(AF_INET, SOCK_STREAM, 0) ) == -1)

    Other instances of this are also in the code.
    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.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    114
    Quote Originally Posted by Salem
    > if (sockfd = socket(AF_INET, SOCK_STREAM, 0) == -1)
    Watch your operator precedence - you've written
    if (sockfd = (socket(AF_INET, SOCK_STREAM, 0) == -1 ) )

    You should have
    if ( ( sockfd = socket(AF_INET, SOCK_STREAM, 0) ) == -1)

    Other instances of this are also in the code.
    Great thanks, after fixing a couple of those it works now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Using a single socket for accept() as well as connect()?
    By nkhambal in forum Networking/Device Communication
    Replies: 3
    Last Post: 10-20-2005, 05:43 AM
  3. connect timeout
    By X PaYnE X in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-14-2005, 09:30 PM
  4. socket connect returning odd
    By WaterNut in forum C++ Programming
    Replies: 5
    Last Post: 05-10-2005, 08:49 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