Я написал веб-сервер на c, и он может установить соединение, введя http://localhost:8080/ в веб-браузере, но как мне продвинуться в проекте, чтобы я мог получить домашнюю страницу в браузере?
Единственное, что происходит сейчас, это то, что соединение принято, и больше ничего!Если вы хотите увидеть код, то так и скажите!
main(){
WORD wVersionRequested;
WSADATA wsaData;
int err, ok, clientAddrLen, serverSocket, clientSocket;
struct addrinfo *info;
struct sockaddr clientAddr;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
//Creates a socket with 3 pre-defined values found in the libraries.
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ok = getaddrinfo("127.0.0.1", "8080", NULL, &info);
if(ok!=0) {
WCHAR * error = gai_strerror(ok);
printf("%s\n",error);
}
printf("Waiting for connection...\n");
//Binds the port to the socket.
ok = bind(serverSocket, info->ai_addr, info->ai_addrlen);
if(ok == SOCKET_ERROR) {
err = WSAGetLastError();
printf("%d\n",err);
}
ok = listen(serverSocket, SOMAXCONN);
if(ok == SOCKET_ERROR) {
err = WSAGetLastError();
printf("%d\n",err);
}
//Creates new socket after another IP connects.
clientAddrLen = sizeof(clientAddr);
clientSocket = accept(serverSocket, &clientAddr, &clientAddrLen);
printf("Connected!\n");
//Sends a message on the new socket.
sendMessage(clientSocket);
printf("Message sent.\n");
//Writes a log for the server.
writeServerLog("127.0.0.1", " GET /HTTP1.1 C:/Server/index.html", "index.html", 100);
getchar(); //Waiting for input, Used to stop the server before terminating.
//Cleanup.
closesocket(serverSocket);
closesocket(clientSocket);
WSACleanup();
}
void sendMessage(int socket){
string message;
message = "<html><body>Welcome to the server!</body></html>";
send(socket, message, strlen(message), 0);
}
void writeServerLog(string ip, string recieved, string response, int sizeOfResponse){
FILE *log;
SYSTEMTIME st;
GetSystemTime(&st);
log = fopen("serverlog.txt", "a");
fprintf(log, "IP: %s DaTi: %d-%d-%d %d:%d:%d Recv: %s Resp: %s Size: %d\n", ip, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, recieved, response, sizeOfResponse);
fclose(log);
}