Thread: Winsock Help!

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    26
    Rep Power
    0

    Winsock Help!


    Can someone help me out and give me an idea of why this returns a fail for the connection?

    #include <iostream.h>
    #include <winsock.h>
    #include <sys/types.h>
    #include <sys/socket.h>


    void main()
    {
    int sockfd;//creates an int to hold the file descriptor for the socket
    struct sockaddr_in my_addr;//creates socket struct

    sockfd=socket(AF_INET,SOCK_STREAM,0);//put a file descriptor into sockfd

    my_addr.sin_family=AF_INET;//sets socket struct to the internet
    my_addr.sin_port=htons(0);//sets socket to a port (0 uses a random port)
    my_addr.sin_addr.s_addr=INADDR_ANY;//sets socket to an IP
    memset(&(my_addr.sin_zero), '\0', 8);//set zero to the rest of the struct

    WSADATA wsaData;

    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
    {
    cout<<"WSAStartup failed."<<endl;
    exit(1);
    }

    connect(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));//connects to an IP
    if(connect(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))!=0)//connect error check
    {
    cout<<"Connect failed: "<<errno<<endl;
    exit(1);
    }

    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,161
    Rep Power
    2222
    First off, I'm surprised that it compiled. Those last two includes, the ones for sys/types.h and sys/socket.h, should have failed because they are UNIX headers, not Windows. winsock.h contains the declarations that they provide in the UNIX side. In UNIX sockets programming, you have a lot of different header files you have to remember to include; in Windows you only need to include winsock.h.

    Second, you are setting up the address struct for a server, not a client. The sockaddr_in that you set up for a client needs to contain the address and port of the server that you are trying to connect to. Your program was trying to connect to a server on your machine at a random port and could not find a server there.

    For example, assume that the IP address is contained in the char string sIpAddr and the port is contained in the int iPort. Here is how your client would initialize my_addr:
    Code:
    memset(&my_addr, 0, sizeof(my_addr));// as an alternative, 
                           // you could pre-initialize the entire struct to zero
    my_addr.sin_family=AF_INET;//sets socket struct to the internet 
    my_addr.sin_port=htons(iPort);//sets socket to the server's well-known port
    my_addr.sin_addr.s_addr=inet_addr(sIpAddr); //sets socket to the server's IP address
                                  // inet_addr converts dotted decimal string to 32-bit address
                                 // and does the host-to-network-order conversion automatically
    I know that it seems confusing at first whether the sockaddr_in contains your own address or the peer's. It all depends on what you are doing and it makes sense when you think it through.

    Another note for when you write a server:
    INADDR_ANY is in host byte-order, so it will need to be converted:
    Code:
    my_addr.sin_addr.s_addr=htonl(INADDR_ANY);
    Also, since you are working on XP, you might want to request Winsock 2.0. I believe that not doing so has caused others problems on XP:
    Code:
    if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
    Last edited by dwise1_aol; July 10th, 2003 at 12:59 AM.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    26
    Rep Power
    0
    Thanks
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    26
    Rep Power
    0
    I made the changes you told me to make so it now look like this:

    #include <iostream.h>
    #include <winsock2.h>

    void main()
    {
    int sockfd;//creates an int to hold the file descriptor for the socket
    struct sockaddr_in my_addr;//creates socket struct

    sockfd=socket(AF_INET,SOCK_STREAM,0);//put a file descriptor into sockfd

    memset(&my_addr, 0, sizeof(my_addr));//sets the struct to 0
    my_addr.sin_family=AF_INET;//sets socket struct to the internet
    my_addr.sin_port=htons(5000);//sets socket to a port (0 uses a random port)
    my_addr.sin_addr.s_addr=inet_addr("24.214.194.228");//sets socket to an IP

    WSADATA wsaData;

    if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
    {
    cout<<"WSAStartup failed."<<endl;
    exit(1);
    }

    connect(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));//connects to an IP
    if(connect(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))!=0)//connect error check
    {
    cout<<"Connect failed: "<<errno<<endl;
    exit(1);
    }

    }

    and I'm still get "Connect failed: 0" from my program when I try to run it.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    NY
    Posts
    18
    Rep Power
    0
    Your pretty close but, needed to change the order of your calls a bit. The problem is that you are trying to create a socket before you have called the WSAStartup() function. This means that your socket was invalid and was causing a failure in your connect method. Also, instead of using errno to get the last winsock error you will want to use WSAGetLastError(). This function will return the last error that occurred from a winsock call. Oh, one last thing, change sockfd from int to SOCKET.

    happy coding,
    messorian


    Code:
    #include <iostream.h>
    #include <winsock2.h>
    
    void main() {
    	SOCKET sockfd;//creates an int to hold the file descriptor for the socket
    	struct sockaddr_in my_addr;//creates socket struct
    	WSADATA wsaData;
    	
    	// make sure to initialize winsock before using any of the winsock methods
    	if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
    		cout<<"WSAStartup failed."<<endl;
    		exit(1);
    	}
    
    	memset(&my_addr, 0, sizeof(my_addr));//sets the struct to 0
    	
    	my_addr.sin_family=AF_INET;//sets socket struct to the internet
    	my_addr.sin_port=htons(5000);//sets socket to a port (0 uses a random port)
    	my_addr.sin_addr.s_addr=inet_addr("24.214.194.228");//sets socket to an IP
    
    
    	// create a socket
    	sockfd = socket(AF_INET, SOCK_STREAM, 0);//put a file descriptor into sockfd
    
    	// validate the socket
    	if (sockfd == SOCKET_ERROR) {
    		
    		cout << "An error occurred while creating a new socket. Error: " << WSAGetLastError() << endl;
    		return;
    	}
    
    	if(connect(sockfd, (sockaddr*)&my_addr, sizeof(sockaddr)) == SOCKET_ERROR) { //connect error check
    		int err = WSAGetLastError();
    		cout<<"Connect failed: " << WSAGetLastError() << endl;
    		exit(1);
    	}
    
    }
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    NY
    Posts
    18
    Rep Power
    0
    Almost forgot, the following link has a list of the errors that can be returned from WSAGetLastError(). That should help you out a ton.

    Happy coding,
    messorian

    Microsoft Winsock2 error codes
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    26
    Rep Power
    0
    Thanks a million!!

IMN logo majestic logo threadwatch logo seochat tools logo