-
Posts
1903 -
Joined
-
Last visited
Content Type
Profiles
Forums
Events
Everything posted by fiveworlds
-
Truly misterious and unique computer problem
fiveworlds replied to Lord Antares's topic in Computer Science
Possibly if the motherboard was badly made. Like crosstalk where a large voltage induces current in a nearby wire connected to the power on switch. -
Truly misterious and unique computer problem
fiveworlds replied to Lord Antares's topic in Computer Science
Are you sure you aren't being hacked?? Was the computer bought from a legitimate store?? -
So what it is a 50 50 chance. A chimpanzee could correctly predict it 50% of the time. What? Last I checked the CIA was run by Americans and not aliens. Why?
-
I followed basic algebraic rules z is a positive whole number multiple of 2. Y is more complicated it is a black box polynomial we don't know anything about since each polynomial will be specific to what number you put in. We do know however that it will follow basic algebra so we don't need to know anything about it.
-
Is it easier to see if I write it like this?? (3x+1)/2= x y(3x+1)/z = x 3yx - xz + y = 0 x(3y - z) + y = 0 x = -y/(3y-z) x = -10/(30-32)As you can see if x is positive it is a fraction and it will not increase forever. We know it won't increase forever for certain values because it is a group of linear equations.
-
Trying to aquire a scientific calculator
fiveworlds replied to ManuelAhumada1's topic in Science Education
Why not ask the school if the T180 would be ok?? https://www.amazon.com/Texas-Instruments-TI-80-Graphing-Calculator/dp/B000OMXVA0/ref=sr_1_1?ie=UTF8&qid=1486313191&sr=8-1&keywords=t180+graphing+calculator -
We could expand the equation out but all that happens the numbers become worse recurring fractions or negative numbers never positive whole numbers for loops and we can assume that no numbers cause it to increase forever.
-
Okay. So for collatz to be false. We must satisfy a simple function and show looping. (3x+1)/2 =x (3x + 1) = 2x 3x - 2x + 1 = 0 We can solve for all looping in the colatz conjecture by introducing a new variable y which is a multiple of 2. 3x - yx + 1 = 0 All except for 2 loops are looping fractions. Therefore no real number exists that falsifies colatz
-
No it is isn't since all even numbers are divisible by 2. We just need to know what the last char is. <script> for(i=1;i<10;i=i+2){ if((i*3+1)%2!=0){ document.write("error"); } } </script> Do you see what I am getting at?? What matters is the frequency of division by 2. Division by 2 occurs more frequently and therefore every number must reduce to 1.
-
So I cleaned up the headers and now they match those sent by the Apache httpd as far as I can tell (apart from servername). //Sendfile.cpp #include "StdAfx.h" #include <Windows.h> #include "MimeHelper.h" #include <algorithm> #include <string> #include "time.h" #include <memory.h> #include "Sendfile.h" #include <stdlib.h> #include <iostream> #include <fstream> #include <tchar.h> #include <strsafe.h> #define LastModifiedBuffer 49 #define GMTLENGTH 37 int sendHeader(int socket, char *header) { char dest[3000]; const char* CRLF = "\r\n"; errno_t headerError; strcpy_s(dest, _countof(dest), header); headerError = strncat_s(dest, _countof(dest), CRLF, strlen(CRLF)); if (headerError) { printf("Error code: %d", (int)headerError); return 0; } header = (char*)dest; send(socket, header, strlen(header), 0); return 1; } int sendDateHeader(int socket) { SYSTEMTIME st; GetSystemTime(&st); char int_to_day[][7] = { { "Mon" },{ "Tue" },{ "Wed" }, { "Thu" },{ "Fri" },{ "Sat" }, { "Sun" } }; char int_to_month[][12] = { { "Jan" },{ "Feb" },{ "Mar" }, { "Apr" },{ "May" },{ "Jun" }, { "Jul" },{ "Aug" },{ "Sep" }, { "Oct" },{ "Nov" },{ "Dec" }, }; char GMT[GMTLENGTH]; sprintf_s(GMT, GMTLENGTH, "Date: %s, %d %s %02d %02d:%02d:%02d GMT", int_to_day[st.wDayOfWeek], st.wDay, int_to_month[st.wMonth], st.wYear, st.wHour, st.wMinute, st.wSecond); char* dateHeader = GMT; sendHeader(socket, GMT); return 1; } int sendContentLength(int socket, int length) { char *message, *contentlength, buffer[32], dest[3000]; errno_t errNum; sprintf_s(buffer, sizeof(buffer),"Content-Length: %d", length); contentlength = buffer; printf(contentlength); printf("\n");printf("\n"); sendHeader(socket, contentlength); return 1; } // GetLastWriteTime - Retrieves the last-write time and converts // the time to a string // // Return value - TRUE if successful, FALSE otherwise // hFile - Valid file handle // lpszString - Pointer to buffer to receive string #define LastModifiedBuffer 49 BOOL GetLastWriteTime(int socket, HANDLE hFile, LPTSTR lpszString, DWORD dwSize) { FILETIME ftCreate, ftAccess, ftWrite; SYSTEMTIME stUTC, stLocal; DWORD dwRet; char LastModified[LastModifiedBuffer]; // Retrieve the file times for the file. if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite)) return FALSE; // Convert the last-write time to local time. FileTimeToSystemTime(&ftWrite, &stUTC); SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal); char int_to_day[][7] = { { "Mon" },{ "Tue" },{ "Wed" }, { "Thu" },{ "Fri" },{ "Sat" }, { "Sun" } }; char int_to_month[][12] = { { "Jan" },{ "Feb" },{ "Mar" }, { "Apr" },{ "May" },{ "Jun" }, { "Jul" },{ "Aug" },{ "Sep" }, { "Oct" },{ "Nov" },{ "Dec" }, }; // Build a string showing the date and time. sprintf_s(LastModified, LastModifiedBuffer, "Last-Modified: %s, %d %s %02d %02d:%02d:%02d GMT", int_to_day[stLocal.wDayOfWeek], stLocal.wDay, int_to_month[stLocal.wMonth], stLocal.wYear, stLocal.wHour, stLocal.wMinute, stLocal.wSecond); sendHeader(socket, LastModified); } wchar_t *convertCharArrayToLPCWSTR(const char* charArray) { wchar_t* wString = new wchar_t[4096]; MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096); return wString; } int sendLastModified(int socket, char* file) { HANDLE hFile; TCHAR szBuf[MAX_PATH]; LPCWSTR filename = convertCharArrayToLPCWSTR(file); hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("CreateFile failed with %d\n", GetLastError()); Sleep(2000); return 0; } GetLastWriteTime(socket, hFile, szBuf, MAX_PATH); CloseHandle(hFile); } void sendTextFile(int socket, char* filename, char* contentType) { char *filedata = ""; char *BOUNDARY = "\r\n", *SERVERNAME = "Server: test", *ETAG = "Etag: 19b-5471fb7cf045b", *ACCEPTRANGES = "Accept-Ranges: bytes", *HTTP_OK = "HTTP/1.1 200 OK", *KEEPALIVE = "Keep-Alive: timeout=5, max=100", *CONNECTION = "Connection: Keep-Alive"; FILE *fp; std::ifstream is(filename, std::ifstream::binary); if (is) { // get length of file: is.seekg(0, is.end); int length = is.tellg(); is.seekg(0, is.beg); if (sendHeader(socket, HTTP_OK) == 0) //Set Http ok header { ///Error Handling } if (sendDateHeader(socket) == 0)//Set the content-length { ///Error Handling } if (sendHeader(socket, SERVERNAME) == 0) //Set servername header { ///Error Handling } if (sendLastModified(socket, filename) == 0) //Set servername header { ///Error Handling } if (sendHeader(socket, ETAG) == 0) //Set the etag header { ///Error Handling } if (sendHeader(socket, ACCEPTRANGES) == 0) //Set the accept-ranges header { ///Error Handling } if (sendContentLength(socket, length) == 0) //Set the content-length { ///Error Handling } if (sendHeader(socket, KEEPALIVE) == 0) //Set the content mime-type header { ///Error Handling } if (sendHeader(socket, CONNECTION) == 0) //Set the content mime-type header { ///Error Handling } if (sendHeader(socket, contentType) == 0) //Set the content mime-type header { ///Error Handling } if (sendHeader(socket, "") == 0)//set the content boundary { ///Error Handling } char * buffer = new char[length]; std::cout << "Reading " << length << " characters... "; // read data as a block: is.read(buffer, length); if (is) { std::cout << "all characters read successfully."; } else { std::cout << "error: only " << is.gcount() << " could be read"; } is.close(); send(socket, buffer, length, 0); delete[] buffer; } else { printf("error"); } } const char* Sendfile::sendfile(int socket, char* filename) { char* contentType; contentType = (char*)MimeHelper::GetMimeTypeFromFileName(filename); if (strstr(contentType, "text") != NULL) { sendTextFile(socket, filename, contentType); } else { sendTextFile(socket, filename, contentType); } shutdown(socket, 1); return "true"; } Hmm SSL is to stop man in the middle attacks via script and form injection into transferred packets from getting access to user data by securely sending data. Wrong I try and test absolutely everything I use. Which takes longer than just reading the material.
-
Yes but it is good to learn. For instance if I wanted to make a game with payment processing then I have to know how to setup ssl sockets because most payment providers won't accept payment from insecure connections. Hewlett Packard has a tutorial on setting up an ssl socket http://h41379.www4.hpe.com/doc/83final/ba554_90007/ch04s03.html using the openSSL API but it is pointless following it until other stuff (like loading images) works properly. At the moment I want to make a code editor with syntax highlighting which will run a web server with https and open the page in google chrome when I click the run button. ps zip data is sent with a boundary given by a multipart/form-data post request. Like so not exactly sure how to receive that content in C though. -----------------------------25312170725491 Content-Disposition: form-data; name="test"; filename="ColorCode_1_0_2.zip" Content-Type: application/x-zip-compressed CRLF content -----------------------------25312170725491 What I am receiving at the moment looks like -----------------------------1137250621972 Content-Disposition: form-data; name="test"; filename="ColorCode_1_0_2.zip" Content-Type: application/x-zip-compressed PK in firefox so obviously the recv is cutting off halfway.
-
Yeah it should be a fun project. I know most of the protocols already. I learn by doing schools can be so frustrating like that who learns anything from reading books?? Not over http. Browsers encrypt with https. That's right https works on every port. However Browsers use 443. Oh and consts are variables kept in memory for faster runtimes on repetitive tasks. #define is not kept in memory.
-
It's a https web server i'm trying to make. So I want to use forms from HTML code. The files are included as resources in the project folder. <!DOCTYPE html> <html> <head> <link type='text/css' rel='stylesheet' href='index.css' /> <link rel='shortcut icon' type='image/x-icon' href='favicon.ico' /> </head> <body> <a href='test'>Testing</a> <img src="favicon.png" /> <form method="post"> <input type="text" name="test" /> <input type="submit" /> </form> </body> </html> Using a constant #include "StdAfx.h" #include <Windows.h> #include "MimeHelper.h" #include <algorithm> #include <string> #include "time.h" #include <memory.h> #include "Sendfile.h" #include <stdlib.h> #include <iostream> #include <fstream> const int BUFFERSIZE = 3000; void sendHeader(int socket, char *header) { char dest[BUFFERSIZE]; const char* CRLF = "\r\n"; errno_t headerError; strcpy_s(dest, _countof(dest), header); headerError = strncat_s(dest, _countof(dest), CRLF, strlen(CRLF)); if (headerError) { printf("Error code: %d", (int)headerError); } header = (char*)dest; send(socket, header, strlen(header), 0); } void sendDateHeader(int socket) { struct tm newtime; __time32_t aclock; const char* CRLF = "\r\n"; char *dateHeader, buff[BUFFERSIZE], buffer[32], *date; errno_t errDate; _time32(&aclock); // Get time in seconds. _localtime32_s(&newtime, &aclock); // Convert time to struct tm form. errDate = asctime_s(buff, 32, &newtime); if (errDate) { printf("Error code: %d", (int)errDate); } //get the current date dateHeader = "Date: "; date = (char*)buff; strcpy_s(buff, _countof(buff), dateHeader); errDate = strncat_s(buff, _countof(buff), date, strlen(date)); if (errDate) { printf("Error code: %d", (int)errDate); } dateHeader = (char*)buff; sendHeader(socket, dateHeader); } void sendContentLength(int socket, int length) { char *message,*contentlength,buffer[32],dest[BUFFERSIZE]; errno_t errNum; message = "Content-Length: "; sprintf(buffer, "%d", length); contentlength = buffer; sendHeader(socket, contentlength); } void sendTextFile(int socket, char* filename, char* contentType) { char *filedata = ""; char *BOUNDARY = "\r\n", *SERVERNAME = "Server: test", *HTTP_OK = "HTTP/1.1 200 OK"; FILE *fp; std::ifstream is(filename, std::ifstream::binary); if (is) { // get length of file: is.seekg(0, is.end); int length = is.tellg(); is.seekg(0, is.beg); sendHeader(socket, HTTP_OK);//Set Http ok header sendHeader(socket, SERVERNAME);//Set servername header sendHeader(socket, contentType);//Set the content mime-type sendContentLength(socket, length);//Set the content-length sendDateHeader(socket);//Set the current date sendHeader(socket, "");//set the content boundary char * buffer = new char[length]; std::cout << "Reading " << length << " characters... "; // read data as a block: is.read(buffer, length); if (is) std::cout << "all characters read successfully."; else std::cout << "error: only " << is.gcount() << " could be read"; is.close(); send(socket, buffer, length, 0); delete[] buffer; } else { printf("error"); } } const char* Sendfile::sendfile(int socket, char* filename) { char* contentType; contentType = (char*)MimeHelper::GetMimeTypeFromFileName(filename); if(strstr(contentType, "text") != NULL) { sendTextFile(socket, filename, contentType); } else { sendTextFile(socket, filename, contentType); } shutdown(socket, 1); return "true"; } Using the #define macro properly. #define max(A,B) ((A) >(B) ? (A):(B)) #define is like a function but isn't it performs inline substitution so that. x = max(p+q,r+s); is replaced by x = ((p+q) > (r+s) ? (p+q) : (r+s)); at compile time.
-
#define is for macros. Why would I use it for a constant?? That's a tiny change. #include "StdAfx.h" #include <Windows.h> #include "MimeHelper.h" #include <algorithm> #include <string> #include "time.h" #include <memory.h> #include "Sendfile.h" #include <stdlib.h> #include <iostream> #include <fstream> void sendHeader(int socket, char *header) { char dest[3000]; const char* CRLF = "\r\n"; errno_t headerError; strcpy_s(dest, _countof(dest), header); headerError = strncat_s(dest, _countof(dest), CRLF, strlen(CRLF)); if (headerError) { printf("Error code: %d", (int)headerError); } header = (char*)dest; send(socket, header, strlen(header), 0); } void sendDateHeader(int socket) { struct tm newtime; __time32_t aclock; const char* CRLF = "\r\n"; char *dateHeader, buff[3000], buffer[32], *date; errno_t errDate; _time32(&aclock); // Get time in seconds. _localtime32_s(&newtime, &aclock); // Convert time to struct tm form. errDate = asctime_s(buff, 32, &newtime); if (errDate) { printf("Error code: %d", (int)errDate); } //get the current date dateHeader = "Date: "; date = (char*)buff; strcpy_s(buff, _countof(buff), dateHeader); errDate = strncat_s(buff, _countof(buff), date, strlen(date)); if (errDate) { printf("Error code: %d", (int)errDate); } dateHeader = (char*)buff; sendHeader(socket, dateHeader); } void sendContentLength(int socket, int length) { char *message,*contentlength,buffer[32],dest[3000]; errno_t errNum; message = "Content-Length: "; sprintf(buffer, "%d", length); contentlength = buffer; sendHeader(socket, contentlength); } void sendTextFile(int socket, char* filename, char* contentType) { char *filedata = ""; char *BOUNDARY = "\r\n", *SERVERNAME = "Server: test", *HTTP_OK = "HTTP/1.1 200 OK"; FILE *fp; std::ifstream is(filename, std::ifstream::binary); if (is) { // get length of file: is.seekg(0, is.end); int length = is.tellg(); is.seekg(0, is.beg); sendHeader(socket, HTTP_OK);//Set Http ok header sendHeader(socket, SERVERNAME);//Set servername header sendHeader(socket, contentType);//Set the content mime-type sendContentLength(socket, length);//Set the content-length sendDateHeader(socket);//Set the current date sendHeader(socket, "");//set the content boundary char * buffer = new char[length]; std::cout << "Reading " << length << " characters... "; // read data as a block: is.read(buffer, length); if (is) std::cout << "all characters read successfully."; else std::cout << "error: only " << is.gcount() << " could be read"; is.close(); send(socket, buffer, length, 0); delete[] buffer; } else { printf("error"); } } const char* Sendfile::sendfile(int socket, char* filename) { char* contentType; contentType = (char*)MimeHelper::GetMimeTypeFromFileName(filename); if(strstr(contentType, "text") != NULL) { sendTextFile(socket, filename, contentType); } else { sendTextFile(socket, filename, contentType); } shutdown(socket, 1); return "true"; } Turns out I just needed to add an else if to get the post request data. else if (strstr(server_reply, "POST / HTTP/1.1") != NULL) { Sendfile::sendfile(new_socket, "index.html"); } The subsequent post header data POST / HTTP/1.1 Host: localhost Connection: keep-alive Content-Length: 9 Cache-Control: max-age=0 Origin: http://localhost Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36 Content-Type: application/x-www-form-urlencoded Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Referer: http://localhost/ Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.8 test=test╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
-
Why can a woman sue a man for child support in rape cases?
fiveworlds replied to Raider5678's topic in Politics
Could you not argue that the child should be put up for adoption on the basis that you believe the woman is an unfit mother and you are not in a position to be supporting the financial needs of the child?? -
Okay so I changed Sendfile.cpp and it appears to be sending images now. #include "StdAfx.h" #include <Windows.h> #include "MimeHelper.h" #include <algorithm> #include <string> #include "time.h" #include <memory.h> #include "Sendfile.h" #include <stdlib.h> #include <iostream> #include <fstream> void sendHeader(int socket, char *header) { char dest[3000]; const char* CRLF = "\r\n"; errno_t headerError; strcpy_s(dest, _countof(dest), header); headerError = strncat_s(dest, _countof(dest), CRLF, strlen(CRLF)); if (headerError) { printf("Error code: %d", (int)headerError); } header = (char*)dest; send(socket, header, strlen(header), 0); } void sendDateHeader(int socket) { struct tm newtime; __time32_t aclock; const char* CRLF = "\r\n"; char *dateHeader, buff[MAXCHAR], buffer[32], *date; errno_t errDate; _time32(&aclock); // Get time in seconds. _localtime32_s(&newtime, &aclock); // Convert time to struct tm form. errDate = asctime_s(buff, 32, &newtime); if (errDate) { printf("Error code: %d", (int)errDate); } //get the current date dateHeader = "Date: "; date = (char*)buff; strcpy_s(buff, _countof(buff), dateHeader); errDate = strncat_s(buff, _countof(buff), date, strlen(date)); if (errDate) { printf("Error code: %d", (int)errDate); } dateHeader = (char*)buff; strcpy_s(buff, _countof(buff), dateHeader); errDate = strncat_s(buff, _countof(buff), CRLF, strlen(CRLF)); if (errDate) { printf("Error code: %d", (int)errDate); } dateHeader = (char*)buff; send(socket, dateHeader, strlen(dateHeader), 0); } void sendContentLength(int socket, int length) { char *message,*contentlength,buffer[32],dest[MAXCHAR]; errno_t errNum; message = "Content-Length: "; sprintf(buffer, "%d", length); contentlength = buffer; strcpy_s(dest, _countof(dest), message); errNum = strncat_s(dest, _countof(dest), contentlength, strlen(contentlength)); message = (char*)dest; send(socket, message, strlen(message), 0); } void sendTextFile(int socket, char* filename, char* contentType) { char *filedata = ""; char *BOUNDARY = "\r\n", *SERVERNAME = "Server: test", *HTTP_OK = "HTTP/1.1 200 OK"; FILE *fp; std::ifstream is(filename, std::ifstream::binary); if (is) { // get length of file: is.seekg(0, is.end); int length = is.tellg(); is.seekg(0, is.beg); sendHeader(socket, HTTP_OK);//Set Http ok header sendHeader(socket, SERVERNAME);//Set servername header sendHeader(socket, contentType);//Set the content mime-type sendContentLength(socket, length);//Set the content-length sendDateHeader(socket);//Set the current date send(socket, BOUNDARY, strlen(BOUNDARY), 0);//set the content boundary char * buffer = new char[length]; std::cout << "Reading " << length << " characters... "; // read data as a block: is.read(buffer, length); if (is) std::cout << "all characters read successfully."; else std::cout << "error: only " << is.gcount() << " could be read"; is.close(); send(socket, buffer, length, 0); delete[] buffer; } else { printf("error"); } } const char* Sendfile::sendfile(int socket, char* filename) { char* contentType; contentType = (char*)MimeHelper::GetMimeTypeFromFileName(filename); if(strstr(contentType, "text") != NULL) { sendTextFile(socket, filename, contentType); } else { sendTextFile(socket, filename, contentType); } shutdown(socket, 1); return "true"; } I don't know how to recieve post requests yet. Get requests are simple enough.
-
Okay so now the problem is with loading images. They seem to be loading forever for some reason. // socketprogramming4.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <io.h> #include <stdio.h> #include <stdlib.h> #include <dos.h> #include <winsock2.h> #include "Sendfile.h" #pragma comment(lib,"ws2_32.lib") //Winsock Library char dest[MAXCHAR]; bool send_all(int socket, void *buffer, size_t length) { char *ptr = (char*)buffer; while (length > 0) { int i = send(socket, ptr, length, 0); if (i < 1) return false; ptr += i; length -= i; } shutdown(socket, SD_SEND); return true; } char* strncat_s_tester(const char * initialDest, const char * src, int count) { char* temp; strcpy_s(dest, _countof(dest), initialDest); errno_t err = strncat_s(dest, _countof(dest), src, count); temp = (char*) dest; return temp; } int strpos(char *haystack, char *needle) { char *p = strstr(haystack, needle); if (p) return p - haystack; return -1; } int main(int argc, char *argv[]) { WSADATA wsa; SOCKET s, new_socket; struct sockaddr_in server, client; int c; char *message, server_reply[2000]; int recv_size; printf("\nInitialising Winsock..."); if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { printf("Failed. Error Code : %d", WSAGetLastError()); return 1; } printf("Initialised.\n"); //Create a socket if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { printf("Could not create socket : %d", WSAGetLastError()); } printf("Socket created.\n"); //Prepare the sockaddr_in structure server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(80); //Bind if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR) { printf("Bind failed with error code : %d", WSAGetLastError()); exit(EXIT_FAILURE); } puts("Bind done"); //Listen to incoming connections listen(s, 3); //Accept and incoming connection puts("Waiting for incoming connections..."); c = sizeof(struct sockaddr_in); while ((new_socket = accept(s, (struct sockaddr *)&client, &c)) != INVALID_SOCKET) { if ((recv_size = recv(new_socket, server_reply, 2000, 0)) == SOCKET_ERROR) { puts("recv failed"); } else { puts(server_reply); } //Receive a reply from the server if (strstr(server_reply, "GET / HTTP/1.1") != NULL) { Sendfile::sendfile(new_socket, "index.html"); } else if (strstr(server_reply, "index.css") != NULL) { Sendfile::sendfile(new_socket,"index.css"); } else if (strstr(server_reply, "favicon.ico") != NULL) { Sendfile::sendfile(new_socket, "favicon.ico"); } else if (strstr(server_reply, "favicon.png") != NULL) { Sendfile::sendfile(new_socket, "favicon.png"); } else { message = "File Not Found"; send_all(new_socket, message, strlen(message)); } } if (new_socket == INVALID_SOCKET) { printf("accept failed with error code : %d", WSAGetLastError()); return 1; } closesocket(s); WSACleanup(); return 0; }MimeHelper.cpp #include "StdAfx.h" #include <Windows.h> #include "MimeHelper.h" #include <algorithm> #include <string> const char MimeTypes[][2][128] = { {"***","Content-Type: application/octet-stream\r\n"}, {"CSV","Content-Type: text/csv\r\n"}, {"TSV","Content-Type: text/tab-separated-values\r\n"}, {"TAB","Content-Type: text/tab-separated-values\r\n"}, {"HTML","Content-Type: text/html\r\n"}, {"HTM","Content-Type: text/html\r\n"}, {"DOC","Content-Type: application/msword\r\n"}, {"DOCX","Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document\r\n"}, {"ODS","Content-Type: application/x-vnd.oasis.opendocument.spreadsheet\r\n"}, {"ODT","Content-Type: application/vnd.oasis.opendocument.text\r\n"}, {"RTF","Content-Type: application/rtf\r\n"}, {"SXW","Content-Type: application/vnd.sun.xml.writer\r\n"}, {"TXT","Content-Type: text/plain\r\n"}, {"XLS","Content-Type: application/vnd.ms-excel\r\n"}, {"XLSX","Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n"}, {"PDF","Content-Type: application/pdf\r\n"}, {"PPT","Content-Type: application/vnd.ms-powerpoint\r\n"}, {"PPS","Content-Type: application/vnd.ms-powerpoint\r\n"}, {"PPTX","Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation\r\n"}, {"WMF","Content-Type: image/x-wmf\r\n"}, {"ATOM","Content-Type: application/atom+xml\r\n"}, {"XML","Content-Type: application/xml\r\n"}, {"JSON","Content-Type: application/json\r\n"}, {"JS","Content-Type: application/javascript\r\n"}, {"OGG","Content-Type: application/ogg\r\n"}, {"PS","Content-Type: application/postscript\r\n"}, {"WOFF","Content-Type: application/x-woff\r\n"}, {"XHTML","Content-Type: application/xhtml+xml\r\n"}, {"XHT","Content-Type: application/xhtml+xml\r\n"}, {"ZIP","Content-Type: application/zip\r\n"}, {"GZ","Content-Type: application/x-gzip\r\n"}, {"RAR","Content-Type: application/rar\r\n"}, {"RM","Content-Type: application/vnd.rn-realmedia\r\n"}, {"RMVB","Content-Type: application/vnd.rn-realmedia-vbr\r\n"}, {"SWF","Content-Type: application/x-shockwave-flash\r\n"}, {"AU","Content-Type: audio/basic\r\n"}, {"SND","Content-Type: audio/basic\r\n"}, {"MID","Content-Type: audio/mid\r\n"}, {"RMI","Content-Type: audio/mid\r\n"}, {"MP3","Content-Type: audio/mpeg\r\n"}, {"AIF","Content-Type: audio/x-aiff\r\n"}, {"AIFC","Content-Type: audio/x-aiff\r\n"}, {"AIFF","Content-Type: audio/x-aiff\r\n"}, {"M3U","Content-Type: audio/x-mpegurl\r\n"}, {"RA","Content-Type: audio/vnd.rn-realaudio\r\n"}, {"RAM","Content-Type: audio/vnd.rn-realaudio\r\n"}, {"WAV","Content-Type: audio/x-wave\r\n"}, {"WMA","Content-Type: audio/x-ms-wma\r\n"}, {"M4A","Content-Type: audio/x-m4a\r\n"}, {"BMP","Content-Type: image/bmp\r\n"}, {"GIF","Content-Type: image/gif\r\n"}, {"JPE","Content-Type: image/jpeg\r\n"}, {"JPEG","Content-Type: image/jpeg\r\n"}, {"JPG","Content-Type: image/jpeg\r\n"}, {"JFIF","Content-Type: image/jpeg\r\n"}, {"PNG","Content-Type: image/png\r\n"}, {"SVG","Content-Type: image/svg+xml\r\n"}, {"TIF","Content-Type: image/tiff\r\n"}, {"TIFF","Content-Type: image/tiff\r\n"}, {"ICO","Content-Type: image/vnd.microsoft.icon\r\n"}, {"CSS","Content-Type: text/css\r\n"}, {"BAS","Content-Type: text/plain\r\n"}, {"C","Content-Type: text/plain\r\n"}, {"H","Content-Type: text/plain\r\n"}, {"RTX","Content-Type: text/richtext\r\n"}, {"MP2","Content-Type: video/mpeg\r\n"}, {"MPA","Content-Type: video/mpeg\r\n"}, {"MPE","Content-Type: video/mpeg\r\n"}, {"MPEG","Content-Type: video/mpeg\r\n"}, {"MPG","Content-Type: video/mpeg\r\n"}, {"MPV2","Content-Type: video/mpeg\r\n"}, {"MOV","Content-Type: video/quicktime\r\n"}, {"QT","Content-Type: video/quicktime\r\n"}, {"LSF","Content-Type: video/x-la-asf\r\n"}, {"LSX","Content-Type: video/x-la-asf\r\n"}, {"ASF","Content-Type: video/x-ms-asf\r\n"}, {"ASR","Content-Type: video/x-ms-asf\r\n"}, {"ASX","Content-Type: video/x-ms-asf\r\n"}, {"AVI","Content-Type: video/x-msvideo\r\n"}, {"3GP","Content-Type: video/3gpp\r\n"}, {"3GPP","Content-Type: video/3gpp\r\n"}, {"3G2","Content-Type: video/3gpp2\r\n"}, {"MOVIE","Content-Type: video/x-sgi-movie\r\n"}, {"MP4","Content-Type: video/mp4\r\n"}, {"WMV","Content-Type: video/x-ms-wmv\r\n"}, {"WEBM","Content-Type: video/webm\r\n"}, {"M4V","Content-Type: video/x-m4v\r\n"}, {"FLV","Content-Type: video/x-flv\r\n"} }; char* get_filename_ext(char* filename) { char* dot = strrchr(filename, '.'); if (!dot || dot == filename) return ""; return dot + 1; } char* ConvertToUpper(char* szSource) { if (szSource == NULL) return NULL; int i = 0; char *str = strdup(szSource); while (str[i]) { if (str[i] >= 97 && str[i] <= 122) str[i] -= 32; i++; } return (str); } const char* MimeHelper::GetMimeTypeFromFileName(char* szFileExt) { szFileExt = get_filename_ext(szFileExt); szFileExt = ConvertToUpper(szFileExt); for (int i = 0; i < sizeof (MimeTypes) / sizeof (MimeTypes[0]); i++) { if (strcmp(MimeTypes[i][0], szFileExt) == 0) { return MimeTypes[i][1]; } } return MimeTypes[0][1]; }sendfile.cpp #include "StdAfx.h" #include "StdAfx.h" #include <Windows.h> #include "MimeHelper.h" #include <algorithm> #include <string> #include "time.h" #include <memory.h> #include "Sendfile.h" #include <stdlib.h> const char* Sendfile::sendfile(int socket, char* filename) { char dest[MAXCHAR]; struct tm newtime; __time32_t aclock; char *message, *date, *contentType, buffer[32], *contentlength = ""; const char *BOUNDARY = "\r\n", *SERVERNAME = "Server: test\r\n", *HTTP_OK = "HTTP/1.1 200 OK\r\n"; _time32(&aclock); // Get time in seconds. _localtime32_s(&newtime, &aclock); // Convert time to struct tm form. errno_t errNum; FILE *fp; int i; errNum = asctime_s(buffer, 32, &newtime); if (errNum) { printf("Error code: %d", (int)errNum); return "false"; } //open and get the file handle if ((fp = fopen(filename, "rb")) == NULL) { return "HTTP 404"; shutdown(socket, 1); } else { //Set Http ok header send(socket, HTTP_OK, strlen(HTTP_OK), 0); //Set servername header send(socket, SERVERNAME, strlen(SERVERNAME), 0); //Set the current date message = "Date : "; date = (char*)buffer; strcpy_s(dest, _countof(dest), message); errNum = strncat_s(dest, _countof(dest), date, strlen(date)); message = (char*)dest; send(socket, message, strlen(message), 0); //Set the content mime-type contentType = (char*)MimeHelper::GetMimeTypeFromFileName(filename); send(socket, contentType, strlen(contentType), 0); //Set the Content-Length fseek(fp, 0L, SEEK_END); i = ftell(fp); rewind(fp); message = "Content-Length: "; sprintf(buffer, "%d", i); contentlength = buffer; strcpy_s(dest, _countof(dest), message); errNum = strncat_s(dest, _countof(dest), contentlength, strlen(contentlength)); message = (char*)dest; send(socket, message, strlen(message), 0); send(socket, "\r\n", strlen("\r\n"), 0); //Set the content boundary send(socket, BOUNDARY, strlen(BOUNDARY), 0); while (fgets(message, 80, fp) != NULL) { send(socket, message, strlen(message), 0); } fclose(fp); } shutdown(socket, 1); return "true"; }
-
The Natural History Museum in London has an exhibit on quartz crystals and their use in silicon growth. Does anybody here know more about the process??
-
Well they can teach you pretty much anything did you read their page?? http://www.gold.ac.uk/ug/bsc-computer-science/ P5 https://p5js.org/ Html, Css, Javascript SQL Computer Architecture. AND OR NOT ALU System bus etc Java
-
I use the caps lock too for typing constants.
-
Alright I'm going to London tomorrow so I won't be able to work on this for a few days.
-
Okay so Seems to be working.
-
shutdown(socket,1) does the same thing. So I just need to add CRLF?? I dunno maybe there is a function on MSDN to generate it. The received header data doesn't have CRLF. GET / HTTP/1.1 Accept: text/html, application/xhtml+xml, image/jxr, */* Accept-Language: en-IE User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393 Accept-Encoding: gzip, deflate Host: localhost Connection: Keep-Alive ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ GET /index.css HTTP/1.1 Accept: text/css, */* Referer: http://localhost/ Accept-Language: en-IE User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393 Accept-Encoding: gzip, deflate Host: localhost Connection: Keep-Alive ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠8|ø I wireshark or something like that?? Yeah I could give it a whirl. Generally the packets are formatted though it generally isn't a raw response. Maybe fiddler would work?? No I didn't I read them and https://msdn.microsoft.com/en-us/library/windows/desktop/ms740673(v=vs.85).aspx trying to figure stuff out and it isn't exactly the same as what I want anyway since it's C++
-
Like reading the Get requests turns out I had to use recv. Have a look. #include "stdafx.h" #include <io.h> #include <stdio.h> #include <stdlib.h> #include <dos.h> #include <winsock2.h> #pragma comment(lib,"ws2_32.lib") //Winsock Library bool send_all(int socket, void *buffer, size_t length) { char *ptr = (char*)buffer; while (length > 0) { int i = send(socket, ptr, length, 0); if (i < 1) return false; ptr += i; length -= i; } shutdown(socket, 1); return true; } int strpos(char *haystack, char *needle) { char *p = strstr(haystack, needle); if (p) return p - haystack; return -1; } int main(int argc, char *argv[]) { WSADATA wsa; SOCKET s, new_socket; struct sockaddr_in server, client; int c; char *message, server_reply[2000]; int recv_size; printf("\nInitialising Winsock..."); if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { printf("Failed. Error Code : %d", WSAGetLastError()); return 1; } printf("Initialised.\n"); //Create a socket if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { printf("Could not create socket : %d", WSAGetLastError()); } printf("Socket created.\n"); //Prepare the sockaddr_in structure server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(80); //Bind if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR) { printf("Bind failed with error code : %d", WSAGetLastError()); exit(EXIT_FAILURE); } puts("Bind done"); //Listen to incoming connections listen(s, 3); //Accept and incoming connection puts("Waiting for incoming connections..."); c = sizeof(struct sockaddr_in); while ((new_socket = accept(s, (struct sockaddr *)&client, &c)) != INVALID_SOCKET) { if ((recv_size = recv(new_socket, server_reply, 2000, 0)) == SOCKET_ERROR) { puts("recv failed"); } else { puts(server_reply); } //Receive a reply from the server if (strstr(server_reply, "GET / HTTP/1.1") != NULL) { message = "<!DOCTYPE html><html><head><link type='text/css' rel='stylesheet' href='index.css' /></head><body><a href='test'>Testing</a></body></html>"; send_all(new_socket, message, strlen(message)); } else if (strstr(server_reply, "index.css") != NULL) { message = "Content-Type: Text/Css"; send_all(new_socket, message, strlen(message)); message = "body{background:blue;}"; send_all(new_socket, message, strlen(message)); } else if (strstr(server_reply, "favicon.ico") != NULL) { message = " "; send_all(new_socket, message, strlen(message)); } else { message = ""; send_all(new_socket, message, strlen(message)); } } if (new_socket == INVALID_SOCKET) { printf("accept failed with error code : %d", WSAGetLastError()); return 1; } closesocket(s); WSACleanup(); return 0; } For the moment I am just using search strings. There is an error with mime types which cause errors to be thrown in the browser when I try to load an image or an external css stylesheet. I'm not sure how to set the headers properly.
-
Yeah I tried that too but it doesn't work 100% properly. The page only occasionally loads anything. //Reply to the client message = "<!DOCTYPE html><html lang='en'><head><link rel='stylesheet' href='css/test.css'/><title>test</title><meta charset='utf-8' /></head><body><div role='navigation' id='mySidenav' class='sidenav'><div id='split-bar'></div><nav id='navbar'><a>Home</a><a>Login</a><a>Register</a><a>Contact</a><a>Forgotten Password</a></nav></div></body></html>"; send(new_socket, message, strlen(message), 0); What I am sometimes getting back when I try to load the page in the browser. <html lang="en"><head><link href="css/test.css" rel="stylesheet"></head></html> So something is wrong somewhere. Maybe the formatting needs to be a certain way?? If it was totally wrong nothing at all would load. Hmm it appears to load in chrome but it is also continuously loading so maybe I need to close the connection to the client once the data is sent???? Edit: The following seems to work ok in microsoft edge. But it isn't working in any other browser. // socketprogramming4.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<io.h> #include<stdio.h> #include<winsock2.h> #pragma comment(lib,"ws2_32.lib") //Winsock Library int main(int argc, char *argv[]) { WSADATA wsa; SOCKET s, new_socket; struct sockaddr_in server, client; int c; char *message; printf("\nInitialising Winsock..."); if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { printf("Failed. Error Code : %d", WSAGetLastError()); return 1; } printf("Initialised.\n"); //Create a socket if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { printf("Could not create socket : %d", WSAGetLastError()); } printf("Socket created.\n"); //Prepare the sockaddr_in structure server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(80); //Bind if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR) { printf("Bind failed with error code : %d", WSAGetLastError()); exit(EXIT_FAILURE); } puts("Bind done"); //Listen to incoming connections listen(s, 3); //Accept and incoming connection puts("Waiting for incoming connections..."); c = sizeof(struct sockaddr_in); while ((new_socket = accept(s, (struct sockaddr *)&client, &c)) != INVALID_SOCKET) { puts("Connection accepted"); //Reply to the client message = "<!DOCTYPE html><html lang='en'><head><link rel='stylesheet' href='css/test.css'/><title>test</title><meta charset='utf-8' /></head><body><div role='navigation' id='mySidenav' class='sidenav'><div id='split-bar'></div><nav id='navbar'><a>Home</a><a>Login</a><a>Register</a><a>Contact</a><a>Forgotten Password</a></nav></div></body></html>"; send(new_socket, message, strlen(message), 0); closesocket(new_socket); } if (new_socket == INVALID_SOCKET) { printf("accept failed with error code : %d", WSAGetLastError()); return 1; } closesocket(s); WSACleanup(); return 0; } I think the socket may be closing too quickly for the other browsers maybe there is a way to make it wait until all the data has been sent before closing?? Edit: Okay so I found the function I needed which is shutdown(socket,1); which stops sending data but allows data to be received from the client. // socketprogramming4.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<io.h> #include<stdio.h> #include<stdlib.h> #include <dos.h> #include<winsock2.h> #pragma comment(lib,"ws2_32.lib") //Winsock Library bool send_all(int socket, void *buffer, size_t length) { char *ptr = (char*)buffer; while (length > 0) { int i = send(socket, ptr, length, 0); if (i < 1) return false; ptr += i; length -= i; } shutdown(socket, 1); return true; } int main(int argc, char *argv[]) { WSADATA wsa; SOCKET s, new_socket; struct sockaddr_in server, client; int c; char *message, server_reply[2000]; int recv_size; printf("\nInitialising Winsock..."); if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { printf("Failed. Error Code : %d", WSAGetLastError()); return 1; } printf("Initialised.\n"); //Create a socket if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { printf("Could not create socket : %d", WSAGetLastError()); } printf("Socket created.\n"); //Prepare the sockaddr_in structure server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(80); //Bind if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR) { printf("Bind failed with error code : %d", WSAGetLastError()); exit(EXIT_FAILURE); } puts("Bind done"); //Listen to incoming connections listen(s, 3); //Accept and incoming connection puts("Waiting for incoming connections..."); c = sizeof(struct sockaddr_in); while ((new_socket = accept(s, (struct sockaddr *)&client, &c)) != INVALID_SOCKET) { puts("Connection accepted"); //Reply to the client message = "<!DOCTYPE html><html lang='en'><head><title>test</title><meta charset='utf-8' /></head><body><div role='navigation' id='mySidenav' class='sidenav'><div id='split-bar'></div><nav id='navbar'><a>Home</a><a>Login</a><a>Register</a><a>Contact</a><a>Forgotten Password</a></nav></div></body></html>"; send_all(new_socket, message, strlen(message)); } if (new_socket == INVALID_SOCKET) { printf("accept failed with error code : %d", WSAGetLastError()); return 1; } closesocket(s); WSACleanup(); return 0; } Hmm now how do I get the request url so I can implement a filesystem?