I've written a server that when a the browser tries to connect to certain sites it checks a blacklist and sends back a 404, however when I call send() there is no error but the message does not appear on the web browser unless I close the connection?
Any advice?
Accepting connection from browser
while(1){
connfd = accept(fd, (struct sockaddr *) &cliaddr, &cliaddrlen);
if (connfd == -1) {
perror ("unable to accept");
return 1;
}
printf("%s:%d connected\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
printf("%d",threadIndex);
pthread_create(&(thread[threadIndex++]), NULL, processRequests, (void *)connfd);
}
Process Requests method send snippet:
if(blacklisted ==1){
printf("is blacklisted\n");
char *response404 = "HTTP:/1.1 404 not avaliable\r\n\r\n";
printf("%s\n",response404);
int len, bytes_sent;
len = strlen(response404);
bytes_sent = send(connfd, response404, len, 0);
if(len != bytes_sent){
perror("message length doesn't match");
}
}
irather thanj.connfd? It looks like in your code they're all sharing the same variable.