#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <winsock2.h>
//Starts up WinSock
void host();
void join();
using namespace std;
int main(int argc, char *argv[])
{
int choice;
cout <<"Loading the game..." << endl;
cout << "would you like to host?" << endl;
cout << "1.Yes" << endl;
cout << "2.No" << endl;
cin >> choice;
if(choice == 1)
{
host();
}
else if (choice == 2)
{
join();
}
cout << "WELCOME 2 DA GAME!!! Well its actuially not here yet..." << endl;
cout << "This just tests the connection :)" << endl;
system("PAUSE");
return 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//Hosts the game
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
void host()
{
int error;
WSADATA wsaData;
error = WSAStartup(MAKEWORD(2, 0), &wsaData);
if (error != 0)
{
cout << "Couldn't Initialize Winsock: 1" << endl;
// unload WinSock
WSACleanup ();
return;
}
cout << "Winsock initialized..." << endl;
SOCKET connectSocket;
connectSocket = socket(AF_INET, SOCK_STREAM,0);
if (connectSocket == INVALID_SOCKET)
{
cout << "Couldnt create socket: 2" << endl;
// unload WinSock
WSACleanup ();
return;
}
cout << "Socket created..." << endl;
//This is the port it connects to, we are using 12521
const int port = 12521;
// the address structure for a TCP socket
sockaddr_in addr;
addr.sin_family = AF_INET; // Address family Internet
addr.sin_port = htons (port); // Assign port to this socket
addr.sin_addr.s_addr = htonl (INADDR_ANY); // No destination
if (bind(connectSocket, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
{
cout << "Couldn't bind socket: 3" << endl;
// unload WinSock
WSACleanup ();
return;
}
cout << "Socket binded..." << endl;
//Waits for the connection
cout << "Waiting for connection from other player..." << endl;
listen(connectSocket, 1);
//accepts the connection
accept(connectSocket, NULL, NULL);
cout << "Client accepted... SUCCESS!" << endl;
}
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//Joins the host
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~
void join()
{
int error;
int ipAddress;
WSADATA wsaData;
error = WSAStartup(MAKEWORD(2, 0), &wsaData);
if (error != 0)
{
cout << "Couldn't Initialize Winsock: 1" << endl;
// unload WinSock
WSACleanup ();
return;
}
cout << "Winsock initialized..." << endl;
//Takes the hosts IP address for the connecton
cout << "Please enter the hosts IP address: " << endl;
cin >> ipAddress;
//Creates socket
SOCKET connectSocket;
connectSocket = socket(AF_INET, SOCK_STREAM,0);
if (connectSocket == INVALID_SOCKET)
{
cout << "Couldnt create socket: 2" << endl;
// unload WinSock
WSACleanup ();
return;
}
cout << "Socket created..." << endl;
//This is the port it connects to, we are using 12521
const int port = 12521;
// the address structure for a TCP socket
sockaddr_in addr;
addr.sin_family = AF_INET; // Address family Internet
addr.sin_port = htons (port); // Assign port to this socket
addr.sin_addr.s_addr = htonl (ipAddress); // Hosts IP address
//Connects to the host
error = connect(connectSocket,(LPSOCKADDR)&addr,sizeof(struct sockaddr));
if (error != 0)
{
cout << "Couldn't connect to host: 3" << endl;
// unload WinSock
WSACleanup ();
return;
}
cout << "Connected" << endl;
}
cout << "Please enter the hosts IP address: " << endl;
cin >> ipAddress;
//Creates socket
SOCKET connectSocket;
connectSocket = socket(AF_INET, SOCK_STREAM,0);
if (connectSocket == INVALID_SOCKET)
{
cout << "Couldnt create socket: 2" << endl;
// unload WinSock
WSACleanup ();
return;
}
cout << "Socket created..." << endl;
//This is the port it connects to, we are using 12521
const int port = 12521;
// the address structure for a TCP socket
sockaddr_in addr;
addr.sin_family = AF_INET; // Address family Internet
addr.sin_port = htons (port); // Assign port to this socket
addr.sin_addr.s_addr = htonl (ipAddress); // Hosts IP address
//Connects to the host
error = connect(connectSocket,(LPSOCKADDR)&addr,sizeof(struct sockaddr));
if (error != 0)
{
cout << "Couldn't connect to host: 3" << endl;
// unload WinSock
WSACleanup ();
return;
}
cout << "Connected" << endl;
}