Home > OS >  C language implementation of HTTP client browser would not get sent to the data
C language implementation of HTTP client browser would not get sent to the data

Time:11-17


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* the File Name: ServerHtml. C
* Created By: chroot
* Creation Date: 08:43 [2020-11-16]
* the Last Modified: 09:51 [2020-11-16]
* Description: the web server for c
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include
#include
#include
#include
#include
#include
#include
# define PROT 2000
Int create_listenfd (void);
Void headle_request (int fd);
Int main ()
{
//1, to create a listening socket
Int sockfd=1, fd=1;
Struct sockaddr_in server_clie;
Bzero (& amp; Server_clie, sizeof (server_clie));
Socklen_t clie_len=sizeof (server_clie);
Sockfd=create_listenfd ();
Printf (" SOCKFD==& gt; % d \ n ", sockfd);
If (sockfd==1)
{
Puts (" socket binding failed ");
While (sockfd!=1)
{
Puts (" to bind socket ");
Sockfd=create_listenfd ();
}
}

//2 receive client connection
While (fd==1) {
Puts (" waiting connection... ");
Fd=accept (sockfd, (struct sockaddr *) & amp; Server_clie, & amp; Clie_len);
}
Perror (" Accept ");
Puts (" add a user ");
//3. Handle requests
Headle_request (fd);
//3. Handle the client request
Close (fd);
Close (sockfd);
}
Int create_listenfd (void)
{
Int fd=socket (AF_INET SOCK_STREAM, 0), rest;
Struct sockaddr_in sin;
Bzero (& amp; Sin, sizeof (sin));
Sin. Sin_family=AF_INET;
Sin. Sin_port=htons (PROT);
Sin. The sin_addr. S_addr=INADDR_ANY;
For (int prot=prot. (rest=bind (fd, (struct sockaddr *) & amp; Sin, sizeof (sin)))==1; Prot++)
{
Sin. Sin_port=htons (prot);
}
Perror (" Bind ");

Listen (fd, 100); Perror (" listen ");
Return fd.
}
Void headle_request (int fd)
{
Char buffer [BUFSIZ];
Int nred=read (fd, buffer, sizeof (buffer));
Printf (" the client data==& gt; \ n \ n % s ", buffer);
}
  • Related