I am trying to write a simple echo server. I first decided to try it
with the BIO without any keys. I keep having problems with blocking. It
seems that it gets stuck somewhere expecting a read or a write. You will
notice on the client that I do BIO_write. Then I do a BIO_read in a loop
to get the data. This comes across, but then when I try to do a
BIO_write the second time, it blocks and I get no return. What am
I doing wrong? 

I have the OpenSSL book and it just has writes from the client and the
server just prints it out. Is there a simple example of an echo server
and client somewhere?

brian

=== simple_client.c ===

#include "openssl/ssl.h"
#include "openssl/bio.h"
#include "openssl/err.h"

#include "stdio.h"
#include "string.h"

int main()
{
    BIO * bio;
    int p;

    char * request = "Some new things coming today\n";
    char * request2 = "Tomorrow is a new day\n";
    char r[1024];

    /* Set up the library */

    ERR_load_BIO_strings();
    SSL_load_error_strings();
    OpenSSL_add_all_algorithms();

    /* Create and setup the connection */

    bio = BIO_new_connect("mary.internal.brie.com:4422");
    if(bio == NULL) { printf("BIO is null\n"); return; }

    if(BIO_do_connect(bio) <= 0)
    {
        ERR_print_errors_fp(stderr);
        BIO_free_all(bio);
        return;
    }

    /* Send the request */

    BIO_write(bio, request, strlen(request));
    for(;;)
    {
        p = BIO_read(bio, r, 1023);
        if(p <= 0) break;
        r[p] = 0;
        printf("%s", r);
    }

    BIO_flush(bio);

    if (BIO_should_read(bio))
        printf("Bio needs to read\n");

    BIO_write(bio, request2, strlen(request2));

    /* Read in the response */

    for(;;)
    {
        p = BIO_read(bio, r, 1023);
        if(p <= 0) break;
        r[p] = 0;
        printf("%s", r);
    }


    /* Close the connection and free the context */

    BIO_free_all(bio);
    return 0;
}

=== simple_server.c ===

#include "stdio.h"
#include "string.h"

#include "openssl/bio.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
#define BUFSIZE 1024


int main()
{
    BIO  *abio, *out ;
    pthread_t tid;
    char buf[80];
    int err, nread;


    printf("Secure Programming with the OpenSSL API, Part 4:\n");
    printf("Serving it up in a secure manner\n\n");
   
    SSL_library_init();

    SSL_load_error_strings();
    ERR_load_BIO_strings();
    ERR_load_SSL_strings();
    OpenSSL_add_all_algorithms();


    printf("Attempting to create BIO object... ");

    
    abio = BIO_new_accept("4422");

    printf("Waiting for incoming connection...\n");

    if(BIO_do_accept(abio) <= 0)
    {
        ERR_print_errors_fp(stdout);
        BIO_free_all(abio);
        return -1;
    }

        if (BIO_do_accept(abio) <= 0)
        {
                ERR_print_errors_fp(stderr);
                BIO_free_all(abio);
                return -1;
        }
        printf("Got a connection\n");

        out = BIO_pop(abio);


    do
    {
        for (nread = 0;  nread < sizeof(buf);  nread += err)
        {
             err = BIO_read(out, buf + nread, sizeof(buf) - nread);
             printf("The number of bytes read %d\n",err);
             if (BIO_should_retry(out) )
                printf("Should retry read\n");
             if (err <= 0)
                 break;

             if ( err > 0 )
             {
                BIO_puts(out,"Hello from the server\n");
                BIO_write(out,buf,err);
                fprintf(stdout, "%s", buf);
             }
        }
    }
    while (err > 0);

    if (err < 0)
        printf("Error on read\n");
    
        

    

// replacement ends here

    BIO_free_all(abio);

}



-- 
Brian Lavender
http://www.brie.com/brian/
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to