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.

Dani AI

Generated

The message "connect: Connection refused" normally means the TCP handshake was actively rejected by the remote end — most commonly because no server is listening on the target port, or a firewall is rejecting the attempt. 's guess that "the service you're expecting is not running" is the likeliest cause here. Port 13 is the well-known "daytime" service and is often disabled on modern systems, so the client can be correct while the server side is simply absent.

Quick diagnostics to run from the client machine: try a raw connect with telnet hostname 13 (or nc -vz hostname 13); if that fails, check the server for a listening TCP socket with ss -ltnp | grep ':13' (or netstat -ltnp | grep ':13' on older Unix). If no listener shows up, start a server process on that port or pick a non-privileged port (>1024) for development. Also check host-side firewalls (iptables -L -n or ufw status) and whether the server is bound only to localhost instead of the expected interface.

A couple of code-level problems in the posted client will make things worse even after the server exists: the read loop never calls read again so it becomes an infinite loop, and the buffer is terminated with a space instead of a NUL byte. Use proper return-value checks (if (sockfd < 0) and if (connect(...) < 0)) and a robust read loop, for example:

ssize_t n;
char *p = buf;
size_t left = BUFLEN;
while ((n = read(suck_it, p, left)) > 0) {
    p += n;
    left -= n;
}
if (n < 0) perror("read");
*p = '\0';

Also consider using getaddrinfo instead of gethostbyname for IPv4/IPv6 safety, include the proper headers (<unistd.h>, <cstring>), and test with an ephemeral port during development so you do not need root to bind privileged ports like 13.

Recommended Answers

All 2 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.