socket is silently not connecting. What am i missing?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gasfusion
    New Member
    • Sep 2006
    • 59

    socket is silently not connecting. What am i missing?

    For some reason my socket isn't doing anything when i try connect. I have the header file and the class provided below. Everything works except for the socket_connect( ) function.
    Also, i do not have a server running. I have a port listener running on localhost. However, it wouldn't detect a connection. I tried connecting to google.com on port 80 and sending something, but it just wouldn't connect.
    Code:
    #include "sockets.h"
    
    /*----------------------------------------------------------------------------
    / Function: socket_start()
    / Comments: Initializes socket instance
    /*----------------------------------------------------------------------------*/
    bool sockets::socket_start() 
    {
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2,2),&wsaData) != NO_ERROR)
            return false;
        else
            return true;
    }
    
    /*----------------------------------------------------------------------------
    / Function: socket_end()
    / Comments: Ends socket instance
    /*----------------------------------------------------------------------------*/
    bool sockets::socket_end()
    {
        if (WSACleanup() != 0)
            return true;
        else
            return false;
    }
    
    /*----------------------------------------------------------------------------
    / Function: socket_create()
    / Comments: Creates a socket ready for connection
    /*----------------------------------------------------------------------------*/
    bool sockets::socket_create()
    {
        if (hSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP) != -1)
            return true;
        else
            return false;
    }
    
    /*----------------------------------------------------------------------------
    / Function: socket_start()
    / Comments: Uses accessors to retrieve port and address of a server. 
    /           Establishes a connection to that server.
    /*----------------------------------------------------------------------------*/
    bool sockets::socket_connect()
    {
        my_addr.sin_family=AF_INET;
        my_addr.sin_port=htons(getPort());
        my_addr.sin_addr.s_addr = htonl(getAddr());
        if (connect(hSocket, (const struct sockaddr*)&my_addr, sizeof(my_addr)) == -1)
            return false;
        else
            return true;
    }
    and the header file itself

    Code:
    #pragma once
    #pragma comment(lib, "Ws2_32.lib")
    #include <WinSock2.h>
    #include <windows.h>
    #include <stdio.h>
    #include <io.h>
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    class sockets
    {
    public:
        /* Constructor */
        sockets(char* _tAddr, char* _tPort)
        {
            port = atoi(_tPort);
            addr = atoi(_tAddr);
        }
      
        bool socket_start();
        bool socket_end();
        bool socket_create();
        bool socket_connect();
    
        // Accessors
        short getPort()const{return port;};
        long  getAddr()const{return addr;};
    protected:
        SOCKET hSocket;
        SOCKADDR_IN my_addr;
        int port;
        int addr;
    };
  • gasfusion
    New Member
    • Sep 2006
    • 59

    #2
    bump..anybody?

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Would you mind posting the code where you instanciate and use you socket class as well please.

      Comment

      • gasfusion
        New Member
        • Sep 2006
        • 59

        #4
        sure: here is the main.cpp
        Code:
        #include "sockets.h"
        
        int main()
        {
             sockets mysock("127.0.0.1", "80");
             if (mysock.socket_start()) 
                 cout << "socket started!\n";
                 if (mysock.socket_create())
                    cout << "socket created!\n";
                    if (mysock.socket_connect())
                        cout << "socket connected!";
                    else 
                        cout << "cannot connect!";
                        mysock.socket_end();
             getche();
             return 0;
        }

        Comment

        • gasfusion
          New Member
          • Sep 2006
          • 59

          #5
          bump bump lol this problem is killing me!

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            This is your problem is cause by this line of code.

            sockets mysock("127.0.0 .1", "80");

            the constructor of mysock is

            Code:
                sockets(char* _tAddr, char* _tPort)
                {
                    port = atoi(_tPort);
                    addr = atoi(_tAddr);
                }
            addr will have the value 127 for the socket you create because atoi converts the string up to the first invalid character and '.' is not valid for integers. Therefore your socket does not have a proper IP address.

            I would guess the class really needs to save the address as a string or an array of 4 integers (unsigned char).

            Comment

            • gasfusion
              New Member
              • Sep 2006
              • 59

              #7
              I don't think that has anything to do with it since i tried hard coding the address and the port without using the constructor of the class like so:

              my_addr.sin_por t=htons(80);
              my_addr.sin_add r.s_addr = inet_addr("127. 0.0.1");

              and it still doesn't work.

              Comment

              • gasfusion
                New Member
                • Sep 2006
                • 59

                #8
                No, i meant to say it is still an error that i was doing that. But i can just put something like "asdf345345wfas dfasdf" in the addr field it doesn't give any errors or anything. Just really odd. It doesn't do anything at all.

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  I agree that it's odd, but I am afraid I've hit the limits of my knowledge on this, sorry.

                  Comment

                  • gasfusion
                    New Member
                    • Sep 2006
                    • 59

                    #10
                    I rewrote class's functionality using function, where i got the same error.
                    Turned out that the handle to the socket needs to be passed by reference, in order for the function to access allocated memory for that socket, instead of allocating new memory. I have no idea why that didn't occur to me earlier.

                    Comment

                    • armondsarkisian
                      New Member
                      • Jan 2007
                      • 1

                      #11
                      Hi were you ever able to fix this problem? I am having the same problem you hare.. What did you do?

                      Armond

                      Comment

                      Working...