I'm having difficulty compiling the C program below, It's just the begining of me trying to understand winsock. The issue is that when compiling the program client.c, I get an error (C2143) missing ';' before 'type' But when I re-name the source file to 'client.cpp' the program compiles with no errors or warnings. I don't understand the syntax error that is an error in C but not C++.
#define WIN32_LEAN_AND_MEAN
#define DEBUG
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define PORT "12186"
#define BUFFERLEN 512
int main(int argc, char* argv[])
{
/*
Variable Declorations
*/
WSADATA wsaData;
SOCKET ConnectionSocket = INVALID_SOCKET;
struct addrinfo *result = NULL, *ptr = NULL, hints;
int addrResult;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC; //unspecified so we can be compatible with IPv4 and IPv6
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
#ifdef DEBUG
printf("IPPROTO_TCP: %d", IPPROTO_TCP);
#endif
//Buffers
char * sendbuffer; // Error C2143
char recievebuffer [BUFFERLEN]; //Error C2143
//Initialize Winsock
addrResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if(addrResult !=0)
{
printf("WSAStartup failed: %d", addrResult);
}
addrResult = getaddrinfo(argv[1], PORT, &hints, &result);
if(addrResult != 0)
{
printf("getaddrinfo failed: %d", addrResult);
WSACleanup();
return 1;
}
return 0;
}
Edit: C variable declorations have to go before all other code in MSVC C functions. Problem solved. Is this a C89 thing or is it just MSVC?