Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm getting these two errors! I'm using Visual Studio 2010 and Win 7.
IntelliSense: identifier is undefined
IntelliSense: a value of type cannot be used to initialize an entity of type

C++
#include <stdio.h>
#include <string.h>
//#include 
#include <stdafx.h> 
//#include 
//#include <netdb.h>
#include <winsock2.h>
#include <stdlib.h>
#include <ws2tcpip.h>

static const char *BODY = "SomeVariable1=foo&SomeVariable2=bar";
static const char *HEADERS = "POST /test.php HTTP/1.0\r\n"
"Host: 192.168.168.40\r\n"
"Accept: */*\r\n"
"Connection: keep-alive\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: ";

int main()
{
   struct addrinfo req, *res;
   memset(&req, 0, sizeof(req));
   req.ai_family = AF_UNSPEC;
   req.ai_socktype = SOCK_STREAM;

   if (getaddrinfo("192.168.168.40", "http", &req, &res) < 0)
   {
      perror("getaddrinfo");
      return -1;
   }

   int s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
   if (s < 0)
   {
      perror("socket");
      return -1;
   }
   if (connect(s, res->ai_addr, res->ai_addrlen) < 0)
   {
      perror("connect");
      close(s);
      return -1;
   }

   char *buffer = malloc(strlen(BODY) + strlen(HEADERS) + 256);
   sprintf(buffer, "%s%d\r\n\r\n%s\r\n\r\n", HEADERS, strlen(BODY), BODY);
   if (send(s, buffer, strlen(buffer), 0) < 0)
   {
      perror("send");
   }
   else
   {
      char recvbuffer[8192] = { 0 };
      int recvcount = 0;
      recvcount = recv(s, recvbuffer, sizeof(recvbuffer), 0);
      while (recvcount > 0)
      {
         printf("Received: %s", recvbuffer);
         recvcount = recv(s, recvbuffer, sizeof(recvbuffer), 0);
      }

      printf("\n");
   }
   free(buffer);
   close(s);
   return 0;
}
Posted
Updated 15-Aug-11 15:38pm
v3
Comments
Sergey Alexandrovich Kryukov 15-Aug-11 21:22pm    
This is not how an issue should be reported. Clearly indicate the lines where you have the error.
--SA
Member 7766180 15-Aug-11 21:33pm    
This line for the void error
char *buffer = malloc(strlen(BODY) + strlen(HEADERS) + 256);

and the close(s)
First I believe my problem is that this is in C (I Think) and I'm doing C++.
Member 7766180 15-Aug-11 21:34pm    
a value of type "void *" cannot be used to initialize an entity of type "char *"
identifier close is undefined
mbue 15-Aug-11 21:58pm    
you have answerd your own question ;).

char *buffer = (char*)malloc(strlen(BODY) + strlen(HEADERS) + 256);
//...
closesocket(s);
Member 7766180 15-Aug-11 22:18pm    
Thank you so much! It worked!

1 solution

I think you're missing the typecast char* buffer = (char*)malloc(...);
 
Share this answer
 

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