TCP server socket: connection refused!

TCP server socket: connection refused!

Post by Shichen » Sun, 10 Oct 1999 04:00:00



Hello there,
We tried a simple TCP client-server app.; we started
the server at a port (say at: 8000), then we tried to start
a client connecting to the server at the same port of the same
localhost; after running the client executable, however, we got
a "con't connect to localhost.8000: connection refused" error.
We work on SunOS 5.6 platform.

Any advice will be appreciated!

Thanks,

Shicheng

Please find the enclosed code:

------------------the server code-----------------------
/* tcpmechod.c - main */

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

#include <stdio.h>

#define QLEN    5
#define BUFSIZE 4096

extern int errno;
extern char *sys_errlist[];

/*--------------------------------------------------
 * main - Concurrent TCP server for ECHO service
 *--------------------------------------------------
 */

int
main(argc, argv)
int argc;
char *argv[];
{
        char *port = "8000";                          /* the desired
port number     */
        struct sockaddr_in fsin;                /* the from address of a
client */
        int msock;                              /* master & slave
sotkets */
        fd_set rfds;                            /* read file descriptor
set     */
        fd_set afds;                            /* active file
descriptor set  */
        int alen;                               /* from address lenth
*/
        int fd, nfds;

        switch (argc) {
        case 2:
                port = argv[1];
                break;
        default:
                errexit( "usage: tcpmechod [port]\n");
        }

        msock = passivetcp(port, QLEN);
        printf("msock: %d\n", msock);

        nfds = getdtablesize();
        FD_ZERO(&afds);
        FD_SET(msock, &afds);

        while (1) {
                bcopy((char *)&afds, (char *)&rfds, sizeof(rfds));

                if (select(nfds, &rfds, (fd_set *)0, (fd_set *)0,
(struct timeval *)0) <0)
                        errexit("select: %s\n", sys_errlist[errno]);
                if (FD_ISSET(msock, &rfds)) {
                        int ssock;

                        alen = sizeof(fsin);
                        ssock = accept(msock, (struct sockaddr *)&fsin,
&alen);
                        if (ssock < 0 )
                                errexit("accept: %d\n",
sys_errlist[errno]);
                        FD_SET(ssock, &afds);
                }
                for (fd=0; fd<nfds; ++fd)
                        if (fd != msock && FD_ISSET(fd, &rfds))
                                if (echo(fd) == 0) {
                                        (void) close(fd);
                                        FD_CLR(fd, &afds);
                                }
        }

}

int
echo(fd)
int fd;
{
        char buf[BUFSIZE];
        int cc;

        cc = read(fd, buf, sizeof(buf) );
        if (cc < 0)
                errexit("echo read: %d\n", sys_errlist[errno]);
        if (cc && write(fd, buf, cc) < 0)
                errexit("echo write: %d\n", sys_errlist[errno]);
        return cc;

}

/* passivetcp.c */

/*--------------------------------------------------------------
 * passiveTCP - create a passive socket for use in a TCP server
 *--------------------------------------------------------------
 */

int
passivetcp( port, qlen )
char *port;             /* the desired port number*/
int qlen;               /* maximum sever request queue length */
{
        return passivesock( port, "tcp", qlen);

}

/* passivesock.c - passivesock */

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <netdb.h>

extern int errno;
extern char *sys_errlist[];

u_short htons(), ntohs();
u_short portbase = 8000;                /* port base, for non-root
servers */

/*-----------------------------------------------------------
 * passivesock - allocate & bind a server socket using TCP or UDP
 *-----------------------------------------------------------
 */

int
passivesock( port, protocol, qlen )
char *port;     /* the desired port     */
char *protocol; /* name of protocol to use ("tcp" or "udp") */
int qlen;       /* maximum length of the server request queue   */
{
        struct servent *pse;    /* pointer to service information entry
*/
        struct protoent *ppe;   /* pointer to protocol information entry
*/
        struct sockaddr_in sin; /* an Internet endpoint address */
        int s, type;            /* socket descriptor and socket type */
        int n = 10;

        bzero((char *)&sin, sizeof(sin));
        sin.sin_family = AF_INET;
        sin.sin_addr.s_addr = INADDR_ANY;

        if ( (sin.sin_port = htons((u_short)atoi(port))) == 0 )
                errexit("can't get \"%s\" port entry\n", port);

        printf("The port is: %d\n", sin.sin_port);

        /* Map protocol name to protocol number */
        if ( (ppe = getprotobyname(protocol)) == 0 )
                errexit("con't get \"%s\" protocol entry\n", protocol);

        /* Use protocol to choose a socket type */
        if (strcmp(protocol, "udp") == 0)
                type = SOCK_DGRAM;
        else
                type = SOCK_STREAM;

        /* Allocate a socket */
        s = socket(PF_INET, type, ppe->p_proto);
        if (s < 0)
                errexit("can't create socket: %s\n",
sys_errlist[errno]);

        /* Bind the socket */
        if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
                errexit("con't bind to %s port: %s\n", port,
sys_errlist[errno]);
        return s;

}

/* errexit.c - errexit */

#include <varargs.h>
#include <stdio.h>

/*---------------------------------------------
 * errexit - print an error message and exit
 *---------------------------------------------
 */
/*VARARGS1*/
int
errexit(format, va_alist)
char *format;
va_dcl
{
        va_list args;
        va_start(args);
        _doprnt(format, args, stderr);
        va_end(args);
        exit(1);

}

---------------------the client code------------------------
/* tcpecho.c - main */

#include <stdio.h>

extern int errno;
extern char *sys_errlist[];

#define LINELEN         128

/*-------------------------------------------
 * main - TCP client for echo service
 *-------------------------------------------
 */

int
main(argc, argv)
int argc;
char *argv[];
{
        char *host;                     /* host to use if none supplied
*/
        char *port;                     /* desired port number */

        switch (argc) {
        case 3:
                host = argv[1];
                port = argv[2];
                break;
        default:
                fprintf(stderr, "usage: tcpecho [host [port]]\n");
                exit(1);
        }
        echo(host, port);
        exit(0);

}

echo(host, port)
char *host;
char *port;
{
        char buf[LINELEN+1];    /* buffer for one line of text */
        int s, n;               /* socket, read count */

        s = connecttcp(host, port);

        while (1) {
                gets(buf);

                (void) write(s, buf, sizeof(buf) );
                n = read(s, buf, sizeof(buf));
                if (n < 0 ) {
                        errexit("read failed: %s\n",
sys_errlist[errno]);
                        exit (1);
                }
                printf("%s\n", buf);
        }

}

/* connecttcp.c*/

/*---------------------------------------------------------------------
 * connecttcp - connect to a specified port on a specified host
 *---------------------------------------------------------------------
 */

int
connecttcp( host, port)
char *host;
char *port;
{
        return connectsock( host, port, "tcp");

}

/* connectsock.c */

#include "/usr/include/sys/types.h"
#include "/usr/include/sys/socket.h"

#include "/usr/include/netinet/in.h"
#include "/usr/include/netdb.h"

#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif /*INADR_NONE */

extern int errno;
extern char *sys_errlist[];

u_short htons();
u_long inet_addr();

/*-----------------------------------------------------------
 * connectsock - allocate & connect a socket using TCP or UDP
 *-----------------------------------------------------------
 */

int
connectsock( host, port, protocol )
char *host;     /* name of host to which connection is desired */
char *port;     /* the desired port     */
char *protocol; /* name of protocol to use ("tcp" or "udp") */
{
        struct hostent *phe;    /* pointer to host information entry */
        struct servent *pse;    /* pointer to service information entry
*/
        struct protoent *ppe;   /* pointer to protocol information entry
*/
        struct sockaddr_in sin; /* an Internet endpoint address */
        int s, type;            /* socket descriptor and socket type */

        bzero((char *)&sin, sizeof(sin));
        sin.sin_family = AF_INET;

        /* Map service name to port number */
        if ( pse = getservbyname(port, protocol) )
                sin.sin_port = pse->s_port;
        else if ( (sin.sin_port = htons((u_short)atoi(port))) == 0 )
                errexit("can't get \"%s\" port entry\n", port);

        printf("port: %d\n", sin.sin_port);

        /* Map host name to IP address, allowing for dotted decimal */
        if ( phe = gethostbyname(host) )
                bcopy(phe->h_addr, (char *)&sin.sin_addr,
phe->h_length);
        else if ( (sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE
)
                errexit("con't get \"%s\" host entry\n", host);

        /* Map protocol name to protocol number */
        if ( (ppe = getprotobyname(protocol)) == 0 )
                errexit("con't get \"%s\" protocol entry\n", protocol);

        /* Use protocol to choose a socket type */
        if (strcmp(protocol, "udp") == 0)
                type = SOCK_DGRAM;
        else
                type = SOCK_STREAM;

        /* Allocate a socket */
        s = socket(PF_INET, type, ppe->p_proto);
        if (s < 0)
                errexit("can't create socket: %s\n",
sys_errlist[errno]);

        /* Connect the socket */
        if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
                errexit("con't connect to %s.%s: %s\n", host, port,
sys_errlist[errno]);
        return s;

}

/* errexit.c - errexit */

#include <varargs.h>
#include <stdio.h>

/*---------------------------------------------
 * errexit - print an error message and exit
 *---------------------------------------------
 */
/*VARARGS1*/
int
errexit(format, va_alist)
char *format;
va_dcl
{
        va_list args;
        va_start(args);
        _doprnt(format, args, stderr);
        va_end(args);
        exit(1);

}

Sent via Deja.com http://www.deja.com/
Before you buy.