Code copy freebsd development manual http://shouce.jb51.net/freebsd-developers-handbook/sockets-essential-functions.html
# include & lt; Stdio. H>
#include
#include
#include
#include
#include
# define BACKLOG 6
Int main (int arg c, char * argv []) {
The register int s_sock_fd c_sock_fd;
Struct sockaddr_in s_addr c_addr;
The FILE * client;
Int port=8080;
Unsigned int ca_len;
If ((s_sock_fd=socket (PF_INET SOCK_STREAM, 0)) & lt; 0 {
Perror (" socket error. \ n ");
return 1;
}
Bzero (& amp; S_addr, sizeof (s_addr));
S_addr. Sin_family=AF_INET;
S_addr. Sin_port=htons (port);
If (INADDR_ANY)
S_addr. Sin_addr. S_addr=htonl (INADDR_ANY);
If (bind (s_sock_fd, (struct sockaddr *) & amp; S_addr, sizeof (s_addr)) & lt; 0 {
Perror (" bind error. \ n ");
Return 2;
}
The switch (the fork ()) {
Case 1:
Perror (" fork error. \ n ");
Return 3;
break;
Default:
Close (s_sock_fd);
return 0;
break;
Case 0:
break;
}
Listen (s_sock_fd, BACKLOG);
For (;; ) {
Ca_len=sizeof (c_addr);
If ((c_sock_fd=accept (s_sock_fd, (struct sockaddr *) & amp; C_addr, & amp; Ca_len) & lt; 0 {
Perror (" accept the error. \ n ");
The return of 4;
}
If ((client=fdopen (c_sock_fd, "w")) & lt; 0 {
Perror (" fdopen error. \ n ");
Return 5;
}
Fprintf (client, "& lt; Html> . .