Click here to Skip to main content
14,837,944 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<conio.h>
#include <stdio.h>
#include <stdlib.h>
#include<winsock2.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library
 
int main(int argc , char *argv[])
{

char *ptr;	
char *token = NULL;
 char *aa = "<head>";

 //ptr=(char*)malloc(30000*sizeof(aa));


    WSADATA wsa;
    SOCKET s;
    struct sockaddr_in server;
     char *message , server_reply[30000];
    int recv_size;
 
    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }
     
    printf("Initialised.\n");
     
    //Create a socket
    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }
 
    printf("Socket created.\n");
     
     
    server.sin_addr.s_addr = inet_addr("ip address" );
    server.sin_family = AF_INET;
    server.sin_port = htons( 80 );
 
    //Connect to remote server
    if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        puts("connect error");
               getch();
        return 1;
    }
     
    puts("Connected");
     
    //Send some data
    message ="GET  / HTTP/1.1\r\nHost: www.....com\r\nConnection: close\r\n\r\n' | ncwww.......com 80 ";
       
       if( send(s , message , strlen(message) , 0) < 0)
    {
        puts("Send failed");
        return 1;
    }
    puts("Data Send\n");
     
    //Receive a reply from the server
 
   if((recv_size = recv(s , server_reply , 20835, 0)) == SOCKET_ERROR)
    {
        puts("recv failed");
    }
     
    puts("Reply received\n");
 
    //Add a NULL terminating character to make it a proper string before printing
    server_reply[recv_size] = '\0';
    //puts(server_reply);
   

  
    token = strtok(server_reply, "\n");

	while (token) {
   
    token = strtok(NULL, "\n");
	
	if(strstr(token,aa) !=NULL)
	{
      printf("%s",token);
    
	}
	
    }
     getch();
     return 0;
	}
Posted
Updated 5-Oct-15 21:02pm
v2
Comments
Abhinav S 6-Oct-15 2:42am
   
Where is the exception? You could debug to figure out the exact location.
Member 11835269 6-Oct-15 3:15am
   
exception in if (strstr(token,aa)!=null);

Your code fails because strtok returns finally NULL and you are passing this null pointer to strstr. So add an additional check or break your while loop in this case:
C++
while (token) {
    token = strtok(NULL, "\n");
    // Break when no more new line characters
    if (NULL == token)
        break;
    if(strstr(token,aa) !=NULL)
    {
      printf("%s",token);
    }
}
   
Comments
Member 11835269 6-Oct-15 5:58am
   
thanks
For parsing HTML in C you best use some library. But there is a bunch of it but here is a comparison on wikipedia.

It mostly depends on your needs.
   
Comments
Member 11835269 6-Oct-15 3:12am
   
my that code i give u above also working but give me an exception on if(strstr()token,aa)!=NULL)
Albert Holguin 6-Oct-15 10:53am
   
when it comes to doing something trivial, common, but tedious.... use a library
Albert Holguin 6-Oct-15 10:54am
   
+5
Member 11835269 7-Oct-15 3:19am
   
every one use lib its not big deal before start any program every one add lib first
u moron

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900