Send a tcp packet and then get the ip header of the received packet on Windows

Tag: windows , sockets , winpcap Author: zhaohaimeng170 Date: 2014-01-24

I want to send a TCP packet ( a HTTP Get, for example. ) with send() or something like that,and then I can get the message returned by the server ("HTTP/1.1 200 OK....", for example) through the recv() function.
But now I want to read the ip header of the packet received from the server.

My solution (Here is the code.) now is to send the packet with a normal socket, such as socket(AF_INET, SOCK_STREAM, 0), and then use the winpcap to sniff the packages with filters to specific the source, destination and protocol.

But this solution is rather slow. When sniffing, the pcap_next_ex(adhandle, &header, &p)function will block for 1 second.

Can I do this with the raw socket? On windows, you can't send a tcp packet with raw socket.But I don't want to costom the sending packet, I just want to read the ip header of the response packet from the server.

My English is bad, if you do not understand my question, please let me know.

Your general solution (socket + separate pcap) looks good. I'd say it is a problem to trust the timing (getting a captured packet right after send()). I'd try to have the sniffer in working state before connect(). You will have to distinguish between handshake and data packets, but you are sure to get the packet you want, fast.
@epx :Thanks a lot. Actually I run into another question now. If I want to change the TCP window size, Do I have to use the raw socket(change the windows size part in tcp header)?Is there any easier way?
You should ask other questions as separate questions. They are sure to get more attention and a higher chance of getting a good answer.