LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2007, 03:01 AM   #1
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189
Blog Entries: 1

Rep: Reputation: 30
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:

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.
 
Old 05-30-2007, 04:39 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
At first glance you are trying to connect to port 13. At least what the constant says. Try 80 as port to connect to and it should work. Also check out your firewall settings if they block the port you are trying to connect to.

Just a question did you compile with the option --wall. It also gives you all the warnings during compile

Regards Zhjim
 
Old 05-30-2007, 05:08 AM   #3
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189

Original Poster
Blog Entries: 1

Rep: Reputation: 30
when i add the option --wall it says unrecognized option...i tried port 50 but it still refuses my request..
 
Old 05-30-2007, 06:03 AM   #4
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Hi jaepi,

It's '-Wall', not '--wall'.

50 and 80 are not the same number! ;o)

You cannot just arbitrarily choose any port number; the server must be expecting connections on that port, otherwise it will refuse the connection. 80 (=www) would have been a good wild guess. 13 or 50 not so. -- But you shouldn't have to guess, you should *know* which ports are open.

Cheers

Rupert
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
NetBeans 4.1 Suse JSP Debug "Connection refused" EddyHahn Programming 0 04-21-2005 02:14 AM
ircd.conf: "Connection failed. Error: Connection refused" hamish Linux - Software 3 03-10-2005 07:23 AM
What is "Xlib: connection to ":0.0" refused by server " rack201 Linux - Software 1 02-04-2004 07:49 AM
lp filename yields "cannot open connection to localhost - Connection refused" jjge Linux - General 3 12-28-2003 11:02 PM
bind and named and "rndc: connect failed: connection refused" coffee9876 Linux - Networking 3 12-16-2003 10:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 02:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration