954,228 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help pls..."connect: Connection refused"

Hello there, I'm currently practicing network programming under Unix system. I have here a sample code from the tutorial. I compiled it, it has no errors. But when I execute it, it prompts me this "connect: Connection refused". What do you think is the problem. Here's my code:

#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/types.h>
#include <iostream>
#define PORT 13
#define BUFLEN 1024

using namespace std;

struct sockaddr_in s_add;
int suck_it, bind_me, r_con;
char* p_buff;
char buf[BUFLEN+1];
struct hostent* h_name;
int main(int argc, char *argv[]){
        if(argc < 2){
                cout << "Missing hostname" << endl;
                exit(1);
        }

        h_name = gethostbyname(argv[1]);

        if(!h_name){
                cout << "couldn't resolve host name" << endl;
        }

        memset(&s_add, 0, sizeof(s_add));

        s_add.sin_family = AF_INET;
        s_add.sin_port = htons(PORT);

        memcpy(&s_add.sin_addr.s_addr, h_name->h_addr_list[0], h_name->h_length);

        suck_it = socket(AF_INET, SOCK_STREAM, 0);


        if(suck_it<=-1){
                perror("im your worst nightmare");
        }

        r_con = connect(suck_it, (struct sockaddr*) &s_add, sizeof(s_add));

        if(r_con){
                perror("connect");
        }

        p_buff = buf;

        r_con = read(suck_it, p_buff, BUFLEN-(p_buff-buf));

        while(r_con){
                p_buff += r_con;
        }

        close(suck_it);

        *p_buff = ' ';
        cout << "Time: " << buf;

        return 0;
}


Help please.

jaepi
Practically a Master Poster
647 posts since Jul 2006
Reputation Points: 32
Solved Threads: 4
 

my guess would be that whatever service you're expecting is not running on the server...

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

that's my suspicion too...oh well, i'll just create a server...whatever server that is. lol

jaepi
Practically a Master Poster
647 posts since Jul 2006
Reputation Points: 32
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: