Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

TCP concurrent web server for the introduction of network hackers

2025-02-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

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

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report