/* request client certificate if any.
gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST);
/* Set maximum compatibility mode. This is only suggested on public webservers
* that need to trade security for compatibility
static gnutls_dh_params_t dh_params;
/* Generate Diffie-Hellman parameters - for use with DHE
* kx algorithms. When short bit length is used, it might
* be wise to regenerate parameters.
* Check the ex-serv-export.c example for using static
gnutls_dh_params_generate2 (dh_params, DH_BITS);
int err, listen_sd;
int sd, ret;
struct sockaddr_in sa_serv;
struct sockaddr_in sa_cli;
char buffer[MAX_BUF + 1];
int optval = 1;
/* to disallow usage of the blocking /dev/random
gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
/* this must be called once in the program
printf ("- connection from %s, port %d\n",
inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
sizeof (topbuf)), ntohs (sa_cli.sin_port));
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
ret = gnutls_handshake (session);
if (ret < 0)
fprintf (stderr, "*** Handshake has failed (%s)\n\n",
printf ("- Handshake was completed\n");
/* see the Getting peer's information example */
/* print_info(session); */
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");
else if (ret < 0)
fprintf (stderr, "\n*** Received corrupted "
"data(%d). Closing the connection.\n\n", ret);
else if (ret > 0)
/* echo data back to the client
gnutls_record_send (session, buffer, strlen (buffer));
/* do not wait for the peer to close the connection.
gnutls_bye (session, GNUTLS_SHUT_WR);
I want to create Java client which can communicate with the server securely using TLS and x509 certificate. I'm new to Java programming. Is there any example code of Java which you can recommend me. I suppose that it's possible to create such a client but I need more information how to start and examples.
You need JSSE which is included in the JDK/JRE . The reference guide is http://docs.oracle.com/javase/1.5.0/docs/guide/security/jsse/JSSERefGuide.html and if you serach for "jsse tutorial" using your favourite search engine you will be overwhelmed by the responses. The IBM tutorial is worth reading but in some ways it is a bit dated.