Advertisement
Guest User

Network code

a guest
Feb 15th, 2012
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.39 KB | None | 0 0
  1. #include "Networking.h"
  2. #include "DialogBox.h"//for the dialogue box which the user enters the IP address into (contains char ipAddress[16] global)
  3.  
  4. #include "CORE LIB/Window.h"
  5. extern window_struct window;
  6. //sort out these globals later
  7. int port = 27015;
  8. int num_clients = 0;
  9. bool client = false;
  10. bool host = false;
  11. bool networking = false;
  12. Socket network;
  13. //these two things are the only things that need to be externally available
  14.     bool transmit = false;
  15.     client_data_struct client_data[MAX_CLIENTS+1];
  16.  
  17. void WSAInit()
  18. {
  19.     if(WSAStartup( MAKEWORD(2, 2), &network.wsaData ) != NO_ERROR){
  20.         printf("Socket Initialization: Error with WSAStartup\n");
  21.         PostQuitMessage(0);
  22.         WSACleanup();
  23.         return;
  24.     }
  25. }
  26. //handle socket data
  27. Socket::Socket()
  28. {
  29.     WSAInit();
  30. }
  31. Socket::~Socket()
  32. {
  33.     WSACleanup();
  34. }
  35. bool Socket::init_socket()
  36. {
  37.     mySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  38.     if (mySocket == INVALID_SOCKET){
  39.         printf("Socket Initialization: Error creating socket");
  40.         PostQuitMessage(0);
  41.         WSACleanup();
  42.         return 0;
  43.     }
  44.     //set socket option tcp_nodelay to stop packets being delayed and sent out as bigger packets (really messes up the reading)
  45.     int flag = 1;
  46.     int result = setsockopt(mySocket, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
  47.     if (result < 0){return 0;}
  48.     return 1;
  49. }
  50. bool Socket::StartHosting(int port)//server bind port
  51. {
  52.     myAddress.sin_family = AF_INET;
  53.     myAddress.sin_port = htons(port);
  54.     myAddress.sin_addr.s_addr = htonl(INADDR_ANY);//inet_addr( "0.0.0.0" );
  55.  
  56.     if (bind(mySocket, (SOCKADDR*)&myAddress, sizeof(myAddress) ) == SOCKET_ERROR){
  57.         printf("Unable to bind socket\n");
  58.         WSACleanup();
  59.         WSAInit();
  60.         return 0;
  61.     }
  62.     if (listen(mySocket, MAX_CLIENTS) == SOCKET_ERROR){
  63.         printf("Unable to listen to socket\n");
  64.         WSACleanup();
  65.         WSAInit();
  66.         return 0;
  67.     }
  68.     return 1;
  69. }
  70. bool Socket::ConnectToServer(const char *ipAddress, int port)//client connect
  71. {
  72.     myAddress.sin_family = AF_INET;
  73.     myAddress.sin_port = htons(port);
  74.     myAddress.sin_addr.s_addr = inet_addr(ipAddress);
  75.  
  76.     if (myAddress.sin_addr.s_addr == INADDR_NONE){printf("Invalid IP!\n");return 0;}
  77.  
  78.     if (connect(mySocket, (LPSOCKADDR)&myAddress, sizeof(myAddress) ) == SOCKET_ERROR){
  79.         int Error = WSAGetLastError();
  80.         if (Error==WSAEWOULDBLOCK){
  81.             printf("Connection requested waiting for response...\n");
  82.             return 0;
  83.         }
  84.         printf("Connection failed: %d\n",Error);
  85.         closesocket(mySocket);
  86.         WSACleanup();
  87.         return 0;
  88.     }
  89.     //connected on first try
  90.     printf("Connected (YES!)!\n");
  91.     return 1;
  92. }
  93.  
  94. //
  95. void connect(int set_port)
  96. {
  97.     if (host || client){
  98.         if (MessageBox( NULL, "You must disconnect first.\nDo you want to disconnect now and connect to a new host?","Disconnect and Continue?", MB_YESNO | MB_ICONINFORMATION)==IDYES){ disconnect(); }
  99.         else{return;}
  100.     }
  101.     //open dialogue box, user inputs ipaddress
  102.     if ( DialogBox(window.hInstance, MAKEINTRESOURCE(IDD_DIALOG1), window.hWnd, (DLGPROC)IP_DlgProc) != IDOK ){return;}
  103.     //check if IP address (ipAddress) is valid
  104.  
  105.     //init and attempt to connect
  106.     if (!network.init_socket()){return;}
  107.     if (set_port != port){port=set_port;}
  108.     printf("Attempting to connect to IP %s, on port %d\n", ipAddress, port);
  109.     WSAAsyncSelect(network.mySocket, window.hWnd, WM_SOCKET, (FD_READ | FD_CONNECT | FD_CLOSE));
  110.     if (!network.ConnectToServer(ipAddress, port)){return;}
  111.     client = true;
  112.     networking = true;
  113. }
  114. void connected(int Error)
  115. {
  116.     if (Error != 0){
  117.         //possible errors: WSAEAFNOSUPPORT WSAECONNREFUSED WSAENETUNREACH WSAEFAULT WSAEINVAL WSAEISCONN WSAEMFILE WSAENOBUFS WSAENOTCONN WSAETIMEDOUT
  118.         printf("Connection errror.");
  119.         PostQuitMessage(0);
  120.         WSACleanup();
  121.         return;
  122.     }
  123.     client = true;
  124.     networking = true;
  125.     printf("Connected!\n");
  126. }
  127. void disconnect()
  128. {
  129.     if (host){
  130.         for (int id=0; id<num_clients; id++){
  131.             char buffer[1] = {1};
  132.             send(client_data[id].socket, buffer, 18, 0);
  133.             shutdown(client_data[id].socket, SD_BOTH);
  134.             closesocket(client_data[id].socket);
  135.         }
  136.         shutdown(network.mySocket, SD_BOTH);
  137.         closesocket(network.mySocket);
  138.         network.init_socket();
  139.         num_clients = 0;
  140.         printf("Stopped hosting!\n");
  141.     }
  142.     else if (client){
  143.         shutdown(network.mySocket, SD_BOTH);
  144.         closesocket(network.mySocket);
  145.         network.init_socket();
  146.         printf("Disconnected!\n");
  147.     }
  148.     else{printf("Not connected!\n");}
  149.     networking = false;
  150.     client = false;
  151.     host = false;
  152. }
  153.  
  154. void host_server(int set_port)
  155. {
  156.     if (host || client){
  157.         if (MessageBox( NULL, "You must disconnect first.\nDo you want to disconnect now and connect to a new host?","Disconnect and Continue?", MB_YESNO | MB_ICONINFORMATION)==IDYES){ disconnect(); }
  158.         else{return;}
  159.     }
  160.     //init and start hosting
  161.     if (!network.init_socket()){return;}
  162.     if (set_port != port){port=set_port;}
  163.     if (!network.StartHosting(port)){return;}
  164.     printf("Hosting on port: %d\n", port);
  165.     WSAAsyncSelect(network.mySocket, window.hWnd, WM_SOCKET, (FD_READ | FD_ACCEPT | FD_CLOSE));
  166.     host = true;
  167.     networking = true;
  168. }
  169. void accept_connection()
  170. {
  171.     int addr_size = sizeof(sockaddr);
  172.     client_data[num_clients].socket = accept(network.mySocket, &client_data[num_clients].socketADDR, &addr_size);
  173.     if (client_data[num_clients].socket==INVALID_SOCKET){printf("Unable to accept connection!\n");return;}
  174.     printf("Client Connected!\n");
  175.     num_clients++;
  176. }
  177. void close_connection(int socket_id, int Error)
  178. {
  179.     if (Error != 0){
  180.         //possible errors: WSAENETDOWN WSAECONNRESET WSAECONNABORTED
  181.         printf("Connection errror.");
  182.         PostQuitMessage(0);
  183.         WSACleanup();
  184.         return;
  185.     }
  186.     if (client){
  187.         printf("Host Disconnected!\n");
  188.         disconnect();
  189.         return;
  190.     }
  191.     //host: close client socket
  192.     int s_id = 0;
  193.     for (int id=0; id<num_clients; id++){
  194.         if (int(client_data[id].socket) == socket_id){s_id=id;}
  195.     }
  196.     printf("Client (%d) Disconnected!\n",s_id);
  197.     shutdown(client_data[s_id].socket, SD_BOTH);
  198.     closesocket(client_data[s_id].socket);
  199.     //re-jiggle the clients
  200.     for (int id=s_id+1; id<num_clients; id++){
  201.         client_data[id] = client_data[id-1];
  202.     }
  203.     num_clients--;
  204. }
  205.  
  206. void recieve_data(int socket_id)
  207. {
  208.     char buffer[64];
  209.     memset(&buffer,0,64);
  210.     recv(socket_id, buffer, 64, 0);
  211.     // [0] *message id*, [1] *size  high bits*, [2] *size  low bits*, [3] *data*...
  212.     string message = (string)buffer;
  213.     if (message[0] == 1){//
  214.     }
  215.     if (message[0] == 2){//
  216.     }
  217.     if (message[0] == 3){//sync data
  218.         //recieve a load of data
  219.         //used for when a client connects etc
  220.     }
  221.     if (message[0] == 4){//
  222.     }
  223.     return;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement