これはオペレーティング システム クラスの宿題であり、私はプログラマーではなく、特に C ではありません。この作業を始めて 1 週間が経ちましたが、行き詰まっており、助けが必要です。Linuxコマンドがクライアントに入力され、サーバーで実行され、出力がクライアントにリダイレクトされるTCPクライアントおよびサーバーアプリケーションを作成する必要があります。私はその概念を理解しており、90% 以上は機能しています。「ls」、「ls -lpq」、「cat somefile」、「man somecommand」などのコマンドはすべて正常に機能します。問題が発生するのは、「mkdir newdir」などの情報を返さないコマンドです (ディレクトリが既に存在する場合は、応答が返されるため正常に動作します)。これは私にはまったく新しいことですが、受信する情報がないため、サーバーの recv コマンドがブロックされていることが問題のようです。これを乗り越える方法がわかりません。この 1 つの問題に 1 週間取り組んできました。私は時間がなくなっており、ファイルのアップロードとダウンロードも実装する必要があります。どこから始めればよいかわかりませんが、この問題を解決するまで作業を開始することさえできません。
ありがとう
// this is where I think the problem is
while ((rcvd_bytes = recv(sock_fd, recv_str, sizeof(recv_str), 0)) > 0 ) {
// Zero out the inputCopy buffer
memset(&inputCopy, 0, sizeof(inputCopy)+1);
// Copy the recv_str into a new string so that
// we can work with it.
strcpy(inputCopy, recv_str);
argcount = parsecommand(inputCopy, args);
//Send the message back to client
dup2(sock_fd, 1);
if((status = forkAndExecute(args)) != 0) {
//printf("Command %s returned %d.\n", args[0], status);
}
// as a test is I uncomment the following line mkdir newdir
// returns but the following commands are messed up - as I expect.
//printf("\n");
memset(&recv_str, 0, sizeof(recv_str)+1);
fflush(stdout);
}
if(rcvd_bytes == 0) {
puts("Client disconnected");
fflush(stdout);
}
else if(rcvd_bytes == -1) {
perror("recv failed");
exit(0);
}