Subject: Re: Network socket application problem
To: Jeremy C. Reed <netbsd-help@netbsd.org>
From: Joe Magee <jmagee@starrider.com>
List: netbsd-help
Date: 09/27/2002 00:53:08
Jeremy,

Thanks for writing. The source files are included in this email.

Using netstat, I discovered that the server is running, however it appears
to be listening on port 65534 (0xfffe) rather than the hard coded value 9734
in the source code. (The code is sample code from a book, and the port 9734
was an arbitrary choice by the author.) I use this sample code here because
my application attempts to utilize sockets in a fashion similar to this, and
this code exhibits the same problem as my code.

Using telenet on port 9734 I got "connection refused." However, a telnet to
port 65534 did connect--and apparently to the server I compiled and ran.

I'm guessing some kind of network order problem, or some kind of data size
issue with sin_port in the struct sockaddr_in. I will investigate that.
However, here's the code. Note: these two files compile and run as described
in the book on a linux 7.3 system.

Joe Magee

Client Code (client2.c)=======================

/*  Make the necessary includes and set up the variables.  */

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

int main()
{
    int sockfd;
    int len;
    struct sockaddr_in address;
    int result;
    char ch = 'A';

/*  Create a socket for the client.  */

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

/*  Name the socket, as agreed with the server.  */

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = 9734;
    len = sizeof(address);

/*  Now connect our socket to the server's socket.  */

    result = connect(sockfd, (struct sockaddr *)&address, len);

    if(result == -1) {
        perror("oops: client2");
        exit(1);
    }

/*  We can now read/write via sockfd.  */

    write(sockfd, &ch, 1);
    read(sockfd, &ch, 1);
    printf("char from server = %c\n", ch);
    close(sockfd);
    exit(0);
}

server code (server2.c)=======================

/*  Make the necessary includes and set up the variables.  */

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

int main()
{
    int server_sockfd, client_sockfd;
    int server_len, client_len;
    struct sockaddr_in server_address;
    struct sockaddr_in client_address;

/*  Create an unnamed socket for the server.  */

    server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

/*  Name the socket.  */

    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
    server_address.sin_port = 9734;
    server_len = sizeof(server_address);
    bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

/*  Create a connection queue and wait for clients.  */

    listen(server_sockfd, 5);
    while(1) {
        char ch;

        printf("server waiting\n");

/*  Accept a connection.  */

        client_len = sizeof(client_address);
        client_sockfd = accept(server_sockfd,
            (struct sockaddr *)&client_address, &client_len);

/*  We can now read/write to client on client_sockfd.  */

        read(client_sockfd, &ch, 1);
        ch++;
        write(client_sockfd, &ch, 1);
        close(client_sockfd);
    }
}



----- Original Message -----
From: "Jeremy C. Reed" <reed@reedmedia.net>
To: "Joe Magee" <jmagee@starrider.com>
Cc: <netbsd-help@netbsd.org>
Sent: Friday, September 27, 2002 12:27 AM
Subject: Re: Network socket application problem


> On Thu, 26 Sep 2002, Joe Magee wrote:
>
> > I am creating an application utilizing sockets. However, the client
always
> > reports connection refused.
>
> You may want to share some sample code of the server (and maybe client
> too).
>
> Also use netstat (and fstat) to see if the server is even LISTENing.
>
> And instead of using your client, also try just using telnet or netcat to
> connect to the server (for testing).
>
>    Jeremy C. Reed
>    http://www.reedmedia.net/
>
>