Connection refused problem

  • Follow


Hi to all,

   I started learning socket programming, and i created a simple
server-client in order to experiment. The server justs sends back to the
client the time of the server machine.
   The problem is that i always get a connection refused error. 
Here is the code:

Server >

#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>

extern int daemonize(void);
static void serve(int);

#define PORT 4040

main( int argc, char **argv )
{
	int lsd;			/* Listening socket */
	struct sockaddr_in sin;		/* Binding struct */
	int sin_size=sizeof(sin);
	int sd;				/* Socket to accept new connexion */

	/* Create listening socket */

	if ( (lsd=socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
		fprintf(stderr, "%s: cannot create listening socket: ", argv[0]);
		perror(0);
		exit(1);
	}

	/* Bind socket to port */

	sin.sin_family	    = AF_INET;
	sin.sin_port	    = htons(PORT);
	sin.sin_addr.s_addr = htonl(INADDR_ANY);

	if ( bind(lsd, &sin, sin_size) < 0 ) {
		fprintf(stderr, "%s: cannot bind listening socket: ", argv[0]);
		perror(0);
		exit(1);
	}

	/* Initiate a listen queue */

	if ( listen(lsd, 5) < 0 ) {
		fprintf(stderr, "%s: cannot listen on socket: ", argv[0]);
		perror(0);
		exit(1);
	}

	/* Become a daemon */
#ifndef DEBUG
	if ( daemonize() < 0 ) {
		fprintf(stderr, "%s: cannot disconnect from controlling terminal: ", argv[0]);
		perror(0);
		exit(1);
	}
#endif 
	/* Take care of the SIGPIPE signal - ignore it */
	signal(SIGPIPE, SIG_IGN);

	/* Ready to accept connexions */
	while ( 1 ) {
		if ( (sd=accept(lsd, &sin, &sin_size)) < 0 )
			exit(errno);
		serve(sd);
		shutdown(sd, 2);
		close(sd);
	}
}


/* Tell the time to socket descriptor sd */

void serve(int sd)
{
	time_t local_time;
	char *time_string;

	time(&local_time);
	time_string = ctime(&local_time);

	write(sd, time_string, strlen(time_string));

	return;
}

daemonize.c >

#include <netdb.h>
#include <stdio.h>
int daemonize(void)
{
	switch ( fork() )
	{
		case -1: return -1;

		case  0: close(0);
			 close(1);
			 close(2);
			 return 0;

		default: exit(0);
	}
}

client >

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define BUFSIZE		1024
#define SERVER_PORT	4040

main( int argc, char **argv )
{
	int sd;				/* Socket descriptor */
	struct sockaddr_in server;	/* Server to connect */
	struct hostent *server_host;	/* Host info */
	char buf[BUFSIZE];
	int nbytes;

	/* Create socket */
	if ( (sd=socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
		fprintf(stderr, "%s: cannot create socket: ", argv[0]);
		perror(0);
		exit(1);
	}

	/* Get info on host */
	if ( (server_host=gethostbyname(argv[1])) == NULL ) {
		fprintf(stderr, "%s: unknown host %s\n", argv[0], argv[1]);
		exit(1);
	}

	/* Set up struct sockaddr_in */
	server.sin_family = AF_INET;
	server.sin_port   = SERVER_PORT;
	bcopy((char*)server_host->h_addr, (char*)&server.sin_addr, 
				server_host->h_length);

	/* Connect */
	if ( connect(sd, &server, sizeof(server)) < 0 ) {
		fprintf(stderr, "%s: cannot connect to server: ", argv[0]);
		perror(0);
		exit(1);
	}

	/* Get date */

	if ( (nbytes=read(sd, buf, BUFSIZE-1)) <= 0 ) {
		fprintf(stderr, "%s: read failed: ", argv[0]);
		perror(0);
		exit(1);
	}

	buf[nbytes] = 0;
	printf("Date on host %s is: %s\n", argv[1], buf);

	close(sd);
	exit(0);
}
  
Any help would be valuable. 
Thank you
0
Reply nobody 1/10/2006 6:18:52 PM

"nobody" <nobody@do-not-spam.me> wrote in message
news:pan.2006.01.10.18.18.51.707065@do-not-spam.me...
>    I started learning socket programming, and i created a simple
> server-client in order to experiment. The server justs sends back to the
> client the time of the server machine.
>    The problem is that i always get a connection refused error.
> Here is the code:
>
> Server >
[snip]
> sin.sin_family     = AF_INET;
> sin.sin_port     = htons(PORT);
> sin.sin_addr.s_addr = htonl(INADDR_ANY);

Here, you (correctly) use htons() to convert the port to network byte order.

[snip]
> client >
[snip]
> server.sin_family = AF_INET;
> server.sin_port   = SERVER_PORT;
> bcopy((char*)server_host->h_addr, (char*)&server.sin_addr,
> server_host->h_length);

Here, you don't. And there is no reason to use bcopy() - use memcpy()
instead.

(I didn't bother looking for any other faults.)

Alex


0
Reply Alex 1/10/2006 6:30:54 PM


On Tue, 10 Jan 2006 18:30:54 +0000, Alex Fraser wrote:

>> Server >
> [snip]
>> sin.sin_family     = AF_INET;
>> sin.sin_port     = htons(PORT);
>> sin.sin_addr.s_addr = htonl(INADDR_ANY);
> 
> Here, you (correctly) use htons() to convert the port to network byte order.
> 
> [snip]
>> client >
> [snip]
>> server.sin_family = AF_INET;
>> server.sin_port   = SERVER_PORT;
>> bcopy((char*)server_host->h_addr, (char*)&server.sin_addr,
>> server_host->h_length);
> 
> Here, you don't. And there is no reason to use bcopy() - use memcpy()
> instead.
> 
> (I didn't bother looking for any other faults.)

Thanks Alex. This was finally the bug...

> Alex

0
Reply nobody 1/10/2006 7:21:30 PM

nobody wrote:
> Hi to all,
>
>    I started learning socket programming, and i created a simple
> server-client in order to experiment. The server justs sends back to the
> client the time of the server machine.
>    The problem is that i always get a connection refused error.
> Here is the code:
>
> Server >
>
> #include <sys/types.h>
> #include <sys/time.h>
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <termios.h>
> #include <unistd.h>
> #include <errno.h>
> #include <signal.h>
> #include <stdio.h>
>
> extern int daemonize(void);
> static void serve(int);
>
> #define PORT 4040
>
> main( int argc, char **argv )
> {
> 	int lsd;			/* Listening socket */
> 	struct sockaddr_in sin;		/* Binding struct */
> 	int sin_size=sizeof(sin);
> 	int sd;				/* Socket to accept new connexion */
>
> 	/* Create listening socket */
>
> 	if ( (lsd=socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
> 		fprintf(stderr, "%s: cannot create listening socket: ", argv[0]);
> 		perror(0);
> 		exit(1);
> 	}
>
> 	/* Bind socket to port */
>
> 	sin.sin_family	    = AF_INET;
> 	sin.sin_port	    = htons(PORT);
> 	sin.sin_addr.s_addr = htonl(INADDR_ANY);

Here you correctly use htons to set the port number into the sin
structure

(...)
> 	/* Create socket */
> 	if ( (sd=socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
> 		fprintf(stderr, "%s: cannot create socket: ", argv[0]);
> 		perror(0);
> 		exit(1);
> 	}
>
> 	/* Get info on host */
> 	if ( (server_host=gethostbyname(argv[1])) == NULL ) {
> 		fprintf(stderr, "%s: unknown host %s\n", argv[0], argv[1]);
> 		exit(1);
> 	}
>
> 	/* Set up struct sockaddr_in */
> 	server.sin_family = AF_INET;
> 	server.sin_port   = SERVER_PORT;

Here you do not.  If you're running on a little-endian machine, line a
pentium or athlon etc, this will fail.  karl m

0
Reply Karl 1/11/2006 12:13:56 AM

3 Replies
478 Views

(page loaded in 0.103 seconds)

Similiar Articles:













7/23/2012 9:49:38 AM


Reply: