私は小さなクライアント/サーバープログラムを持っており、最大タイムアウトが10秒後にクライアントへの接続を閉じたいとします。私はアラームを持っているし、接続を閉じ機能、アラームハンドラは次のようになります。Cクライアント/サーバープログラムのタイムアウトの処理
void closeClient() {
int nr = close(conn);
if (nr == 0)
printf("Client connection closed.\n");
else {
printf("Error while closing client connection. Error code: %d\n",errno);
exit(1);
}
exit(0); // Process ends after serving the client
}
void time_out(int signal) {
printf("Time out.\n");
char* msg = "Time out.Connection to server is closed\n\0";
send(conn, msg, strlen(msg),0);
closeClient(conn);
exit(1);
}
問題は、クライアントが(「閉じたサーバーへのタイムout.Connection」)メッセージを出力していることである場合にのみ、接続が既に閉じられた後にサーバーに何かを送信しようとします。なぜ私は理解できません。いくつかの提案?
time_out関数が呼び出されるコードは決して表示されません。 – YeenFei
これは次のようなものです: 'code'シグナル(SIGALRM、time_out); アラーム(10); cod = recv(conn、&buf、sizeof(buf)、0); ブール値isNumber = validateNumber(buf); printf( "受け取った文字列:%s \ n"、buf); アラーム(0); //タイマを停止する if(!isNumber){//サーバが有効な整数を受け取らなかった errorMsg = "有効な整数を入力してください!\ n \ 0"; printf( "送信中のエラーメッセージ.. \ n"); send(conn、errorMsg、strlen(errorMsg)+1、0); } else .. 'code' – joanna