Thread: Winsock client code help.... very basic.

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Winsock client code help.... very basic.

    Hello all, Im new to winsock, and Im having troubles with my C client. It is just intended for basic connectivity as of now. The code compiles fine, but when i run it, i never get it to display the "connected to the server" message, meaning it doesnt connect. Heres the code, and thanks!
    Code:
    /* Winsock Simple client code ( attempt anyway )*/
    /* By Jay_Tech */
    
    #include <stdio.h>
    #include <winsock.h>
    
    main()
    
    {
    
    WSADATA WsaDat;
    
    if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
    {
    printf("WSA Initialization failed!\n");
    WSACleanup();
    }
    
    else printf("Winsock initialized!\n");
    
    SOCKET Socket;
    Socket= socket(AF_INET,SOCK_STREAM,0);
    if(Socket== INVALID_SOCKET)
    {
    printf("Attempt to create socket failed!\n");
    }
    
    else printf("Socket created successfully!\n");
    
    printf("Attempting to connect to host...\n");
    
    
    
    
    /* Address and port info */
    SOCKADDR_IN SockAddr;
    SockAddr.sin_port= htons(6666);
    SockAddr.sin_family= AF_INET;
    SockAddr.sin_addr.s_addr= inet_addr("twisted.dal.net");
    
    //inet_addr("twisted.dal.net");
    
    if (connect(Socket,(SOCKADDR*)(&SockAddr), sizeof(SockAddr))!=0)
    {
    printf("Attempt to connect to host failed!\n");
    
    }
    
    else printf("Connected to host.\n");
    WSACleanup();
    return 0;
    
    }
    [edit]Code tags added by Hammer

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Make use of some breakpoints. What does the program output?

    Kuphryn

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    Exclamation The program....

    Goes step by step, intializing winsock, creating the socket, setting address information, and then connecting to host. THroughout all the steps, it has error check. Everything runs fine, but When it comes to the connecting part, it just never connects, and displays "Couldnt connect to host". I know the host is a valid host as well as the port. ANy ideas?

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try using WSAGetLastError()
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Consider a different port and even a different server.

    Kuphryn

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Reply

    Well, it has something to do with INET_ADDR().
    It only accepts ip address, not domain name addresses. The way around this is by using gethostbyname(), but I dont quite understand the command. Can someone explain how it works or how it fits into the code? Thanks.

    P.S. Kuphryn.

    >Consider a different port and even a different server.

    That has nothing to do with why it wont connect.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Exclamation Problem solved

    Found how to use gethostbyname() PROPERLY, and fixed the problem. Thanks to all who helped.

    Code:
    /* Winsock example client connection by domain name resolving by Jay*/
    #include <stdio.h>
    #include <winsock.h>
    main()
    {
    
      /* Initialize Winsock */
        WSADATA WsaDat;
    
         if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
               {
                printf("Winsock Initialization failed!\n");
                WSACleanup();
               }
    
               else printf("Winsock initialized!\n");
    
      /* Create socket */
      SOCKET client;
      client=socket(AF_INET, SOCK_STREAM, 0);
      printf("Socket created.\n");
    
      /* Get host (resolve domain name to an IP address) */
         LPHOSTENT lpHostEntry;
    
    	lpHostEntry = gethostbyname("twisted.dal.net");
        if (lpHostEntry == NULL)
        {
            printf("Error resolving host name!\n");
            WSACleanup();
            return 0;
        }
    
        else printf("Host name resolved!\n");
    
    
      /* Address info */
      SOCKADDR_IN SockAddr;
      SockAddr.sin_family= AF_INET;
      SockAddr.sin_addr= *((LPIN_ADDR)*lpHostEntry->h_addr_list);
      SockAddr.sin_port= htons(6666);
    
      /* Connect */
      if (connect( client,(LPSOCKADDR)(&SockAddr), sizeof(SockAddr))== SOCKET_ERROR)
      {
      printf("Could not connect to host!\n");
      WSACleanup();
      }
    
      /* Connected to host. End of program */
    
      else printf("Connected to host!...\n");
      WSACleanup();
      printf("Disconnected. Have a nice day!\n");
      return 0;
      }

    Code tags added by Hammer

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. Any further questions or ways I can help please feel free to PM me.

    Good Luck,
    Hammer
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie needing help with basic code!
    By TeZ258 in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2007, 08:35 PM
  2. Need winsock select() client & server source code or tutorial
    By draggy in forum Networking/Device Communication
    Replies: 2
    Last Post: 06-19-2006, 11:49 AM
  3. basic code help
    By matt37664 in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2006, 01:40 PM
  4. Multithreaded Winsock
    By X PaYnE X in forum Windows Programming
    Replies: 6
    Last Post: 01-05-2004, 09:00 AM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM