We are going to attempt a simple web client program. We request a webpage from "yahoo.com". Get the IP Address using ping command. It is "98.137.246.7". Use it in the program.
Regular steps involved in creating a socket:
Line 11 - Creating a socket. Stream Socket as HTTP involves TCP.
Line 22 - IP Address to be copied in the binary format
Line 28 - Connect to yahoo server with the port 80
Retrieving Data will be:
Line 35 - Send a HTTP Get packet to retrieve a web page
Line 46 - Loop to retrieve characters in the batches of 80
Line 52 - Carriage Return and New Line Characters has to be displayed as \r and \n
Close the connection:
Line 65 - Close the socket
When executed, Line Batch number and the Contents are displayed as
1 2 3 4 5 | [root@svtap01end1 work]# ping yahoo.com PING yahoo.com (98.137.246.7) 56(84) bytes of data. 64 bytes from media-router-fp1.prod1.media.vip.gq1.yahoo.com (98.137.246.7): icmp_seq=1 ttl=51 time=265 ms 64 bytes from media-router-fp1.prod1.media.vip.gq1.yahoo.com (98.137.246.7): icmp_seq=2 ttl=51 time=265 ms 64 bytes from media-router-fp1.prod1.media.vip.gq1.yahoo.com (98.137.246.7): icmp_seq=3 ttl=51 time=265 ms |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <stdio.h> #include <sys/socket.h> #include <arpa/inet.h> #include <string.h> int main(int argc, char const *argv[]) { int sock; // Create a socket if ((sock = socket(AF_INET,SOCK_STREAM,0))<0) { printf("socket creation error\n"); return -1; } struct sockaddr_in serv_addr; serv_addr.sin_family=AF_INET; serv_addr.sin_port=htons(80); // Convert IP Address in binary format if(inet_pton(AF_INET,"98.137.246.7",&serv_addr.sin_addr) <= 0) { printf("Address conversion to binary\n"); return -1; } // Connect client socket to the web server if(connect(sock,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) <0) { printf("Connection Error\n"); return -1; } // Request for a web page by sending HTTP Get Packet if(write(sock, "GET / HTTP/1.1\r\n\r\n",strlen("GET / HTTP/1.1\r\n\r\n")) != strlen("GET / HTTP/1.1\r\n\r\n")) { printf("Error write\n"); return -1; } char recvline[80]; memset(recvline,0,80); //Fill all 80 characters to 0 int i,readlen,linecounter=1; // Read the characters in batches of 80 while ((readlen=read(sock,recvline,80)) >0) { printf("%d -",linecounter); //Carriage Return and New Line Character has to be displayed as it is for (i=0; i<readlen; i++) { if (recvline[i] == '\n') { printf("\\n"); } else if (recvline[i] == '\r') { printf("\\r"); } else { printf("%c",recvline[i]); } } linecounter++; printf("\n"); } //Close the socket close(sock); printf("Successful creation"); return 0; } |
Regular steps involved in creating a socket:
Line 11 - Creating a socket. Stream Socket as HTTP involves TCP.
Line 22 - IP Address to be copied in the binary format
Line 28 - Connect to yahoo server with the port 80
Retrieving Data will be:
Line 35 - Send a HTTP Get packet to retrieve a web page
Line 46 - Loop to retrieve characters in the batches of 80
Line 52 - Carriage Return and New Line Characters has to be displayed as \r and \n
Close the connection:
Line 65 - Close the socket
When executed, Line Batch number and the Contents are displayed as
# gcc -o webclient webclient.c
# ./webclient 1 -HTTP/1.1 400 Host Header Required\r\nDate: Sun, 26 Apr 2020 16:48:32 GMT\r\nConnecti 2 -on: close\r\nServer: ATS\r\nCache-Control: no-store\r\nContent-Type: text/html\r\nConten 3 -t-Language: en\r\nX-Frame-Options: SAMEORIGIN\r\nContent-Length: 4383\r\n\r\n<!DOCTYPE h 4 -tml>\n<html lang="en-us">\n <head>\n <meta http-equiv="content-type" content="t 5 -ext/html; charset=UTF-8">\n <meta charset="utf-8">\n <title>Yahoo</title>\n 6 - <meta name="viewport" content="width=device-width,initial-scale=1,minimal-ui"> 7 -\n <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">\n <style>\n 8 - html {\n height: 100%;\n }\n body {\n background: 9 -#fafafc url(https://s.yimg.com/nn/img/sad-panda-201402200631.png) 50% 50%;\n 10 - background-size: cover;\n height: 100%;\n text-align: cente 11 -r;\n font: 300 18px "helvetica neue", helvetica, verdana, tahoma, arial, 12 - sans-serif;\n margin: 0;\n }\n table {\n height: 100%;\n 13 - width: 100%;\n table-layout: fixed;\n border-collapse: 14 - collapse;\n border-spacing: 0;\n border: none;\n }\n h1 15 - {\n font-size: 42px;\n font-weight: 400;\n color: #4000 16 -90;\n }\n p {\n color: #1A1A1A;\n }\n #message-1 {\n 17 - font-weight: bold;\n margin: 0;\n }\n #message-2 {\n 18 - display: inline-block;\n *display: inline;\n zoom: 1;\n 19 - max-width: 17em;\n _width: 17em;\n }\n </style>\n <script> 20 -\n \n </script>\n </head>\n <body>\n <!-- status code : 400 -->\n <!-- H 21 -ost Header Required -->\n <!-- host machine: media-router-fp1001.prod.media.gq1. 22 -yahoo.com -->\n <!-- timestamp: 1587919712.936 -->\n <!-- url: http:///-->\n <sc 23 -ript type="text/javascript">\n function buildUrl(url, parameters){\n var q 24 -s = [];\n for(var key in parameters) {\n var value = parameters[key];\n 25 - qs.push(encodeURIComponent(key) + "=" + encodeURIComponent(value));\n 26 - }\n url = url + "?" + qs.join('&');\n return url;\n }\n\n function 27 -generateBRBMarkup(site) {\n params.source = 'brb';\n generateBeaconMarku 28 -p(params);\n var englishHeader = 'Will be right back...';\n var englishM 29 -essage1 = 'Thank you for your patience.';\n var englishMessage2 = 'Our engin 30 -eers are working quickly to resolve the issue.';\n var defaultLogoStyle = '' 31 -;\n var siteDataMap = {\n 'default': {\n logo: 'https://s.yimg 32 -.com/rz/p/yahoo_frontpage_en-US_s_f_p_205x58_frontpage.png',\n logoAlt: 33 -'Yahoo Logo',\n logoStyle: defaultLogoStyle,\n header: englishHe 34 -ader,\n message1: englishMessage1,\n message2: englishMessage2\n 35 - }\n };\n\n var siteDetails = siteDataMap['default'];\n\n docume 36 -nt.write('<table><tbody><tr><td>');\n document.write('<div id="content">');\n 37 - document.write('<img src="' + siteDetails['logo'] + '" alt="' + siteDetail 38 -s['logoAlt'] + '" style="' + siteDetails['logoStyle'] + '">');\n document.wr 39 -ite('<h1 style="margin-top:20px;">' + siteDetails['header'] + '</h1>');\n do 40 -cument.write('<p id="message-1">' + siteDetails['message1'] + '</p>');\n doc 41 -ument.write('<p id="message-2">' + siteDetails['message2'] + '</p>');\n docu 42 -ment.write('</div>');\n document.write('</td></tr></tbody></table>');\n }\n 43 -\n function generateBeaconMarkup(params) {\n document.write('<img src="' 44 - + buildUrl('//geo.yahoo.com/b', params) + '" style="display:none;" width="0px" 45 -height="0px"/>');\n var beacon = new Image();\n beacon.src = buildUr 46 -l('//bcn.fp.yahoo.com/p', params);\n }\n\n var hostname = window.location.hos 47 -tname;\n var device = '-';\n var ynet = ('-' === '1');\n var time = new Da 48 -te().getTime();\n var params = {\n s: '1197757129',\n t: time,\n 49 - err_url: document.URL,\n err: '400',\n test: '-',\n ats_h 50 -ost: 'media-router-fp1001.prod.media.gq1.yahoo.com',\n rid: '-',\n m 51 -essage: 'Host Header Required'\n };\n\n if(ynet) {\n document.write('<d 52 -iv style="height: 5px; background-color: red;"></div>');\n }\n generateBRBMa 53 -rkup(hostname, params);\n\n </script>\n <noscript>\n <table>\n <tbody>\n <t 54 -r>\n <td>\n <div id="englishContent">\n <h1 style="margi 55 -n-top:20px;">Will be right back...</h1>\n <p id="message-1">Thank you 56 -for your patience.</p>\n <p id="message-2">Our engineers are working q 57 -uickly to resolve the issue.</p>\n </div>\n </td>\n </tr>\n 58 -</tbody>\n </table>\n </noscript>\n </body>\n</html>\n Successful creation
No comments:
Post a Comment