Thread: Socket connection problem

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Socket connection problem

    Hello There, I am trying to change a simple client code that will retrieve the web pages from any web site using TCP sockets. It fails in the connect function.
    When I made the code run on my m/c (with a server), it worked fine but it doesnt create connection to other web sites like google and all. I have a similar Java code that works but don't know where exactly am I going wrong.

    I am running it on a 64 bit AMD Turion X2 with Ubuntu 9.10

    Code :

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <netdb.h>

    #define MAXRCVLEN 500
    #define PORTNUM 80

    int main(int argc, char *argv[])
    {
    char buffer[MAXRCVLEN + 1];
    int len, mysocket;
    struct sockaddr_in dest;
    int ret;
    struct hostent * hinfo;
    char * sname;
    if (argc > 1)
    {sname=argv[1];}
    else
    {sname="cprogramming.com";}

    mysocket = socket(AF_INET, SOCK_STREAM, 0);

    memset(&dest, 0, sizeof(dest));
    dest.sin_family = AF_INET;

    hinfo = gethostbyname(sname);
    if(hinfo == NULL) {printf("getbyname failed!\n");}

    dest.sin_addr.s_addr = inet_addr(hinfo->h_addr);

    dest.sin_port = htons(PORTNUM);

    ret = connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));
    printf("Connection status:%d\n", ret); /* ret is -1 here*/

    len = recv(mysocket, buffer, MAXRCVLEN, 0);
    buffer[len] = '\0';

    close(mysocket);
    return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    well, my friend helped me out. i made the following change
    As was:
    dest.sin_addr.s_addr = inet_addr(hinfo->h_addr);
    Changed To:
    dest.sin_addr = *(struct in_addr *)(hinfo->h_addr);

    Now the connection is working but recv gets hanged. Probably need to send some formatted request to server. But why dont we need to do that request in Java?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Destination Port Problem
    By radeberger in forum Networking/Device Communication
    Replies: 15
    Last Post: 03-26-2009, 11:00 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Winsock connection problem
    By Nephiroth in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2006, 07:54 AM
  4. Please Help! Socket Problem
    By robsmith in forum C Programming
    Replies: 2
    Last Post: 05-06-2005, 12:35 PM
  5. Varifying Socket Connection :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 05-11-2002, 03:26 PM