Get the App
SLTechnology News&Howtos  ›  Network Security  › 

TCP concurrent web server for the introduction of network hackers

Shulou Source: shulou.com Published: 2022-06-01 02:52:36 10月02日 Update

TCP concurrent web server for the introduction of network hackers

The TCP concurrent server was originally intended to be written after TCP programming for getting started with network hackers, but because the code was a bit long, I wrote a separate article

Note:

Because the browser sends a lot of data, the recv_buf as the acceptance buffer of this server should be larger, at least 512 bytes, and 1024 bytes is recommended

If the reception is not complete, under no circumstances can you upload the web page to the browser. This bug stuck me all night, so I remember it very clearly.

The parameters passed to the thread when creating the thread pay attention to the value of the write connection socket, which is first converted to type (void*)

Pthread_create (& pth,NULL,msg_echo, (void *) connfd)

And then switch back to the int type in the thread

Int connfd = (int) arg

Cannot write an address to prevent the value of connfd from changing too fast when there are multiple requests at the same time, before the subthread takes the address out.

1. Header file

# include

# include

# include

# include

# include

# include

# include

# include

# include

/ / the child thread processes the browser's web page request

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 200 OK\ r\ n"\

"Content-Type: text/html\ r\ n"\

"\ r\ n"

/ / request failed and returned

Char err [] = "HTTP/1.1 404 Not Found\ r\ n"\

"Content-Type: text/html\ r\ n"\

"\ r\ n"\

"File not found"

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 the web page file name

Sscanf (recv_buf+4, "% [^]", (filename + 5))

Printf ("filename:%s\ n", filename)

/ / Open the web file

Fd = open (filename, O_RDONLY)

If (fd

< 0) { perror("open"); send(connfd, err, strlen(err), 0); close(connfd); return NULL; } //将网页文件发给浏览器 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;// setting Port

If (argc > 1) / / Ports can be specified

{

Port = atoi (argv [1])

}

Int sockfd

Struct sockaddr_in my_addr

/ / structure

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)

/ / socket

Sockfd = socket (AF_INET, SOCK_STREAM, 0)

If (sockfd < 0)

{

Perror ("socket")

Exit (- 1)

}

/ / bind 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 processes 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 the 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))

/ / create thread

Pthread_t pth

Pthread_create (& pth,NULL,msg_echo, (void *) connfd)

Pthread_detach (pth)

}

Close (sockfd)

Return 0

}

Tags: Threads web pages files browsers ports browsing servers services sockets networks hackers addresses bytes data processing clarity success anyway code information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi OPPO Reno Shulou Information Docker vpn