Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Some comments in no particular order:</p> <ul> <li><p>You're passing up the opportunity to know exact errors too often:</p> <pre><code>if(listen(sockfd,BACKLOG) == -1) { printf("ERROR: Failed to listen Port %d.\n", PORT); return (0); } </code></pre> <p>This block should definitely include a <code>perror("listen")</code> or something similar. Always include <code>perror()</code> or <code>strerror()</code> in every error handling block when the error details will be reported via <code>errno</code>. Having exact failure reasons will save you hours when programming and will save you and your users hours when things don't work as expected in the future.</p></li> <li><p>Your error handling needs some further standardizing:</p> <pre><code>if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) { printf("ERROR: Failed to obtain Socket Descriptor.\n"); return (0); } </code></pre> <p>This should <em>not</em> <code>return 0</code> because that will signal to the shell that the program ran to completion without error. You should <code>return 1</code> (or use <code>EXIT_SUCCESS</code> and <code>EXIT_FAILURE</code>) to signal an abnormal exit.</p> <pre><code> else printf("[Server] Server has got connected from %s.\n", inet_ntoa(addr_remote.sin_addr)); /*Receive File from Client */ </code></pre> <p>In this preceding block you've gotten an error condition but continue executing anyway. That's a quick way to get very undesirable behavior. This should either re-start the main server loop or exit the child process or something similar. (Depends if you keep the multi-process server.)</p> <pre><code>if(!fork()) { </code></pre> <p>The preceding block forgot to account for <code>fork()</code> <em>failing</em>. <code>fork()</code> can, and does fail -- especially in shared hosting environments common at universities -- so you should be prepared for the full, complicated <em>three</em> possible return values from <code>fork()</code>: failure, child, parent.</p></li> <li><p>It appears you're using <code>fork()</code> indiscriminately; your client and server are both very simple and the way they are designed to run means they <em>cannot</em> be used to service multiple clients simultaneously. You should probably stick to exactly one process for each, at least until the algorithm is perfectly debugged and you figure out some way to run multiple clients simultaneously. I expect this is the source of the problem you're encountering now.</p></li> <li><p>You need to use functions to encapsulate details; write a function to connect to the server, a function to send the file, a function to write the file, etc. Write a function to handle the complicated partial writes. (I especially recommend stealing the <code>writen</code> function from the <a href="http://www.apuebook.com/" rel="noreferrer">Advanced Programming in the Unix Environment</a> book's source code. File <code>lib/writen.c</code>.) If you write the functions correctly you can re-use them in both the client and server. (Something like placing them in <code>utils.c</code> and compiling the programs like <code>gcc -o server server.c utils.c</code>.)</p> <p>Having smaller functions that each do one thing will allow you to focus on smaller amounts of code at a time <em>and</em> write little tests for each that will help you narrow down which sections of code still need improvement.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload