Hey all,

I am pretty new to C and am trying to make a basic HTTP server to learn more about sockets. I managed to get the sockets working fine, and sent some data to the browser when I hardcoded the data. However I am now trying to read from a HTML file, append that data to some HTTP headers, and send that down the socket.

I cannot get it to send just the data, and it usually just sends a load of weird symbols.

��"h���K��"ȴ��`�!ȅ��䅍�$�"����p���v�!�����������������I",3,3��"��!��"���

Well the lines array just has space to store the contests of the file. It does not have space to store the header.
Secondly in C if you want to assign a string constant to an char array you want to do something of this sort
char arr[]="This is a constant sting";
Thirdly you have messed up the syntax of strcat on line 53. Should it not be the other way around

You could print the data before sending it across. This will tell you if there is a problem with the send or your string processing

Would it be best to have the headers in 1 variable, the file contents in another, then mash them both together in another variable that is given to the send() function?

Would it be best to have the headers in 1 variable, the file contents in another, then mash them both together in another variable that is given to the send() function?

It would be better if you learn how to declare and define variable types before attempting to do anything else more ambitious.

e.g.

char headers = "HTTP/1.1 200 OK\r\n"
                     "Date: Mon, 23 May 2005 22:38:34 GMT\r\n"
                     "Server: eoPanel/1.3.3.7 (Unix) (Ubuntu/Linux)\r\n"
                     "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n"
                     "Accept-Ranges: bytes\r\n"
                     "Content-Length: 438\r\n"
                     "Connection: close\r\n"
                     "Content-Type: text/html; charset=UTF-8\r\n\r\n";

Incorrect. variable header is a char; it can only hold one byte, that's it.

int i = 0;
    int count = 0;
    int ch;

   char lines[count];
  char headers = "HTTP/1.1 200 OK\r\n"
                   "Date: Mon, 23 May 2005 22:38:34 GMT\r\n"
                   "Server: eoPanel/1.3.3.7 (Unix)  (Ubuntu/Linux)\r\n"
                   "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n"
                   "Accept-Ranges: bytes\r\n"
                   "Content-Length: 438\r\n"
                   "Connection: close\r\n"
                   "Content-Type: text/html; charset=UTF-8\r\n\r\n";

All those variables are declared incorrectly. They should be declared on the beginning of the block, before any other statements. Furthermore, lines cannot have a variable subscript like count which it will be known only at run time. Compiler needs to know before that the fixed size. And I have told you already about headers.

Learn how to stand before walk; learn how to walk before run; be good at running before getting in front of a bull.

O_O - I'm going to be having words with my IT teacher, I was taught this was the correct way to declare strings :O!

Thanks for telling me that, im really quite annoyed that I was told incorrect ways of doing things -.-

O_O - I'm going to be having words with my IT teacher, I was taught this was the correct way to declare strings :O!

Maybe you misunderstood the teacher?
Anyway.

/* These three forms are equivalent */
char hi[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char hi[6] = "Hello"; /* the '\0' is hidden but it is there, must make space for it 6 */
char hi[] = "Hello"; /* compiler knows that there are 6 chars in that string */

That's different than:

/* equivalent */
char hi[3][6] = { "Hello", "World" }; /* two strings each with its own length */
char hi[][6] = { "Hello", "World" }; /* first dimension is not necessary, compiler knows by the second which is 6 */

/*Not equivalent */
char hi[2][] = { "Hello", "World" }; /* wrong */ 

/* second dimension must be enough to accommodate longest string */
char hi[][9] = { "Hello", "cruelest", "World!" }; /* cruelest is longest (9 chars) */
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.