why doesn't this work:
PHP Code://skeleton server
#include <winsock.h>
#include <iostream.h>
#define PORTNUM 50000 //port number to be used
void DoSomething (SOCKET); //use this to do anything with socket
SOCKET Establish (unsigned short PortNum)
{
char *own_name ;
SOCKET ret_socket; //socket to be returned
sockaddr_in sa; //internet socket address
hostent *hp; //host info
memset (&sa, 0, sizeof (sockaddr_in)); //clear the address (mem space)
gethostname (own_name, sizeof (own_name)); //get local host name - our own
hp = gethostbyname (own_name); //retrieve address info
if (hp == NULL) //hopefully we do exist, and we wont get that error
return INVALID_SOCKET;
sa.sin_family = hp->h_addrtype; //our host address
sa.sin_port = htons (PortNum); //our port number
ret_socket = socket (AF_INET, SOCK_STREAM, 0); //create the socket
if (ret_socket == INVALID_SOCKET) //failed to create socket
return INVALID_SOCKET;
//now, we bind the socket to the internet address
if (bind (ret_socket, (sockaddr *) &sa, sizeof (sockaddr_in)) == SOCKET_ERROR) {
closesocket (ret_socket); //failed to do so, so get rid of the socket
return INVALID_SOCKET;
}
listen (ret_socket, 3); //make the socket listen
return ret_socket;
} //Establish
int main (int argc, char *argv[])
{
SOCKET my_socket; //main socket
if ((my_socket = Establish (PORTNUM)) == INVALID_SOCKET) { //plug in the phone
cout << "Establish: Failed to install socket" << endl;
return 0;
}
for ( ; ; ) { //loop for phone calls
SOCKET new_socket = accept (my_socket, NULL, NULL);
if (my_socket == INVALID_SOCKET) {
cout << "Error waiting for connection" << endl;
return 0;
}
DoSomething (new_socket);
closesocket (new_socket);
}
return 0;
}
void DoSomething (SOCKET sock)
{
//do thing with socket
}




Reply With Quote