Original Post
I am new to networking programming. I cant get this simple server code to compile with my vc++ 6 compiler. the compiler gives me these errors: error C2065: 'socklen_t' : undeclared identifier error C2146: syntax error : missing ';' before identifier 'size' error C2065: 'size' : undeclared identifier error C2065: 'close' : undeclared identifier I believed socklen_t is a typedef of int. And after adding typedef int socklen_t; did fix the first errors. However, I cant seem to fix the last error with the close() function. I dont know what I did wrong. All I did was just copy and paste the code from an example. Thanks for your time!
#include <iostream>
#include <winsock.h>
using namespace std;
const unsigned short int port = 5432;
const int max_pending = 10;
const int max_len = 256;
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 1, 1 );
WSAStartup( wVersionRequested, &wsaData );
sockaddr_in address; //address
sockaddr_in client_address; //client address
char message[max_len];
int s;
int new_s;
int len;
//build address
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(port);
//setup passive open
if ((s=socket(PF_INET, SOCK_STREAM, 0)) < 0) {
cout<<"error in socket";
return 0;
}
//bind socket to address
if (bind(s, (sockaddr *)&address, sizeof(address)) < 0) {
cout<<"error in bind";
return 0;
}
if (listen(s, max_pending) < 0) {
cout<<"error in listen";
return 0;
}
//wait for connection, then receive message
socklen_t size;
while (1) {
if ((new_s = accept(s, (sockaddr *)&client_address, &size)) < 0) {
cout<<"error in accept";
return 0;
}
while (len = recv(new_s, message, sizeof(message), 0)) {
cout<<message<<"\n";
}
close(new_s);
}
}