Hi. am starting out with network programming and unix network programming book. all its trying to do is connect to server and read time, but when i run it it says : 'connect error: Connection refused'

Here's the code :

#include    "unp.h"

main(int argc, char **argv)
    int sockfd, n;
    char                recvline[MAXLINE + 1];
    struct sockaddr_in  servaddr;

    if (argc != 2)
        err_quit("usage: a.out <IPaddress>");

    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        err_sys("socket error"); 

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port   = htons(14);    /* daytime server */
    if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
        err_quit("inet_pton error for %s", argv[1]);

    if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
        err_sys("connect error");

    while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
        recvline[n] = 0;    /* null terminate */
        if (fputs(recvline, stdout) == EOF)
            err_sys("fputs error");
    if (n < 0)
        err_sys("read error");


Note that it compiles with no errors. Please help this aspiring programmer. Thank you

Edited by nhrnjic6_1

3 Years
Discussion Span
Last Post by rubberman

Chances are that the server is not listening on port 14. Port 13 is used for daytime services. Also, you may want to connect with an NTP server, which would be listening on port 123.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.