Network Programming FAQ
Why browser does’t render my HTML payload?
You payload should be closely follow the Response Header, for example
Connection: close\r\n\r\n<html>payload</html>
You must first read the request header before you write response data to the socket
How to detect EOF in a socket
Solution 1:
if you know you are working with HTTP protocal
1 | char * getheaderline(int socketfd){ |
Solution 2 (more generic)
Useful!!!
On common way is to use
ioctl(..)
to queryFIONREAD
of the socket which will return how much data is available.
1 | int len = 0; |
Solution 3 (recommended)
recv, recvfrom, recvmsg - receive a message from a socket
When a stream socket peer has performed an orderly shutdown, the return value will be 0 (the traditional “end-of-file” return).
1 |
|
Each client request must be responsed
Not matter what kind of request it is, GET
or POST
, the server should send a response back
Heap overflow
Run time error message
1 | free(): invalid next size (normal) in SomeKindOfFunction |
Quote from stackoverflow
It means that you have a memory error. You may be trying to
free
a pointer that wasn’t allocated bymalloc
(ordelete
an object that wasn’t created bynew
) or you may be trying tofree
/delete
such an object more than once. You may be overflowing a buffer or otherwise writing to memory to which you shouldn’t be writing, causing heap corruption.Any number of programming errors can cause this problem. You need to use a debugger, get a backtrace, and see what your program is doing when the error occurs. If that fails and you determine you have corrupted the heap at some previous point in time, you may be in for some painful debugging (it may not be too painful if the project is small enough that you can tackle it piece by piece).
Solution:
Write to heap memory with caution !!!
1 | typedef char byte |
This kind of bug is hard to detect and painful to debug, since the program will crash in next malloc
or free
rather than where the overflow happens !!!
For example
1 | strcpy(buffer , input); // overflow here, the program will continue running without crash |
1 | while(1){ |