Skip to content

Commit cb1b6ac

Browse files
committed
Now we seem to have a proper server that correctly closes clients.
1 parent 7bae8b1 commit cb1b6ac

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

ddcd.cpp

+25-13
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ int set_nonblocking(int fd)
5151
return 1;
5252
}
5353

54+
int proc_exists(pid_t pid)
55+
{
56+
if(pid==0 || pid==1) return 1;
57+
return kill(pid, 0) != -1;
58+
}
59+
5460
client_t* this_client;
5561

5662
int main(int argc, char* argv[])
@@ -119,13 +125,12 @@ int main(int argc, char* argv[])
119125
if( listen(listen_socket, 10) == -1 )
120126
error_exit(MSG_START "cannot listen() on socket.\n");
121127

128+
fprintf(stderr,MSG_START "listening on %s:%d\n", inet_ntoa(addr_host.sin_addr), host_port);
129+
122130
struct sockaddr_in addr_cli;
123131
socklen_t addr_cli_len = sizeof(addr_cli);
124132
int new_socket;
125133

126-
//The server will wait on these sockets later...
127-
128-
129134
//Set stdin and listen_socket to non-blocking
130135
if(set_nonblocking(STDIN_FILENO) || set_nonblocking(listen_socket))
131136
error_exit(MSG_START "cannot set_nonblocking().\n");
@@ -200,24 +205,31 @@ int main(int argc, char* argv[])
200205
if(write(clients[i]->pipefd[1], buf, retval)==-1)
201206
{
202207

203-
if(!clients[i]->error) print_client(clients[i], "lost buffer, failed to write pipe");
204-
else clients[i]->error=1;
208+
if(!clients[i]->error)
209+
{
210+
print_client(clients[i], "lost buffer, failed to write pipe.");
211+
clients[i]->error=1;
212+
}
205213
//fprintf(stderr, MSG_START "errno is %d\n", errno); //usually 11
206-
int wpstatus;
207-
int wpresult = waitpid(clients[i]->pid, &wpstatus, WNOHANG);
208-
if(wpresult == -1) print_client(clients[i], "error while waitpid()!");
209-
else if(wpresult == 0)
214+
//int wpstatus;
215+
//int wpresult = waitpid(clients[i]->pid, &wpstatus, WNOHANG);
216+
//fprintf(stderr, MSG_START "pid is %d\n",clients[i]->pid);
217+
//perror("somethings wrong");
218+
//if(wpresult == -1) print_client(clients[i], "error while waitpid()!");
219+
//else if(wpresult == 0)
220+
waitpid(clients[i]->pid, NULL, WNOHANG);
221+
if(!proc_exists(clients[i]->pid))
210222
{
211223
//Client exited!
212224
print_client(clients[i], "closing client from main process.");
213225
close(clients[i]->pipefd[1]);
214226
close(clients[i]->socket);
215227
delete clients[i];
216228
clients.erase(clients.begin()+i);
217-
print_client(clients[i], "okay.");
229+
print_client(clients[i], "done closing client from main process.");
218230
}
219231
}
220-
else clients[i]->error=0;
232+
else { if(clients[i]->error) print_client(clients[i], "pipe okay again."); clients[i]->error=0; }
221233
}
222234
}
223235
//TODO: at the end, server closes pipefd[1] for client
@@ -229,7 +241,7 @@ int main(int argc, char* argv[])
229241

230242
void print_client(client_t* client, const char* what)
231243
{
232-
fprintf(stderr,MSG_START " (client %s:%d) %s\n", inet_ntoa(client->addr.sin_addr), client->addr.sin_port, what);
244+
fprintf(stderr,MSG_START "(client %s:%d) %s\n", inet_ntoa(client->addr.sin_addr), client->addr.sin_port, what);
233245
}
234246

235247
void client_cleanup()
@@ -245,7 +257,7 @@ void client()
245257
read(this_client->pipefd[0],buf,bufsizeall);
246258
if(send(this_client->socket,buf,bufsizeall,0)==-1)
247259
{
248-
print_client(this_client, "closing.");
260+
print_client(this_client, "client process is exiting.");
249261
exit(0);
250262
}
251263
}

ddcd.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ typedef struct client_s
2929
void client();
3030
void error_exit(const char* why);
3131
void print_client(client_t* client, const char* what);
32+
int proc_exists(pid_t pid);

0 commit comments

Comments
 (0)