C: Linux Socket Programming, TCP, a simple HTTP client -- page 2
Articles may may have files attached at the end of the post
Let's go over some sections from the source.
Line 38, we create the socket by calling a custom function: create_tcp_socket defined from line 117 to 125.
- int create_tcp_socket()
- {
- int sock;
- if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
- perror("Can't create TCP socket");
- exit(1);
- }
- return sock;
- }
In order to have a TCP socket, the domain has to be AF_INET for IPv4, the type of the socket is SOCK_STREAM in order to have a connection-oriented socket, and finally, the protocol is set to IPPROTO_TCP for TCP.
Then we call get_ip(), defined from line 128-145. get_ip takes a hostname as an argument and will attempt to convert it to a string representing its IP address.
- char *get_ip(char *host)
- {
- struct hostent *hent;
- int iplen = 15; //XXX.XXX.XXX.XXX
- char *ip = (char *)malloc(iplen+1);
- memset(ip, 0, iplen+1);
- if((hent = gethostbyname(host)) == NULL)
- {
- herror("Can't get IP");
- exit(1);
- }
- if(inet_ntop(AF_INET, (void *)hent->h_addr_list[0], ip, iplen ) == NULL)
- {
- perror("Can't resolve host");
- exit(1);
- }
- return ip;
- }
Let's look at this function a bit closer. First we allocate just enough characters to hold an IP address string. Then, we call gethostbyname, which on success return a non-NULL pointer to a struct of type hostent, which will hold all the aliases and network adresses (in network byte order). We then convert the first network address to a string by using inet_ntop and return the string.
Back to main, from line 41 to 53, we set the remote address to finally connect our socket to it on line 55.
Now, our socket is ready to receive or send packet.
Line 59, we build the HTTP query and send it from line 63 to 72. As there is no guarantee that the packet is sent in one go, we need to use a loop that will make sure that all the bytes are sent.
Line 77 to 97, we retrieve the reply from the server. Same here, we need to loop over as we might not receive all the bytes in one shot. This algo will fail to detect the beginning of the HTML content if the "\r\n\r\n" sequence in retrieve in 2 times. But anyway, this is good enough for the example :D.
Finally, we clean up the ressources we allocated manually.













ry chain. best swiss
ry chain.
best swiss replicas
Its 403 stores produced
replica Gucci 101G Steel Brown Mens Watch YA101312
$81 million in 1963,
A Lange & Sohne Replica Watches
with a net income of nearly $5 million. Diamonds continued
watches replica
to represent the largest
watch replica
share of Zale's sales,
cheap Harry Winston Ladys Quartz HW-3-470 watch replica
about $27 million. Operating manufacturing plants
ugg sale
in New York, Tel
cheap ugg UGG Womens Bailey Button sale
Aviv, and Puerto Rico,
cheap ugg UGG Womens Bailey Button sale
the company also operated a wholesale division, selling to
exact watch shop
other jewelry retailers. Zale
exact replica watch review
also made and sold
best replica watches
watches under its own Baylor's label, buying mechanisms
replica handbags
from Switzerland. The company
replica handbags
in 1962 had also
replica handbags
bought Bailey Banks & Biddle, a high-end jewelry
replica tiffany jewelry
retailer based in Philadelphia
Tiffany Bracelets Hot jewelry
that was founded in
replica jewelry
1832. By 1965, Zale found itself with
sockets
this is a good program