Like Tree8Likes

Setting up server for game - initialization is skipped by 'goto'

This is a discussion on Setting up server for game - initialization is skipped by 'goto' within the C++ Programming forums, part of the General Programming Boards category; You may need to install the appropriate Visual C++ 2010 redistributable package on those other computers....

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    You may need to install the appropriate Visual C++ 2010 redistributable package on those other computers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    Registered User
    Join Date
    May 2012
    Posts
    28
    Aaah, is there any way to compile the program so that it will run on a computer that doesn't install it? I think my friends would prefer a larger file instead of having to install an extra package.

    EDIT: nevermind~ I played around with it and got it to work. Now it'll run on other computers, but the client doesn't connect to the server. I changed the IP address in the code to my current IP address, but I'm not sure if I have to change the port too...
    Last edited by Pikmeir; 05-04-2012 at 03:16 AM.

  3. #33
    Registered User
    Join Date
    Oct 2006
    Posts
    2,432
    in the project settings you can set it to use statically linked runtime libraries instead of dynamic. that will make it run on other computers without installing any extra packages.
    Pikmeir likes this.

  4. #34
    Registered User
    Join Date
    May 2012
    Posts
    28
    Yeah, that's exactly what I did and it worked. Unfortunately now I have to worry about how to make the other computer actually able to connect to my computer using IP (not as easy for me).

  5. #35
    Registered User
    Join Date
    May 2012
    Posts
    28
    EDIT: it works when I leave the IP address at 127.0.0.1 on the client, but if I change that to my actual IP address and put the client on another computer outside of my home network, it fails to connect to the server (note that the server is running when I try this).

    WinClient.cpp
    Code:
    #include <winsock2.h>
    #include <windows.h>
    #include <fcntl.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <stdio.h>
    #include <conio.h>
    #include <iostream> 
    int main(int argv, char** argc)
    {
        //The port and address you want to connect to
        int host_port= 1101;
        char* host_name="127.0.0.1"; // here's where I'm putting my IP address and trying to connect
       
        // Initialize socket support WINDOWS ONLY!
        unsigned short wVersionRequested;
        WSADATA wsaData;
        int err;
        wVersionRequested = MAKEWORD( 2, 2 );
        err = WSAStartup( wVersionRequested, &wsaData );
        if ( err != 0 || ( LOBYTE( wsaData.wVersion ) != 2 ||
          HIBYTE( wsaData.wVersion ) != 2 )) {
          fprintf(stderr, "Could not find useable sock dll %d\n",WSAGetLastError());
          return 0;
        }
        // Initialize sockets and set any options
        int hsock;
        int * p_int ;
        hsock = socket(AF_INET, SOCK_STREAM, 0);
        if(hsock == -1){
           printf("Error initializing socket %d\n",WSAGetLastError());
           return 0;
        }
        p_int = new int;
        *p_int = 1;
        if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )||
          (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){
          printf("Error setting options %d\n", WSAGetLastError());
          delete p_int;
          return 0;
       }
       delete p_int;
    
       //Connect to the server
       struct sockaddr_in my_addr;
       my_addr.sin_family = AF_INET ;
       my_addr.sin_port = htons(host_port);
       
       memset(&(my_addr.sin_zero), 0, 8);
       my_addr.sin_addr.s_addr = inet_addr(host_name);
       if( connect( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == SOCKET_ERROR ){
         fprintf(stderr, "*Error connecting* (Socket %d)\n", WSAGetLastError());
         return 0;
       }
    
       // Now let's do the client related stuff
       char buffer[1024];
       int buffer_len = 1024;
       int bytecount;
       int c;
       memset(buffer, '\0', buffer_len);
       for(char* p=buffer ; (c=_getch())!=13 ; p++){
          printf("%c", c);
          *p = c;
       }
       if( (bytecount=send(hsock, buffer, strlen(buffer),0))==SOCKET_ERROR){
           fprintf(stderr, "*Error sending data* (%d)\n", WSAGetLastError());
           return 0;
       }
       if((bytecount = recv(hsock, buffer, buffer_len, 0))==SOCKET_ERROR){
           fprintf(stderr, "*Error receiving data* (%d)\n", WSAGetLastError());
           return 0;
       }
       printf("\nReceived string \"%s\"\n", buffer);
       closesocket(hsock);
    ;
    }
    Last edited by Pikmeir; 05-04-2012 at 03:22 PM.

  6. #36
    Registered User
    Join Date
    Oct 2006
    Posts
    2,432
    on your calls to setsockopt, you don't need to use new to allocate your p_int variable. you can simply create a normal int variable and pass its address to setsockopt by using the address-of operator like so:

    Code:
    int p_int = 1;
    setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, &p_int, sizeof(int))
    the & symbol has multiple uses in C++. it can be used for bitwise and boolean logic, and it can be used to take the address of an object, or it can be used to indicate a reference type.

  7. #37
    Registered User
    Join Date
    May 2012
    Posts
    28
    I solved the problem by downloading the Portforward Static IP program, and changed my dynamic IP to a static IP (I also did port forwarding on my router, and left the IP in my code at my regular, home IP address), and everything works! I tried connecting with another computer and it's fine now. Woohoo!

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Setting up server socket?
    By aprop in forum Networking/Device Communication
    Replies: 1
    Last Post: 07-20-2010, 06:40 PM
  2. facing problems in setting up NFS server/client
    By kris.c in forum Tech Board
    Replies: 0
    Last Post: 12-19-2006, 09:12 PM
  3. Setting up 2D game (VIEW)
    By Deo in forum Game Programming
    Replies: 9
    Last Post: 06-08-2005, 03:54 PM
  4. Give me some opinions on setting up a server
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-19-2004, 10:38 AM
  5. Your favourite fantasy game setting?
    By fry in forum Game Programming
    Replies: 4
    Last Post: 10-16-2002, 06:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21