|
Home ► Lab Exercise ► Network Lab Exercise Programs► Implementation Of Http Client▼
Implementation Of Http Client: |
Algorithm Steps:
Step 1: Start.
Step 2: Read the Ip, port ID and message from command line
Step 3: Create a socket with address family AF_INET type SOCK_STERAM and default protocol.
Step 4: Initialize the socket and set it attributes set the required port number.
Step 5: Connect to the server using connect () function to initialize the request.
Step 6: Repeat steps 6-8 until server sends bye
Step 7: Accept the client message and write it to the server using write function.
Step 8: If the client message is bye go to step 9.
Step 9: Read the message from the server using read() function. Display the message.
Step 10: stop.
C Program To Implement Http Client
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXBUF 1024
void PANIC(char *msg);
#define PANIC(msg) {perror(msg); abort();}
int main(int Count, char *Strings[])
{ int sockfd, bytes_read;
struct sockaddr_in dest;
char buffer[MAXBUF];
/*---Make sure we have the right number of parameters---*/
if ( Count != 3 )
PANIC("usage: testport \n");
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
PANIC("Socket");
/*---Initialize server address/port struct---*/
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(80); /*default HTTP Server port */
if ( inet_addr(Strings[1], &dest.sin_addr.s_addr) == 0 )
PANIC(Strings[1]);
/*---Connect to server---*/
if ( connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 )
PANIC("Connect");
sprintf(buffer, "GET %s HTTP/1.0\n\n", Strings[2]);
send(sockfd, buffer, strlen(buffer), 0);
/*---While there's data, read and print it---*/
do
{
bzero(buffer, sizeof(buffer));
bytes_read = recv(sockfd, buffer, sizeof(buffer), 0);
if ( bytes_read > 0 )
printf("%s", buffer);
}
while ( bytes_read > 0 );
/*---Clean up---*/
close(sockfd);
return 0;
}
SAMPLE INPUT OUTPUT:
$ gcc httpclient.c -o http.exe
$ http.exe 127.0.0.1:8080 hai
Msg: hai
|
|
⇓Student Projects⇓
⇑Student Projects⇑
|