sockets - How to read extract parameters and payload from recv() in C? -


i running simple client code in c post , server , read data stream. printing full response received in recv() , have parse further use. before reading data stream, want know http status code (200, 400, 401 etc.) server. getting same in header. there way http code before reading headers?

what best practice read head parameters , payload? searched answers got read '\r\n' sequence , apply logic. there better way?

i using ubuntu systems testing code. following response being printed:

<p style="color:blue;">http/1.1 401 unauthorized   server: apache-coyote/1.1   content-type: application/json   content-length: 69   date: mon, 08 aug 2016 09:46:46 gmt   </p> <p style="color:blue;">"you not authorized user access resource. access denied." 

no. recv() receives raw data socket, tcp/ip in case suppose. tcp/ip lower level protocol , not have notion of http status code, http data bytes no meaning.

so have read data, interpret http protocol data, , status code it.


you should separate concerns of tcp/ip , http protocols. can either write simple http client library if want learn, or find existing library (libcurl comes mind) if need production quality code. use library handle http protocol handling, , hide lower level tcp/ip traffic application code.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -