problem with connecting to my own machine

This is a discussion on problem with connecting to my own machine within the Networking/Device Communication forums, part of the General Programming Boards category; ok, i am starting network programming on linux, how can i connect to my own machine through the program? I ...

  1. #1
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790

    problem with connecting to my own machine

    ok, i am starting network programming on linux, how can i connect to my own machine through the program? I have setup everything, but it either just sits there if i try connecting to some random address, or it just ends if i try to connect to my own machines address. also, when i try to telnet to my own machine , it says i dont have priveliges to do it. here is the code(i know there are probably bugs besides this, but one at a time):


    client app so far:
    Code:
    /*************************************************************************/
    /*          Twisted, copyright 2003      ,  of                           */
    /*                         Matthew Valley                                */
    /*                   Lead Designer/Programmer                            */
    /*                    CodeMonkeys, Copyright 2003                        */
    /*************************************************************************/
    
    
    /*************************************************************************/
    /*                 Includes:                                             */
    /*                        Here is where all of our needed header files   */
    /*                                 Are included                          */
    /*************************************************************************/
    
    #include <stdio.h>
    #include <iostream>
    using namespace std;
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    
    /*************************************************************************/
    /*          Definitions                                                  */
    /*************************************************************************/
    #define SERVER_PORT 6666
    
    
    /*************************************************************************/
    /*        Classes                                                        */
    /*************************************************************************/
    
    class PacketWrap
    {
    public :
    
    char *whosaid;
    int packlen;
    char *packet;
    };
    
    class ClientInfo
    {
    private:
     
     int sock;                            // this is our socket
     struct sockaddr_in client_addr;      // this is the address info needed for the socket to connect
     hostent* he;                         // this containt our host info
     PacketWrap Packet;            // this contains our packet info
     
     public:
     
     
     void GetHostByName(char* argv);      // this initiats and sets our host info
     void SetupAndConnect();              // this sets up and connects the sockets
     void Send(char *string);             // sends a string
     void ReadAndDisplay();               // reads in packets and displays them
     };
     
    
    
     /*************************************************************************/
     /*       FUNCTION:   sendall                                             */
     /*       PURPOSE :   to keep sending a packet until it is finished       */
     /*                   sending                                             */
     /*************************************************************************/
      int sendall(int s, char *buf, int *len)
        {
            int total = 0;        // how many bytes we've sent
            int bytesleft = *len; // how many we have left to send
            int n;
    
            while(total < *len) {
                n = send(s, buf+total, bytesleft, 0);
                if (n == -1) { break; }
                total += n;
                bytesleft -= n;
            }
    
            *len = total; // return number actually sent here
    
            return n==-1?-1:0; // return -1 on failure, 0 on success
        }  
     /*************************************************************************/
     /*       FUNCTION:   ClientInfo::GetHostByName(char* argv)               */
     /*       PURPOSE :   to Setup our Host info by reading in the IP address */
     /*                   of the server and planting it into our hostinfo     */
     /*************************************************************************/
     void ClientInfo::GetHostByName(char* argv)  
     {
     
     he = gethostbyname(argv);
     }
     
     /*************************************************************************/
     /*       FUNCTION:   ClientInfo::SetupAndConnect()                       */
     /*       PURPOSE :   To setup the address info for the socket, then      */
     /*                   Connect it to the Server                            */
     /*************************************************************************/
    void ClientInfo::SetupAndConnect()
    {
    
    
    
    
    client_addr.sin_family = AF_INET; // setup sockaddr family to AF_INET
    client_addr.sin_port   = htons(SERVER_PORT); // Setup Destination port
    client_addr.sin_addr= *((struct in_addr *)he->h_addr); // setup Destination IP address
    memset(&(client_addr.sin_zero),'\0',8); // set rest to zero
    
    
    
       if((sock = socket(AF_INET,SOCK_STREAM,0)) == -1){ // setup our socket with error clause
       
       perror("socket");
       exit(1);
       }
          
        
        if(connect(sock,(struct sockaddr*)&client_addr,sizeof(struct sockaddr)) == -1){// connect to address
        
        perror("connect");
        exit(1);
        }
       
     }
       
    
     /*************************************************************************/
     /*       FUNCTION:   ClientInfo::Send(char *string)                      */
     /*       PURPOSE :   encapsulates a packet, then sends it                */
     /*************************************************************************/
     
    void ClientInfo::Send(char *string) 
    {
    
    Packet.whosaid = inet_ntoa(client_addr.sin_addr);
    Packet.packlen = strlen(strcat(Packet.whosaid,string)) + 4;
    Packet.packet = Packet.whosaid;
    Packet.packet = strcat(Packet.packet,(const char*)Packet.packlen);
    Packet.packet = strcat(Packet.packet,string);
    
    sendall(sock,Packet.packet,&Packet.packlen);
    }
    
    void ClientInfo::ReadAndDisplay()
    {
    
    }
    
       
    int main(int argc, char *argv[]){
    
    ClientInfo ci;
    int choice;
    
     if (argc != 2) {
                fprintf(stderr,"usage: client hostname\n");
                exit(1);
            }
    ci.GetHostByName(argv[1]); // setup he
    
    printf (" \n************************8 Twisted 8*******************\n");
    printf ("pick a choice:\n");
    printf ("1) Connect\n");
    printf ("2) Readme\n");
    printf ("3) Exit\n");
    
    cin >> choice;
    switch (choice)
    {
    
    case 1:
    ci.SetupAndConnect();
    break;
    
    case 2:
    break;
    
    case 3:
    exit(1);
    
    return 0;
    }
    
    return 0;
    }
    and here is the program that is supposed to respond so far:
    Code:
    #include <stdio.h>
    #include <iostream>
    #include <errno.h>
    #include <string.h>
    #include <unistd.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #define BACKLOG 10
    #define LIST_PORT 6666
    
    
    int SetupSocket()
    {
    
    int sock;
    struct sockaddr_in addr_info;
    int yes = 1;
    addr_info.sin_family = AF_INET; // setup sockaddr family to AF_INET
    addr_info.sin_port   = htons(LIST_PORT); // Setup Destination port
    addr_info.sin_addr.s_addr = 0; // setup Destination IP address
    memset(&(addr_info.sin_zero),'\0',8); // set rest to zero
    
    
       if((sock = socket(AF_INET,SOCK_STREAM,0)) == -1){ // setup our socket with error clause
       
       perror("socket");
       exit(1);
       }
       
       if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { // set socket so its reusable
                perror("setsockopt");
                exit(1);
            }
       
       if(bind(sock,(struct sockaddr*)&addr_info,sizeof(struct sockaddr))){ // bind the socket with error clause
        
        perror("bind");
        exit(1);
        }
          
        
        if(listen(sock,BACKLOG) == -1){// connect to address
        
        perror("listen");
        exit(1);
        }
        
       return sock;
     }
    int main()
    {
    int bytesent;
    int sock,s_client;
    socklen_t sinsize;
    char buf[1024];
    char szIntro[] ="Hello, welcome to Twisted Mongrel what do you want to do:\n";
    char szIntro_choice[]="1) start a Hack \n 2)continue a Hack \n 3)exit\n";
    
    struct sockaddr_in client_info;
    
    
    sock = SetupSocket();
    
    
    
    while(1){// maina accept loop
    
    sinsize = sizeof(struct sockaddr_in);
    
    s_client = accept(sock,(struct sockaddr*) &client_info ,&sinsize);  // accept connect request
    
    if((bytesent = send(s_client,szIntro,strlen(szIntro),0)) <= 2)// check if bytes are getting through
    {
    printf("no bytes getting through");
    }
    
    send(s_client,szIntro_choice,strlen(szIntro_choice),0);
    
    
       
      }
    return 0;
    }
    thanks in advance
    Last edited by EvBladeRunnervE; 07-23-2003 at 10:53 AM.

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    either just sits there if i try connecting to some random address
    That's normal, unless the random address is actually one being used, and the other computer is on and has a server set up listening on the port your client uses.
    or it just ends if i try to connect to my own machines address.
    So? Your program doesn't tell it to do anything once it connects, so it just connects then quits.

    Another thing, I don't know if it's important in your case, but I don't like the ClientInfo::Send(char* string). In this case, the function will only work if string is a null-terminated string, and will probably die horribly if it isn't. Depending on whether you just want to send text strings etc. this might be very annoying if you keep having to add a null 0 to the end of your data segments.

    *Also, please properly indent your code next time you post.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    ok, actually, it is just refusing to connect, with the message "connection not allowed", and btw, if you look and see, the 'code' tags are there, and therefore i follow the rules.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    "connection not allowed"? Hold on, maybe I'm wrong, but I didn't see anything in there that displays error messages. Where did it come from?

    Also, I wasn't referring to code tags I meant the indentation, which is the tabbing and stuff. It's pretty well indented at the top, but after GetHostByName(), there are huge white-space gaps and entire functions that are completely un-tabbed. Just thought I'd point this out, since it would be a lot easier to read that way.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    Code:
    if(connect(sock,(struct sockaddr*)&client_addr,sizeof(struct sockaddr)) == -1){// connect to address
        
        perror("connect");
        exit(1);
        }
    the perror prints the error if there is one.

    and i will try to tab stuff and indent it, although it looks fine by me at 1024x768

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oh, I see. But then, how do you know it's "connection not allowed"? How are you connecting to your own machine? Are you connecting by "localhost" (I hope that's not just a Windows-specific thing) or are you finding your machine's ip on the internet then trying to connect to that?

    **EDIT**
    I think your problem might be that you need to call WSAStartup() at the beginning of the program and and WSACleanup() at the end.. I don't see that in your code
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    think your problem might be that you need to call WSAStartup() at the beginning of the program and and WSACleanup() at the end.. I don't see that in your code
    actually, that is a windows specific command, and unnecesary under linux.

    about connecting to my own machine, i have done it both by localhost and internet IP, i believe i might need to somehow integrate permissions in order to connect

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    that is a windows specific command
    Ahhh damn... lol

    Integrate permissions? lol didn't even know there was such thing... I thought you just needed to go bind(), listen() and accept() and then call connect()...

    And anyways, how are you getting the specific message "connection not allowed"? I only see a set error message defined by you
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    I only see a set error message defined by you
    no, its system defines based on different stuff, much like GetLastError(), except is done to only display connect's error.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ahh, I should read the docs more carefully I looked and all I saw was "Prints a string", but I missed "followed by the last error"...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Are you still having troubles with this? If so:

    Start your server, and run netstat -an to see connections and listening ports. Make sure you are listening on the correct port number and IP. You should see something like:
    Code:
    TCP    0.0.0.0:6666           0.0.0.0:0              LISTENING
    If you see it listening, you can then simply telnet to it:
    >>telnet localhost 6666
    This will hopefully give up a clue as to the location of your problem (client or server), by eliminating the client from the test.

    If this works, you could also try to connect your client to one of your existing open ports (like a telnet server or FTP server, or any benign service). Again, this is only for a kind of debugging exercise, helping you to pin point your problem.
    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. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  3. Problem connecting to MySQL Database?
    By MrLucky in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2006, 10:30 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21