JSSE和GnuTLS配合来实现Java和C的安全通信(2)

do
        {
          ret = gnutls_handshake (session);
        }
      while (ret < 0 && gnutls_error_is_fatal (ret) == 0);

if (ret < 0)
        {
          close (sd);
          gnutls_deinit (session);
          fprintf (stderr, "*** Handshake has failed (%s)\n\n",
                  gnutls_strerror (ret));
          continue;
        }
      printf ("- Handshake was completed\n");

/* see the Getting peer's information example */
      /* print_info(session); */

for (;;)
        {
          memset (buffer, 0, MAX_BUF + 1);
          ret = gnutls_record_recv (session, buffer, MAX_BUF);

if (ret == 0)
            {
              printf ("\n- Peer has closed the GnuTLS connection\n");
              break;
            }
          else if (ret < 0)
            {
              fprintf (stderr, "\n*** Received corrupted "
                      "data(%d). Closing the connection.\n\n", ret);
              break;
            }
          else if (ret > 0)
            {
              /* echo data back to the client
              */
          printf("Server receives a packet with content:%s\n", buffer);
              gnutls_record_send (session, buffer, strlen (buffer));
            }
        }
      printf ("\n");
      /* do not wait for the peer to close the connection.
      */
      gnutls_bye (session, GNUTLS_SHUT_WR);

close (sd);
      gnutls_deinit (session);

}
  close (listen_sd);

gnutls_certificate_free_credentials (x509_cred);
  gnutls_priority_deinit (priority_cache);

gnutls_global_deinit ();

return 0;

}

GnuTLS 的详细介绍请点这里
GnuTLS 的下载地址请点这里

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/cdc1247e14587a5dac15ff91bfa32873.html