0

asdfg askjhgkasdhj gfskjd;hdgfk ;jasgdjkf hasdjkghf asdjkf hasdjkfgh klasdhdf jkasdjkf hdjksahf jklhdjkf asjkdhdf jkdlf ajsdhf jklashdf jaksdf hkjlasdjklf hasdhdf kdf kjlasdhdf hkjadf ljkdf adf kjahdkf adf kjashdfhkjlsasdf

 else {
        printf("[-]Invalid command\n");
      }
    }

    bzero(buffer, SIZE);
  }

}
  • 1
    You didn't bind to the port on the client end so it just picks a random port. This is expected (and common for most client apps). – jmq May 27 '20 at 13:13
  • So it's ok then? Would it be better if i bind my port on the client? But since it's concurrent program wouldn't that be an issue? –  May 27 '20 at 13:29
  • 2
    If would be better not to bind. The server doesn't care what port originated the request. Binding to a specific port on the client side would also mean that only one client could run on that machine at a time. – jmq May 27 '20 at 13:47
  • Ok great then. Is my use of the Fork function correct? As that's what concerned me most. –  May 27 '20 at 14:32
  • 1
    Your fork looks slightly wrong. You close sockfd. This will prevent the server from accepting any more connections. Also, does quit actually quit? It looks like the while loop will run forever. – jmq May 27 '20 at 15:07
  • 1
    Missed the break statement, so quit will break out of the loop, but you should close the socket on both ends. – jmq May 27 '20 at 15:18
  • Ah ok i see, thank you so much for taking the time to help! If you want post what you said as an answer. –  May 27 '20 at 16:10
  • in the server: regarding: `if ( send( sockfd, data, sizeof(data), 0 ) < 0 )` You really only want to send the number of bytes input in the call to `fgets()`, which (probably) is less than the size of the buffer – user3629249 May 27 '20 at 19:17
  • in the server: regarding: `if ( send( sockfd, data, sizeof(data), 0 ) < 0 )` the function: `send()` returns the number of bytes transmitted, which may be less than the number requested (and even 0) Suggest: `ssize_t bytesSent; bytesSent = send( sockfd, data, sizeof(data), 0 ); if( bytesSent == 0 ) { then client closed connection } else if( bytesSent <0) { then some error occurred } else if( bytesSent < sizeof(data) ) { then need to loop, to send rest of data } – user3629249 May 27 '20 at 19:23
  • OT: regarding: `fp = fopen(filepath, "r");` always check (!=NULL) the returned value to assure the operation was successful. If not successful (==NULL) then call `perror( "fopen failed" );` to send both that error message and the text reason the system thinks the error occurred to `stderr`. Note: this is not recoverable, so clean up and call: `exit( EXIT_FAILURE );` – user3629249 May 27 '20 at 19:26
  • in the server: regarding: `perror("[-]Error in sending file."); exit(1);` between `perror()` and `exit()` should be closing files, closing `new_sock` closing and if going to exit the program and not just that one connection, then close `sockfd` – user3629249 May 27 '20 at 19:32
  • in the server: regarding: `if (new_sock < 0){ perror("[-]Error in accpet"); exit(1); }` do you really want to exit the program or just drop that one connection – user3629249 May 27 '20 at 19:34

1 Answers1

0

You have a couple of issues:

-Don't close sockfd, this will prevent further connections from being accepted on the server
-The socket should be closed on both ends in the quit case
-The fact that the client side uses a random port is fine.  Nothing needs to be changed there
jmq
  • 1,559
  • 9
  • 21