我正在构建自己的Web服务器。现在,我的简约代码是:
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#define SERVER_PORT 80
int main () {
int nReqSocketId, nReqSize = 1024, nMainSocketId = socket(AF_INET, SOCK_STREAM, 0);
char *sRequest = malloc(nReqSize);
socklen_t nAddrLen;
struct sockaddr_in oAddress;
oAddress.sin_family = AF_INET;
oAddress.sin_addr.s_addr = INADDR_ANY;
oAddress.sin_port = htons(SERVER_PORT);
if (nMainSocketId == 0) {
fprintf(stderr, "Error during the creation of the socket\n");
return 1;
}
if (bind(nMainSocketId, (struct sockaddr *) &oAddress, sizeof(oAddress))) {
fprintf(stderr, "The port %d is busy\n", SERVER_PORT);
close(nMainSocketId);
return 1;
}
printf("HTTP server listening on port %d\n", SERVER_PORT);
while (1) {
if (listen(nMainSocketId, 10) < 0) {
perror("server: listen");
close(nMainSocketId);
exit(1);
}
nReqSocketId = accept(nMainSocketId, (struct sockaddr *) &oAddress, &nAddrLen);
if (nReqSocketId < 0) {
perror("server: accept");
close(nMainSocketId);
exit(1);
}
recv(nReqSocketId, sRequest, nReqSize, 0);
if (nReqSocketId > 0){
printf("The Client is connected...\n\n%s\n", sRequest);
}
write(nReqSocketId, "HTTP/1.1 200 OK\n", 16);
write(nReqSocketId, "Content-length: 50\n", 19);
write(nReqSocketId, "Content-Type: text/html\n\n", 25);
write(nReqSocketId, "<html><body><h1>Hello world!!!</h1></body></html>\n", 50);
close(nReqSocketId);
}
printf("Goodbye!\n");
close(nMainSocketId);
return 0;
}
我可以创建一个“软关闭机制”,使网络服务器打印“再见!”短语位于无限循环之后?例如,当我键入“ q”字母时…
为什么不消除所有这些写函数,而只使用一个send()?
您需要做的就是将响应存储在缓冲区中,然后发送缓冲区:
// Global #define MAX 2048 char response[MAX]; // No need for char* // In Main memset(response, 0, MAX); // **EDIT** strcpy(response, "HTTP/1.1 200 OK\n"); strcat(response, "Content-length: 50\n"); strcat(response, "Content-Type: text/html\n\n"); strcat(response, "<html><body><h1>Hello world!!!</h1></body></html>\n"); // Now simply send the whole response in one go: send(nReqSocketId, response, strlen(response), 0);
此外,您也可以像这样简单地使其成为非持久连接:
// Global #define MAX 2048 char response[MAX]; // No need for char* // In Main memset(response, 0, MAX); // **EDIT** strcpy(response, "HTTP/1.1 200 OK\n"); strcat(response, "Content-Type: text/html\n\n"); strcat(response, "<html><body><h1>Hello world!!!</h1></body></html>\n"); // Now simply send the whole response in one go again: send(nReqSocketId, response, strlen(response), 0); // Shutdown the socket so it cannot write anymore: shutdown(nReqSocketId,1); // Then totally close it when you are ready: close(nReqSocketId);
后者可能更适合您当前正在执行的操作;因为无论如何您都不会在网络服务器中保持多个连接处于活动状态。
一旦关闭服务器端的连接,客户端(即Web浏览器)就会知道不再期望内容,并且可以正确完成工作。
希望这会有所帮助。
干杯!
PS-
当然,这是对您在此线程中最后一个问题的回答,而不是软终止部分。
我还必须建议您使用memset(response,0,MAX),以便每次响应时都有一个干净的状态。
在* IXish操作系统下,信号将唤醒某些阻塞的系统调用并使它们返回。
它们将指示错误,并将errno
设置为EINTR
。您可以使用这种机制来正常关闭服务器。
请在下面略作调整/更正的答案中查看代码,]:>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#define SERVER_PORT 8080 /* Using a port>1024 one does not need to run this as root. */
void onInt()
{
/* Do nothing. */
}
void onQuit()
{
/* Do nothing. */
}
int main()
{
int nMainSocketId = -1, nReqSocketId = -1;
size_t nReqSize = 1024;
char * sRequest = malloc(nReqSize); /* TODO: add error checking */
socklen_t nAddrLen;
struct sockaddr_in oAddress = {0};
signal(SIGINT, onInt); /* TODO: add error checking */
signal(SIGQUIT, onQuit); /* TODO: add error checking */
printf("\n W E L C O M E!\n\nPress CTRL-C or CTRL-\\ to quit.\n");
oAddress.sin_family = AF_INET;
oAddress.sin_addr.s_addr = INADDR_ANY;
oAddress.sin_port = htons(SERVER_PORT);
nMainSocketId = socket(AF_INET, SOCK_STREAM, 0);
if (nMainSocketId < 0) /* 0 is a valid file/socket descriptor! */
{
perror("server: socket() failed");
return 1;
}
if (bind(nMainSocketId, (struct sockaddr *) &oAddress, sizeof(oAddress)))
{
perror("server: bind() failed");
close(nMainSocketId);
return 1;
}
printf("HTTP server listening on port %d\n", SERVER_PORT);
while (1)
{
int result = 0;
if (listen(nMainSocketId, 10) < 0)
{
perror("server: listen() failed");
close(nMainSocketId);
return 1;
}
result = accept(nMainSocketId, (struct sockaddr *) &oAddress, &nAddrLen);
if (result < 0)
{
if (EINTR == errno)
{
printf("Shutdown requested. Exiting ...\n");
break;
}
perror("server: accept failed()");
close(nMainSocketId);
return 1;
}
nReqSocketId = result;
result = recv(nReqSocketId, sRequest, nReqSize, 0); /* TODO: Check wether the request size was really read! */
if (result < 0)
{
perror("server: recv() failed");
close(nMainSocketId);
return 1;
}
else if (result == 0)
{
printf("The client is disconnected. Waiting for another connection ...\n");
close(nReqSocketId);
nReqSocketId = -1;
continue;
}
printf("The client is connected...\n\n%s\n", sRequest);
/* TODO: add error checking for ALL write()s */
write(nReqSocketId, "HTTP/1.1 200 OK\n", 16);
write(nReqSocketId, "Content-length: 50\n", 19);
write(nReqSocketId, "Content-Type: text/html\n\n", 25);
write(nReqSocketId, "<html><body><h1>Hello world!!!</h1></body></html>\n", 50);
close(nReqSocketId);
nReqSocketId = -1;
}
printf("Goodbye!\n");
close(nMainSocketId);
return 0;
}
[请注意,对recv()
的调用也可能会阻止等待数据,然后由于SIGINT
而中断。因此,应将与检查accept()
的结果相同的逻辑应用于recv()
。
@@ PandaSobao
我使用fprintf()和文件描述符一起删除了write(),我认为这是最好的方法,因为不需要strlen()...请看下面的代码:
#include <stdio.h>
#include <netinet/in.h>
#include <signal.h>
#define REQUEST_SIZE 1024
#define SERVER_PORT 80
int bExiting, nMainSocketId;
void terminateServer () {
if (bExiting) { return; }
printf("\n\nTerminating server...\n");
close(nMainSocketId);
bExiting = 1;
}
int main () {
int nRecvResult = -1, nReqSocketId = -1;
char sRequest[REQUEST_SIZE];
socklen_t nAddrLen;
struct sockaddr_in oAddress;
FILE *nRespFD;
printf("\n W E L C O M E!\n\nPress CTRL-C or CTRL-\\ to quit.\n");
signal(SIGINT, terminateServer);
signal(SIGQUIT, terminateServer);
oAddress.sin_family = AF_INET;
oAddress.sin_addr.s_addr = INADDR_ANY;
oAddress.sin_port = htons(SERVER_PORT);
nMainSocketId = socket(AF_INET, SOCK_STREAM, 0);
if (nMainSocketId < 0) {
perror("server: socket() failed");
return 1;
}
if (bind(nMainSocketId, (struct sockaddr *) &oAddress, sizeof(oAddress))) {
perror("server: bind() failed");
terminateServer();
return 1;
}
printf("HTTP server listening on port %d\n", SERVER_PORT);
while (bExiting == 0) {
if (listen(nMainSocketId, 10) < 0) {
perror("server: listen() failed");
terminateServer();
return 1;
}
nReqSocketId = accept(nMainSocketId, (struct sockaddr *) &oAddress, &nAddrLen);
if (bExiting) { break; }
if (nReqSocketId < 0) {
perror("server: accept() failed");
terminateServer();
return 1;
}
nRecvResult = recv(nReqSocketId, sRequest, REQUEST_SIZE, 0);
if (nRecvResult < 0) {
perror("server: recv() failed");
terminateServer();
return 1;
}
if (nRecvResult == 0) {
printf("The client is disconnected. Waiting for another connection...\n");
close(nReqSocketId);
continue;
}
printf("The client is connected...\n\n%s\n", sRequest);
nRespFD = fdopen(nReqSocketId, "a+");
fprintf(nRespFD, "HTTP/1.1 200 OK\n");
fprintf(nRespFD, "Content-length: 50\n");
fprintf(nRespFD, "Content-Type: text/html\n\n");
fprintf(nRespFD, "<html><body><h1>Hello world!!!</h1></body></html>\n");
fclose(nRespFD);
close(nReqSocketId);
}
printf("Goodbye!\n");
return 0;
}
关于shutdown()与close(),您能更好地解释一下区别吗?
好,因此,我的回复的第二部分说明了如何使Web服务器保持非持久状态。持久连接在HTTP 1.1之前被称为Keep-Alive,btw。由于我看到您每次发送后都会关闭响应套接字,因此也可能会使Web服务器“非持久”。
这意味着由于shutdown(),您不必发送“ Content-Length:X”。您可以通过三种不同的方式关闭套接字:
"The constants SHUT_RD, SHUT_WR, SHUT_RDWR have the value 0, 1, 2, respectively..."
通过执行shutdown(SOCKET,1),我们实质上是向客户端(浏览器)发送FIN ack,以使其知道套接字已完成写入。因此,无需设置“ Content-Length:X”头响应。
现在,这不是shutdown()VS close()的事情。这是shutdown()和close()的事情。您仍然必须关闭套接字以销毁它。关机不帮您这样做。
总之,如果您无需跟踪多个连接,请考虑从标题中删除“ Content-Length:”。只需将shutdown(SOCKET,1)用作“传输结束”机制,然后关闭销毁套接字即可。
/ ----------------------------------------------- -------------------------------------------------- --------------------------------- /
就创建整个FILE *仅用于发送字符串文字而言,我不确定是否值得。你替补上场并取得了一些结果吗?无论如何,如果性能是您想要的,我还知道围绕strcat()(当然也包括strlen()和strlen())的其他方法。我不知道是这种情况。
PS- strcat()在一开始就非常快,仅当串联一个大缓冲区时,它才开始增加其复杂性。查找SIMD,以及如何根据您的体系结构优化某些类型的功能。
等待标准输入和套接字
这是特定于POSIX的行为,在Windows上显然不起作用。它依赖于以下事实:在* n * x平台上,套接字是文件描述符,并且使用select
函数。
这里是一个简化的函数,它包装select
以在单个套接字上等待。
/*Waits on a single socket and stdin.*/
int waitOnStdinAndSocket(
int sockFd, /*[in] Socket descriptor*/
int *pInputOnStdin, /*[out] Set to a nonzero value if there is input on stdin*/
int *pInputOnSocket, /*[out] Set to a nonzero value if there is input on the socket*/
sturct timeval *timeout /*[in/opt] Timeout*/
) /*Returns a negative value on failure.*/
{
int ret;
fd_set fds;
*pInputOnStdin = 0;
*pInputOnSocket = 0;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
FD_SET(sockFd, &fds);
ret = select(sockFd+1, &fds, NULL, NULL, timeout);
if(ret >= 0)
{
*pInputOnStdin = FD_ISSET(STDIN_FILENO, &fds);
*pInputOnSocket = FD_ISSET(sockFd, &fds);
}
return ret;
}
@ Medinoc
非常感谢!
阅读您的回复后,我在网上做了一些尝试,并找到了这个GNU页面:http://www.gnu.org/software/libc/manual/html_node/Server-Example.html。
因此,经过一些尝试,我已经能够将所有内容与“ hello world”示例进行整合。结果如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#define SERVER_PORT 80
#define REQUEST_MAX_SIZE 1024
char sRequest[REQUEST_MAX_SIZE];
int bListening = 1, nMainSocket;
void terminateServer () {
if (bListening == 0) { return; }
printf("\n\nTerminating server...\n");
close(nMainSocket);
bListening = 0;
}
void switchStdin () {
if (strcmp(sRequest, "q\n") == 0) {
terminateServer();
} else {
printf("Unknown request %s\n", sRequest);
}
}
void helloWorld (const int nRequestId) {
printf("The client is connected...\n\n%s\n", sRequest);
write(nRequestId, "HTTP/1.1 200 OK\n", 16);
write(nRequestId, "Content-length: 50\n", 19);
write(nRequestId, "Content-Type: text/html\n\n", 25);
write(nRequestId, "<html><body><h1>Hello world!!!</h1></body></html>\n", 50);
}
int main (void) {
int nOldReqSock, nNewReqSock, nReqLen, nUninitLen = REQUEST_MAX_SIZE;
fd_set oActiveFD, oReadFD;
socklen_t nAddrLen;
struct sockaddr_in oAddress;
oAddress.sin_family = AF_INET;
oAddress.sin_addr.s_addr = INADDR_ANY;
oAddress.sin_port = htons(SERVER_PORT);
/* Create the socket and set it up to accept connections. */
nMainSocket = socket(AF_INET, SOCK_STREAM, 0);
if (nMainSocket < 0) {
perror("socket");
return 1;
}
if (bind(nMainSocket, (struct sockaddr *) &oAddress, sizeof(oAddress))) {
perror("bind");
terminateServer();
return 1;
}
if (listen(nMainSocket, 10) < 0) {
perror("listen");
terminateServer();
return 1;
}
/* Initialize the set of active sockets plus STDIN. */
FD_ZERO(&oActiveFD);
FD_SET(STDIN_FILENO, &oActiveFD);
FD_SET(nMainSocket, &oActiveFD);
printf("\n W E L C O M E!\n\nType \"q\" to quit.\n");
while (bListening) {
/* Block until input arrives on one or more active sockets. */
oReadFD = oActiveFD;
if (select(FD_SETSIZE, &oReadFD, NULL, NULL, NULL) < 0) {
perror("select");
terminateServer();
return EXIT_FAILURE;
}
/* Service all the sockets with input pending. */
for (nOldReqSock = 0; bListening && nOldReqSock < FD_SETSIZE; ++nOldReqSock) {
if (FD_ISSET(nOldReqSock, &oReadFD)) {
if (nOldReqSock == nMainSocket) {
/* Connection request on original socket. */
nAddrLen = sizeof(oAddress); /* why??? */
nNewReqSock = accept(nMainSocket, (struct sockaddr *) &oAddress, &nAddrLen);
if (nNewReqSock < 0) {
perror("accept");
terminateServer();
return EXIT_FAILURE;
}
FD_SET(nNewReqSock, &oActiveFD);
} else {
/* Data arriving on an already-connected socket. */
nReqLen = read(nOldReqSock, sRequest, REQUEST_MAX_SIZE);
if (nReqLen < 0) {
/* Read error. */
perror("read");
terminateServer();
return EXIT_FAILURE;
} else if (nReqLen == 0) {
/* End-of-file. */
printf("End-of-file\n");
close(nOldReqSock); /* why??? */
FD_CLR(nOldReqSock, &oActiveFD); /* why??? */
continue;
} else {
/* Data read. */
if (nUninitLen > nReqLen) { memset(sRequest + nReqLen, 0, nUninitLen - nReqLen); }
nUninitLen = nReqLen;
}
if (nOldReqSock == STDIN_FILENO) {
/* Standard input received */
switchStdin(nReqLen);
} else {
/* TCP/IP request received */
helloWorld(nOldReqSock);
}
}
}
}
}
printf("Goodbye\n");
return 0;
}
我还在我不理解的行附近添加了一些/* why??? */
注释。我可以请您简要解释一下吗?
根据@ Medinoc,@ someuser和@Elchonon的建议,我通过以下方式更正了我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <signal.h>
#define SERVER_PORT 80
int nStatus, nMainSocketId;
void onInt () {
printf("You have pressed CTRL-C.\n");
nStatus = 0;
shutdown(nMainSocketId, 2);
}
void onQuit () {
printf("You have pressed CTRL-\\.\n");
nStatus = 0;
shutdown(nMainSocketId, 2);
}
int main () {
int nReqSocketId, nReqSize = 1024;
nMainSocketId = socket(AF_INET, SOCK_STREAM, 0);
char *sRequest = malloc(nReqSize);
socklen_t nAddrLen;
struct sockaddr_in oAddress;
signal(SIGINT, onInt);
signal(SIGQUIT, onQuit);
oAddress.sin_family = AF_INET;
oAddress.sin_addr.s_addr = INADDR_ANY;
oAddress.sin_port = htons(SERVER_PORT);
nStatus = 1;
printf("\n W E L C O M E!\n\nPress CTRL-C or CTRL-\\ to quit.\n");
if (nMainSocketId == 0) {
fprintf(stderr, "Error during the creation of the socket\n");
return 1;
}
if (bind(nMainSocketId, (struct sockaddr *) &oAddress, sizeof(oAddress))) {
fprintf(stderr, "The port %d is busy\n", SERVER_PORT);
close(nMainSocketId);
return 1;
}
printf("HTTP server listening on port %d\n", SERVER_PORT);
while (nStatus) {
if (listen(nMainSocketId, 10) < 0) {
perror("server: listen");
close(nMainSocketId);
return 1;
}
nReqSocketId = accept(nMainSocketId, (struct sockaddr *) &oAddress, &nAddrLen);
if (nReqSocketId < 0) {
perror("server: accept");
close(nMainSocketId);
return 1;
}
recv(nReqSocketId, sRequest, nReqSize, 0);
if (nReqSocketId > 0){
printf("The Client is connected...\n\n%s\n", sRequest);
}
write(nReqSocketId, "HTTP/1.1 200 OK\n", 16);
write(nReqSocketId, "Content-length: 50\n", 19);
write(nReqSocketId, "Content-Type: text/html\n\n", 25);
write(nReqSocketId, "<html><body><h1>Hello world!!!</h1></body></html>\n", 50);
close(nReqSocketId);
}
printf("Goodbye!\n");
close(nMainSocketId);
return 0;
}
现在该过程终止了!!但是,不幸的是,插座不! :(…因此,每当我按CTRL-C或CTRL- \以退出时,都会收到以下消息:
server: accept: Invalid argument
是否有解决建议?
编辑
@ alk
谢谢您的回答!您向我建议了另一种软终止网络服务器的方法...:
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <signal.h>
#define SERVER_PORT 80
int bExiting, nMainSocketId;
void terminateServer () {
if (bExiting) { return; }
printf("\n\nTerminating server...\n");
close(nMainSocketId);
bExiting = 1;
}
int main () {
int nRecvResult = -1, nReqSocketId = -1;
size_t nReqSize = 1024;
char * sRequest = malloc(nReqSize);
socklen_t nAddrLen;
struct sockaddr_in oAddress = {0};
printf("\n W E L C O M E!\n\nPress CTRL-C or CTRL-\\ to quit.\n");
signal(SIGINT, terminateServer);
signal(SIGQUIT, terminateServer);
oAddress.sin_family = AF_INET;
oAddress.sin_addr.s_addr = INADDR_ANY;
oAddress.sin_port = htons(SERVER_PORT);
nMainSocketId = socket(AF_INET, SOCK_STREAM, 0);
if (nMainSocketId < 0) {
perror("server: socket() failed");
return 1;
}
if (bind(nMainSocketId, (struct sockaddr *) &oAddress, sizeof(oAddress))) {
perror("server: bind() failed");
terminateServer();
return 1;
}
printf("HTTP server listening on port %d\n", SERVER_PORT);
while (bExiting == 0) {
if (listen(nMainSocketId, 10) < 0) {
perror("server: listen() failed");
terminateServer();
return 1;
}
nReqSocketId = accept(nMainSocketId, (struct sockaddr *) &oAddress, &nAddrLen);
if (bExiting) { break; }
if (nReqSocketId < 0) {
perror("server: accept failed()");
terminateServer();
return 1;
}
nRecvResult = recv(nReqSocketId, sRequest, nReqSize, 0);
if (nRecvResult < 0) {
perror("server: recv() failed");
terminateServer();
return 1;
}
if (nRecvResult == 0) {
printf("The client is disconnected. Waiting for another connection...\n");
close(nReqSocketId);
continue;
}
printf("The client is connected...\n\n%s\n", sRequest);
/* This is only a simple "Hello world"... */
write(nReqSocketId, "HTTP/1.1 200 OK\n", 16);
write(nReqSocketId, "Content-length: 50\n", 19);
write(nReqSocketId, "Content-Type: text/html\n\n", 25);
write(nReqSocketId, "<html><body><h1>Hello world!!!</h1></body></html>\n", 50);
close(nReqSocketId);
}
free(sRequest);
printf("Goodbye!\n");
return 0;
}
您如何看待?我应该添加些东西吗?
仍然,我还有另一个问题。我可以将fprintf()与文件描述符结合使用来代替write()吗?还有...如何?