structsockaddr_in { short sin_family; // e.g. AF_INET unsignedshort sin_port; // e.g. htons(3490) structin_addrsin_addr;// see struct in_addr, below char sin_zero[8]; // zero this if you want to };
structin_addr { unsignedlong s_addr; // load with inet_aton() };
这些是所有处理网络地址的系统调用和函数的基本结构。在内存中,struct sockaddr_in和struct sockaddr的size是一样的,可以自由地在二者间转换指针(cast the pointer),且不造成任何损失,除非碰上可能的宇宙终结(except the possible end of the universe)
// you can specify an IP address: inet_aton("63.161.169.137", &myaddr.sin_addr.s_addr);
// or you can let it automatically select one: myaddr.sin_addr.s_addr = INADDR_ANY;
s = socket(PF_INET, SOCK_STREAM, 0); bind(s, (struct sockaddr*)myaddr, sizeof(myaddr));
getsockopt、setsockopt
get and set option on sockets
1 2 3 4 5
#include<sys/types.h>/* See NOTES */ #include<sys/socket.h> intgetsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen);intsetsockopt(int sockfd, int level, int optname, constvoid *optval, socklen_t optlen);
structstat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* Inode number */ mode_t st_mode; /* File type and mode */ nlink_t st_nlink; /* Number of hard links */ uid_t st_uid; /* User ID of owner */ gid_t st_gid; /* Group ID of owner */ dev_t st_rdev; /* Device ID (if special file) */ off_t st_size; /* Total size, in bytes */ blksize_t st_blksize; /* Block size for filesystem I/O */ blkcnt_t st_blocks; /* Number of 512B blocks allocated */
/* Since Linux 2.6, the kernel supports nanosecond precision for the following timestamp fields. For the details before Linux 2.6, see NOTES. */
structtimespecst_atim;/* Time of last access */ structtimespecst_mtim;/* Time of last modification */ structtimespecst_ctim;/* Time of last status change */
Parameters : fd[0] will be the fd(file descriptor)for the read end of pipe. fd[1] will be the fd for the write end of pipe. Returns : 0 on Success. -1 on error.
/* J. David's webserver */ /* This is a simple webserver. * Created November 1999 by J. David Blackstone. * CSE 4344 (Network concepts), Prof. Zeigler * University of Texas at Arlington */ /* This program compiles for Sparc Solaris 2.6. * To compile for Linux: * 1) Comment out the #include <pthread.h> line. * 2) Comment out the line that defines the variable newthread. * 3) Comment out the two lines that run pthread_create(). * 4) Uncomment the line that runs accept_request(). * 5) Remove -lsocket from the Makefile. */ #include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<arpa/inet.h> #include<unistd.h> #include<ctype.h> #include<strings.h> #include<string.h> #include<sys/stat.h> // #include <pthread.h> #include<sys/wait.h> #include<stdlib.h> #include<stdint.h>
/**********************************************************************/ /* This function starts the process of listening for web connections * on a specified port. If the port is 0, then dynamically allocate a * port and modify the original port variable to reflect the actual * port. * Parameters: pointer to variable containing the port to connect on * Returns: the socket */ /**********************************************************************/ intstartup(u_short *port) { int httpd = 0; int on = 1; structsockaddr_inname;
httpd = socket(PF_INET, SOCK_STREAM, 0); if (httpd == -1) error_die("socket"); memset(&name, 0, sizeof(name)); name.sin_family = AF_INET; name.sin_port = htons(*port); name.sin_addr.s_addr = htonl(INADDR_ANY); if ((setsockopt(httpd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) < 0) { error_die("setsockopt failed"); } if (bind(httpd, (struct sockaddr *)&name, sizeof(name)) < 0) error_die("bind"); if (*port == 0) /* if dynamically allocating a port */ { socklen_t namelen = sizeof(name); if (getsockname(httpd, (struct sockaddr *)&name, &namelen) == -1) error_die("getsockname"); *port = ntohs(name.sin_port); } if (listen(httpd, 5) < 0) error_die("listen"); return(httpd); }
/**********************************************************************/ /* A request has caused a call to accept() on the server port to * return. Process the request appropriately. * Parameters: the socket connected to the client */ /**********************************************************************/ void *accept_request(void* tclient) { int client = *(int *)tclient; char buf[1024]; size_t numchars; char method[255]; char url[255]; char path[512]; size_t i, j; structstatst; int cgi = 0; /* becomes true if server decides this is a CGI * program */ char *query_string = NULL;
numchars = get_line(client, buf, sizeof(buf)); i = 0; j = 0; while (!ISspace(buf[i]) && (i < sizeof(method) - 1)) { method[i] = buf[i]; i++; } j=i; method[i] = '\0';
if (strcasecmp(method, "GET") && strcasecmp(method, "POST")) { unimplemented(client); return; }
if (strcasecmp(method, "POST") == 0) cgi = 1;
i = 0; while (ISspace(buf[j]) && (j < numchars)) j++; while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < numchars)) { url[i] = buf[j]; i++; j++; } url[i] = '\0';
/**********************************************************************/ /* Get a line from a socket, whether the line ends in a newline, * carriage return, or a CRLF combination. Terminates the string read * with a null character. If no newline indicator is found before the * end of the buffer, the string is terminated with a null. If any of * the above three line terminators is read, the last character of the * string will be a linefeed and the string will be terminated with a * null character. * Parameters: the socket descriptor * the buffer to save the data in * the size of the buffer * Returns: the number of bytes stored (excluding null) */ /**********************************************************************/ intget_line(int sock, char *buf, int size) { int i = 0; char c = '\0'; int n;
while ((i < size - 1) && (c != '\n')) { n = recv(sock, &c, 1, 0); /* DEBUG printf("%02X\n", c); */ if (n > 0) { if (c == '\r') { n = recv(sock, &c, 1, MSG_PEEK); /* DEBUG printf("%02X\n", c); */ if ((n > 0) && (c == '\n')) recv(sock, &c, 1, 0); else c = '\n'; } buf[i] = c; i++; } else c = '\n'; } buf[i] = '\0';
/**********************************************************************/ /* Send a regular file to the client. Use headers, and report * errors to client if they occur. * Parameters: a pointer to a file structure produced from the socket * file descriptor * the name of the file to serve */ /**********************************************************************/ voidserve_file(int client, constchar *filename) { FILE *resource = NULL; int numchars = 1; char buf[1024];
/**********************************************************************/ /* Return the informational HTTP headers about a file. */ /* Parameters: the socket to print the headers on * the name of the file */ /**********************************************************************/ voidheaders(int client, constchar *filename) { char buf[1024]; (void)filename; /* could use filename to determine file type */
/**********************************************************************/ /* Put the entire contents of a file out on a socket. This function * is named after the UNIX "cat" command, because it might have been * easier just to do something like pipe, fork, and exec("cat"). * Parameters: the client socket descriptor * FILE pointer for the file to cat */ /**********************************************************************/ voidcat(int client, FILE *resource) { char buf[1024];
/**********************************************************************/ /* Execute a CGI script. Will need to set environment variables as * appropriate. * Parameters: client socket descriptor * path to the CGI script */ /**********************************************************************/ voidexecute_cgi(int client, constchar *path, constchar *method, constchar *query_string) { char buf[1024]; int cgi_output[2]; int cgi_input[2]; pid_t pid; int status; int i; char c; int numchars = 1; int content_length = -1;
/**********************************************************************/ /* Print out an error message with perror() (for system errors; based * on value of errno, which indicates system call errors) and exit the * program indicating an error. */ /**********************************************************************/ voiderror_die(constchar *sc) { perror(sc); exit(1); }
cannot_execute
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/**********************************************************************/ /* Inform the client that a CGI script could not be executed. * Parameter: the client socket descriptor. */ /**********************************************************************/ voidcannot_execute(int client) { char buf[1024];
/**********************************************************************/ /* Inform the client that the requested web method has not been * implemented. * Parameter: the client socket */ /**********************************************************************/ voidunimplemented(int client) { char buf[1024];