title: Why I can't read from and write to socket with printf and scanf?
body: <p>I wonder why this code is not working. I mean: When both the server and client are running I can type and type with no result. However, when I kill the client, the server starts outputting everything I previously wrote to the client app.</p>
  <p>full server.c: <a href="https://pastebin.com/iXSYLZS3" rel="nofollow noreferrer">https://pastebin.com/iXSYLZS3</a></p>
  <pre><code>#include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netinet/in.h&gt;
  
  #define PORT 8080
  
  int main(void)
  {
          // some code to initialize connection
  
          printf(&quot;connection accepted!\n&quot;);
          fflush(stdout);
  
          close(0);
          dup(sock);
          while (1) {
                  scanf(&quot;%s&quot;, str);
                  printf(&quot;%s&quot;, str);
                  fflush(stdout);
          }
  
          return(0);
  }
  </code></pre>
  <p>full client.c: <a href="https://pastebin.com/sFmi72ZP" rel="nofollow noreferrer">https://pastebin.com/sFmi72ZP</a></p>
  <pre><code>#include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;arpa/inet.h&gt;
   
  #define PORT 8080
   
  int main(void)
  {
          // some code to initialize connection
   
          printf(&quot;connected!\n&quot;);
          fflush(stdout);
   
          close(1);
          dup(sock);
          while (1) {
                  scanf(&quot;%s&quot;, str);
                  printf(&quot;%s&quot;, str);
                  fflush(stdout);
          }
   
          return(0);
  }
  </code></pre>