Trying to try to write a simple http server. To begin with, I'm just trying to create a TCP server. It seems to be a port, but when you try to connect to the server, an error crashes. alt text

#include "web_server.h"; #define HOST "127.0.0.1" SOCKET Start_Server(short port) { WSADATA wsaData; SOCKET Winsock; struct sockaddr_in Winsock_In; struct hostent *Ip; char hn[1024]; WSAStartup(MAKEWORD(2, 2), &wsaData); Winsock=WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, (unsigned int)NULL, (unsigned int)NULL); if(Winsock == INVALID_SOCKET){ WSACleanup(); return -1; } Ip = gethostbyname(HOST); Winsock_In.sin_port = htons(port); Winsock_In.sin_family = AF_INET; Winsock_In.sin_addr.s_addr = inet_addr(HOST);//inet_addr(inet_ntoa(*((in_addr*)Ip->h_addr_list[0]))); if(bind(Winsock, (SOCKADDR*)&Winsock_In, sizeof(Winsock_In)) == SOCKET_ERROR){ WSACleanup(); return -1; } return Winsock; } void GetHtmlList(FILE* f) { fprintf(f, "<HTML><HEAD><TITLE>ggg</TITLE></HEAD>\r\n"); } void Main_WebServer() { SOCKET Winsock; char Buf[255]; Winsock = Start_Server(5656); listen(Winsock, 5); strcat(Buf, "TEST!!!"); while (1) { int s; FILE *f; s = accept(Winsock, (sockaddr *)HOST, (int *)HOST); //send(s, Buf, (int)strlen(Buf), 0); if (s < 0) break; f = fdopen(s, "r+"); GetHtmlList(f); fclose(f); } //close(Winsock); } 

Help me to understand )

  • First, print printfs, determine which line to crash into. And it's better to make a console application for now (it's easier to debug). It looks suspiciously strcat (Buf, "TEST !!!"); Mb is strcpy () better? - avp

1 answer 1

Found a mistake) It should be so

 struct sockaddr_in client; int clientsize = sizeof(client); s = accept(Winsock, (struct sockaddr*)&client, &clientsize); 

  • Now I can not figure out how to display it in the browser. Be sure to send all the headers? - or_die
  • Yes, send headlines <br> ru.wikipedia.org/wiki/HTTP - toxicdream
  • I just have a page hanging, trying to load, but it never loads. Those. It turns out, you need to start accepting headers from the browser, and then send it to the browser? - or_die
  • I advise you to track communication with browsers by some sniffer. For example, wireshark.org See how the browser communicates with a working server and how it is with yours. - Stas Litvinenko