Thread: Socket wont connect

  1. #1
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96

    Socket wont connect

    I'm trying to write my first socket server but i'm running into trouble, when I run the program it wont connect to a socket and i dont know why it's erroring.

    Code:
    #include <winsock2.h>
    #include <ws2tcpip.h>
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
    int status =  0; //Error checker
    int sockdis = 0; //Socket Discriptor
    
    struct addrinfo base, *server, *x; //Set up address struct
    memset(&base, 0, sizeof base);     //Clean out address struct
    base.ai_family = AF_UNSPEC;        //Set address struct's params
    base.ai_socktype = SOCK_STREAM;
    base.ai_flags = AI_PASSIVE;
    
    
    /* COLECT ADRESS INFORMATION */
    status = getaddrinfo(NULL, "3490", &base, &server);
         if(status <= 0){cout << "Error in: Getaddrinfo() \n"; return 0;}
    
    /* FIND AVALIABLE SOCKET */
    for(x = server; x != NULL; x = x->ai_next)
    {sockdis = socket(x->ai_family, x->ai_socktype, x->ai_protocol);}
        if(sockdis == -1){cout << "Error in: Socket() \n" ; return 0;}
    
    /* BIND SOCKET TO PORT */
    status = bind(sockdis, server->ai_addr, sizeof(server->ai_addrlen));
         if(sockdis <= 0){cout << "Error in: Bind() \n" ;return 0;}
    
    
    freeaddrinfo(server);
    
    system("pause");
        }//END OF MAIN

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Don't these functions set GetLastError (or some winsock equivalent)? Why don't you have that tell you what's wrong?

  3. #3
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    I was using perror() however it was returning no error; but sockdis is still returning -1

    EDIT: oh i figgured it out, i didnt have WSAstartup initalized :s
    Last edited by Matty_Alan; 04-10-2010 at 01:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  2. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  3. 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
  4. My socket attempt refuses to connect
    By kzar in forum C Programming
    Replies: 3
    Last Post: 06-01-2005, 04:15 AM
  5. socket connect returning odd
    By WaterNut in forum C++ Programming
    Replies: 5
    Last Post: 05-10-2005, 08:49 PM