<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[wolfSSL - Embedded SSL Library — TLS Handshake exchanging messages]]></title>
		<link>https://www.wolfssl.com/forums/topic2056-tls-handshake-exchanging-messages.html</link>
		<atom:link href="https://www.wolfssl.com/forums/feed-rss-topic2056.xml" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in TLS Handshake exchanging messages.]]></description>
		<lastBuildDate>Fri, 17 Nov 2023 11:28:34 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: TLS Handshake exchanging messages]]></title>
			<link>https://www.wolfssl.com/forums/post7338.html#p7338</link>
			<description><![CDATA[<p>Same behavior, but i have found an ugly fix</p><div class="codebox"><pre><code>        /* Flush */
        memset(stringtosend, 0, sizeof(stringtosend));
        if ((ret = wolfSSL_read(ssl, stringtosend, sizeof(stringtosend)-1)) == -1) {
            fprintf(stderr, &quot;ERROR: failed to read\n&quot;);
            goto done;
        }

        /* Read the server data into our buff array */
        if(stringtosend[0] == 0x0){
            if ((ret = wolfSSL_read(ssl, stringtosend, sizeof(stringtosend)-1)) == -1) {
                fprintf(stderr, &quot;ERROR: failed to read\n&quot;);
                goto done;
            }
        }</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (astc)]]></author>
			<pubDate>Fri, 17 Nov 2023 11:28:34 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post7338.html#p7338</guid>
		</item>
		<item>
			<title><![CDATA[Re: TLS Handshake exchanging messages]]></title>
			<link>https://www.wolfssl.com/forums/post7334.html#p7334</link>
			<description><![CDATA[<p>Hi astc</p><p>What is the server doing? If it is not sending messages, I could see where your test would break.</p><p>I modified our simple examples to do what you are trying to accomplish:<br /><a href="https://github.com/wolfSSL/wolfssl-examples/tree/master/tls">https://github.com/wolfSSL/wolfssl-exam … master/tls</a></p><div class="codebox"><pre><code>diff --git a/tls/client-tls.c b/tls/client-tls.c
index d1e06be..9f13d84 100644
--- a/tls/client-tls.c
+++ b/tls/client-tls.c
@@ -133,32 +133,68 @@ int main(int argc, char** argv)
         goto cleanup;
     }
 
-    /* Get a message for the server from stdin */
-    printf(&quot;Message for server: &quot;);
-    memset(buff, 0, sizeof(buff));
-    if (fgets(buff, sizeof(buff), stdin) == NULL) {
-        fprintf(stderr, &quot;ERROR: failed to get message for server\n&quot;);
-        ret = -1;
-        goto cleanup;
-    }
-    len = strnlen(buff, sizeof(buff));
-
-    /* Send the message to the server */
-    if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
-        fprintf(stderr, &quot;ERROR: failed to write entire message\n&quot;);
-        fprintf(stderr, &quot;%d bytes of %d bytes were sent&quot;, ret, (int) len);
-        goto cleanup;
+#if 1
+    while (1)
+    {
+        int err;
+        char stringtosend[1024];
+        char readBuf[1024];
+
+        printf(&quot;Send a string to the server\n&quot;
+               &quot;x to exit\n&quot;);
+        if (fgets(stringtosend, sizeof(stringtosend), stdin) == NULL) {
+            printf(&quot;error reading&quot;);
+        }
+        do {
+            ret = wolfSSL_write(ssl, stringtosend, sizeof(stringtosend));
+            err = wolfSSL_get_error(ssl, ret);
+        } while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
+        printf(&quot;Sent (%d): %s\n&quot;, err, stringtosend);
+
+        XMEMSET(readBuf, 0, sizeof(readBuf));
+        do {
+            ret = wolfSSL_read(ssl, readBuf, sizeof(readBuf)-1);
+            err = wolfSSL_get_error(ssl, ret);
+        } while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
+        printf(&quot;Read (%d): %s\n&quot;, err, readBuf);
+
+
+        //ssl-&gt;buffers.clearOutputBuffer.length = 0;
+        if (stringtosend[0] == &#039;x&#039; &amp;&amp; stringtosend[1] == &#039;\n&#039;){
+            break;
+        }
     }
-
-    /* Read the server data into our buff array */
-    memset(buff, 0, sizeof(buff));
-    if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) {
-        fprintf(stderr, &quot;ERROR: failed to read\n&quot;);
-        goto cleanup;
-    }
-
-    /* Print to stdout any data the server sends */
-    printf(&quot;Server: %s\n&quot;, buff);
+#else
+    do {
+        /* Get a message for the server from stdin */
+        printf(&quot;Message for server: &quot;);
+        memset(buff, 0, sizeof(buff));
+        if (fgets(buff, sizeof(buff), stdin) == NULL) {
+            fprintf(stderr, &quot;ERROR: failed to get message for server\n&quot;);
+            ret = -1;
+            goto cleanup;
+        }
+        len = strnlen(buff, sizeof(buff));
+
+        /* Send the message to the server */
+        if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
+            fprintf(stderr, &quot;ERROR: failed to write entire message\n&quot;);
+            fprintf(stderr, &quot;%d bytes of %d bytes were sent&quot;, ret, (int) len);
+            goto cleanup;
+        }
+
+        /* Read the server data into our buff array */
+        memset(buff, 0, sizeof(buff));
+        if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) {
+            fprintf(stderr, &quot;ERROR: failed to read\n&quot;);
+            goto cleanup;
+        }
+
+        /* Print to stdout any data the server sends */
+        printf(&quot;Server: %s\n&quot;, buff);
+
+    } while(1);
+#endif
 
     /* Bidirectional shutdown */
     while (wolfSSL_shutdown(ssl) == SSL_SHUTDOWN_NOT_DONE) {</code></pre></div><div class="codebox"><pre><code>diff --git a/tls/server-tls.c b/tls/server-tls.c
index fa79a4d..6fc3c50 100644
--- a/tls/server-tls.c
+++ b/tls/server-tls.c
@@ -160,35 +160,35 @@ int main()
 
         printf(&quot;Client connected successfully\n&quot;);
 
-
-
-        /* Read the client data into our buff array */
-        memset(buff, 0, sizeof(buff));
-        if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) {
-            fprintf(stderr, &quot;ERROR: failed to read\n&quot;);
-            goto exit;
-        }
-
-        /* Print to stdout any data the client sends */
-        printf(&quot;Client: %s\n&quot;, buff);
-
-        /* Check for server shutdown command */
-        if (strncmp(buff, &quot;shutdown&quot;, 8) == 0) {
-            printf(&quot;Shutdown command issued!\n&quot;);
-            shutdown = 1;
-        }
-
-
-
-        /* Write our reply into buff */
-        memset(buff, 0, sizeof(buff));
-        memcpy(buff, reply, strlen(reply));
-        len = strnlen(buff, sizeof(buff));
-
-        /* Reply back to the client */
-        if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
-            fprintf(stderr, &quot;ERROR: failed to write\n&quot;);
-            goto exit;
+        while(!shutdown) {
+            /* Read the client data into our buff array */
+            memset(buff, 0, sizeof(buff));
+            if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) {
+                fprintf(stderr, &quot;ERROR: failed to read\n&quot;);
+                goto exit;
+            }
+
+            /* Print to stdout any data the client sends */
+            printf(&quot;Client: %s\n&quot;, buff);
+
+            /* Check for server shutdown command */
+            if (strncmp(buff, &quot;shutdown&quot;, 8) == 0) {
+                printf(&quot;Shutdown command issued!\n&quot;);
+                shutdown = 1;
+            }
+
+
+
+            /* Write our reply into buff */
+            memset(buff, 0, sizeof(buff));
+            memcpy(buff, reply, strlen(reply));
+            len = strnlen(buff, sizeof(buff));
+
+            /* Reply back to the client */
+            if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
+                fprintf(stderr, &quot;ERROR: failed to write\n&quot;);
+                goto exit;
+            }
         }
 
         /* Notify the client that the connection is ending */</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (embhorn)]]></author>
			<pubDate>Thu, 16 Nov 2023 17:36:21 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post7334.html#p7334</guid>
		</item>
		<item>
			<title><![CDATA[TLS Handshake exchanging messages]]></title>
			<link>https://www.wolfssl.com/forums/post7333.html#p7333</link>
			<description><![CDATA[<p>I am doing&nbsp; an handshake between a client and a server, after the handshake the client sends a message and waits for the server to reply with the same message. The first message always goes through, but the client is never able to read the second one and the ones after, it simply reads 0.</p><p>I found that the first loop <em>ssl-&gt;buffers.clearOutputBuffer.length</em> is equal to zero, but the second time it&#039;s like 900, so wolfSSL_read follows a different procedure, doesn&#039;t read and sets the buffer at zero. If in debug i set <em>ssl-&gt;buffers.clearOutputBuffer.length</em> to zero everything works. So i would like to know how can i do it in code, or what am i doing wrong.</p><div class="codebox"><pre><code>    while (true)
    {
        printf(&quot;Send a string to the server\n&quot;
               &quot;x to exit\n&quot;);
        if (fgets(stringtosend, sizeof(stringtosend), stdin) == NULL) {
            printf(&quot;error reading&quot;);
        }
        do {
            ret = wolfSSL_write(ssl, stringtosend, sizeof(stringtosend));
            err = wolfSSL_get_error(ssl, ret);
        } while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
        printf(&quot;Sent (%d): %s\n&quot;, err, stringtosend);

        XMEMSET(readBuf, 0, sizeof(readBuf));
        do {
            ret = wolfSSL_read(ssl, readBuf, sizeof(readBuf)-1);
            err = wolfSSL_get_error(ssl, ret);
        } while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
        printf(&quot;Read (%d): %s\n&quot;, err, readBuf);
        
        
        //ssl-&gt;buffers.clearOutputBuffer.length = 0;
        if (stringtosend[0] == &#039;x&#039; &amp;&amp; stringtosend[1] == &#039;\n&#039;){
            return;
        }
    }</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (astc)]]></author>
			<pubDate>Thu, 16 Nov 2023 16:23:52 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post7333.html#p7333</guid>
		</item>
	</channel>
</rss>
