2

I've got an example socket program from my teacher -- get the datetime from the server.However, I can not run this program well. I run the server on a terminal, then run the client on another terminal(use "./client 127.0.0.1"), then I get a "connect error". I have replaced the "print" to "perror", Then I get an error message: "Connection refused".

I don't know what's wrong with this. Can you help me fix the problem? You can modify my code to make it run, then paste it here. I just want to get the current time from the server.

My code is here:

datetime.h:

#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define MAXLINE 4096

datetimes.c:

/****************************************************/
/************* datetime Example Server **************/
/****************************************************/
#include "datetime.h"
#include <sys/time.h>

int
main( int argc , char * * argv )
{
    int listenfd , connfd;
    struct sockaddr_in servaddr;
    char buff[ MAXLINE ];
    time_t ticks;

    listenfd = socket( AF_INET , SOCK_STREAM , 0 );

    memset( &servaddr , 0 , sizeof( servaddr ) );
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
    servaddr.sin_port = htons( 13 );

    bind( listenfd , (struct sockaddr *)&servaddr , sizeof( servaddr ) );
    listen( listenfd , 1024 );

    for( ; ; )
    {
        printf("Before Accept...\n");
        connfd = accept( listenfd , (struct sockaddr *)NULL , NULL );
        ticks = time( NULL );
        snprintf( buff , sizeof( buff ) , "%.24s\r\n" , ctime( &ticks ) );
        write( connfd , buff , strlen( buff ) );
        close( connfd );
    }
}

datetimec.c:

/****************************************************/
/************* datetime Example Client **************/
/****************************************************/

#include "datetime.h"

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

    if( argc != 2 )  {
        printf( "usage : a.out <IP address>\n" );
        exit( 1 );
    }

    if( ( sockfd = socket( AF_INET , SOCK_STREAM , 0 ) ) < 0 ) {
        printf( "socket error\n" );
        exit( 1 );
    }

    memset( &servaddr , 0 , sizeof( servaddr ) );
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons( 13 );

    if( inet_pton( AF_INET , argv[ 1 ] , &servaddr.sin_addr ) <= 0 )  {
        printf( "inet_pton error for %s\n" , argv[ 1 ] );
        exit( 1 ); 
    }

    if( connect( sockfd , (struct sockaddr *)&servaddr , sizeof( servaddr ) ) < 0 )  {
        printf( "connect error\n" );
        exit( 1 );
    }

    while( ( n = read( sockfd , recvline , MAXLINE ) ) > 0 )  {
        recvline[ n ] = 0;
        if( fputs( recvline , stdout ) == EOF ) {
            printf( "fputs error\n" );
            exit( 1 );
        }
    }

    if( n < 0 )  {
        printf( "read error\n" );
        exit( 1 );
    }
    exit( 0 );  
}
7
  • Use perror rather than printf to get the actual error message. Commented May 10, 2015 at 10:51
  • Thanks. I replaced print to perror, now I get the error message: Connection refused. I still can't figure it out. Commented May 10, 2015 at 11:01
  • 1
    Good. Now check the return values of all the system calls in the server too, and use perror when they fail. Commented May 10, 2015 at 11:03
  • On Linux, you need root privileges (sudo), to create a port < 1025; also you shouldn't bind on 13 and then connect from 13. Commented May 10, 2015 at 11:10
  • The server is always waiting, and I have no chance to check the return value. connfd = accept( listenfd , (struct sockaddr *)NULL , NULL ); There must be a while loop in the "accept" function. Commented May 10, 2015 at 11:14

1 Answer 1

2

You should change the port number (currently: 13) in both client and server:

servaddr.sin_port = htons( 13 );

to a value higher than 1024 because the first 1024 porst are reserved for root access and many of them are already used by some services (actually, 13 is a port number of daytime protocol).

I believe your proffesor wants you to just get datetime from the server, not use the datetime protocol.

However, your code gives a SEGFAULT when ports are corrected, because of this line:

snprintf( buff , sizeof( buff ) , "%.24s\r\n" , ctime( &ticks ) );

To fix it:

  1. Use strncpy() instead, i.e. change that line to: strncpy(buff, ctime(&ticks), MAXLINE);
  2. Use ctime() function from the proper header file: change <sys/time.h> to <time.h> in you includes.
Sign up to request clarification or add additional context in comments.

3 Comments

I get a SEGFAULT too. Then, how to fix that line? Can you give me further instruction?
I have explained in my answer how you should fix it.
Finally, I've got it down. Your answer is really useful. @REACHUS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.