plough
plough

Reputation: 81

Connect error: simple socket programming in C in Ubuntu

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 );  
}

Upvotes: 2

Views: 536

Answers (1)

syntagma
syntagma

Reputation: 24324

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.

Upvotes: 2

Related Questions