Network Hacker introduction of TCP Concurrent Web server
The TCP concurrent server was supposed to be written behind the network hacker's introduction to TCP programming, but because the code was a bit long, it wrote a separate
Attention:
Because the browser sends more data, so the accept buffer as this server recv_buf to a larger point, at least 512 bytes, 1024 bytes recommended
If the reception is not complete, in any case can not pass the Web page to the browser, this bug card I one night, so remember especially clear.
The parameters that are passed to the thread when the thread is created note the value of the write connection socket, first converted to the (void*) type,
Pthread_create (&pth,null,msg_echo, (void *) CONNFD);
Then back in the thread (int) type
int CONNFD = (int) arg;
Cannot write an address to prevent a CONNFD value from becoming too fast if there are multiple requests at the same time, and the value changes before the child thread takes the address out.
1. Header files
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <fcntl.h>
Child Threading Web page request for browser processing
void *msg_echo (void *arg)
{
int CONNFD = (int) arg;
int ret_read=0;
int ret_recv=0;
Char recv_buf[1024]= "";
Char read_buf[1024]= "";
Char send_buf[1024]= "";
int FD;
Char filename[50] = "html/";
Request returned successfully
Char head[]= "http/1.1 ok\r\n"\
"content-type:text/html\r\n"\
"\ r \ n";
Request failed to return
Char err[]="http/1.1 404 Not found\r\n"\
"content-type:text/html\r\n"\
"\ r\ n" \
"
printf ("connfd=%d\n", CONNFD);
Receive request data
RET_RECV = recv (connfd,recv_buf,sizeof (RECV_BUF), 0);
printf ("ret_recv:%d\n", RET_RECV);
Read Page file name
SSCANF (Recv_buf+4, "%[^]", (filename + 5));
printf ("filename:%s\n", filename);
Open a Web page file
FD = open (filename, o_rdonly);
if (FD < 0)
{
Perror ("open");
Send (CONNFD, err, strlen (err), 0);
Close (CONNFD);
return NULL;
}
Send a Web page file to the browser
Send (Connfd,head,strlen (head), 0);
while ((Ret_read = Read (fd,read_buf,sizeof (READ_BUF))) >0)
{
printf ("%s\n", read_buf);
Send (connfd,read_buf,ret_read,0);
}
Close (CONNFD);
Close (FD);
}
2.main function
int main (int argc, char *argv[])
{
unsigned int port=8000;//set port
if (argc > 1)//can specify port
{
Port = atoi (argv[1]);
}
int sockfd;
struct sockaddr_in my_addr;
Structural body
memset (&my_addr,0,sizeof (MY_ADDR));
my_addr.sin_family = af_inet;
My_addr.sin_port = htons (port);
MY_ADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);
Sockets
SOCKFD = socket (af_inet, sock_stream, 0);
if (SOCKFD < 0)
{
Perror ("socket");
Exit (-1);
}
Binding port
int err = bind (SOCKFD, (struct sockaddr*) &my_addr,sizeof (MY_ADDR));
if (err! = 0)
{
Perror ("bind");
Close (SOCKFD);
Exit (-1);
}
Listening port
Err = Listen (sockfd,10);
if (err! = 0)
{
Perror ("Listen");
Close (SOCKFD);
Exit (-1);
}
printf ("Listen at%d\n", port);
Multithreading Processing connection Requests
while (1)
{
int connfd;//Connection Socket
struct sockaddr_in client_addr;
Char cli_ip[inet_addrstrlen]= "";
socklen_t Cliaddr_len = sizeof (CLIENT_ADDR);
Accept Request
CONNFD = Accept (SOCKFD, (struct sockaddr *) &client_addr,&cliaddr_len);
if (CONNFD < 0)
{
Perror ("accept");
}
Output connector Information
Inet_ntop (Af_inet,&client_addr.sin_addr,cli_ip,inet_addrstrlen);
printf ("accepted--ip:%s port:%d\n", Cli_ip,ntohs (Client_addr.sin_port));
Creating Threads
pthread_t PTH;
Pthread_create (&pth,null,msg_echo, (void *) CONNFD);
Pthread_detach (PTH);
}
Close (SOCKFD);
return 0;
}
Network Hacker introduction of TCP Concurrent Web server