Code:
int connecting::connecttoserver(unsigned short port, const char *servername)
{
int error;
WSAData wsaData; // the winsock data structure
if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) {
cout << "Winsock Start Up Failed!" << endl; // startup winsock
return -1;
}
int mySocket = socket(AF_INET, SOCK_STREAM, 0); // create my socket
if (mySocket == SOCKET_ERROR) {
cout << "Error Opening Socket!" << endl; // make sure nothing bad happened
return -1;
}
struct hostent *host_entry;
if ((host_entry = gethostbyname(servername)) == NULL) { // setup the host entry
cout << "Could not find host!" << endl;
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(port); // fill in the server socket info
server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;
if (connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) { // connect to the server
cout<< "Error connecting to server!" << endl;
}
cout << "Connection Established" << endl;
return mySocket;
}
thats the function, i just can't see where i am going wrong. prob been staring at the code too long