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; If you read my whole post (sorry it's so long), you'll understand what my problem is now (my first problem ...

  1. #16
    Registered User
    Join Date
    May 2012
    Posts
    28
    If you read my whole post (sorry it's so long), you'll understand what my problem is now (my first problem was caused by gotos, but I removed some of those and it fixed it). Currently it will run, but I'm trying to get the server to send a string back to the client and it won't work. I'm not sure how I can fix this.

    I still have gotos in my program because I haven't removed the others yet, but I will later. Once my program can communicate successfully between the client and server, I'll go through my code again and organize it better, and remove all of the gotos, but before I do that I want to make the program do what I need it to.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Quote Originally Posted by Pikmeir
    Currently it will run, but I'm trying to get the server to send a string back to the client and it won't work. I'm not sure how I can fix this.
    Then answer my question: how does it not work?

    Also, while it is good that you made an attempt to indent your code, I suggest that your indent level be more than just a space. Two spaces per indent level would help make things clearer. (I prefer four spaces.)
    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

  3. #18
    Registered User
    Join Date
    May 2012
    Posts
    28
    I don't know how to send a string (called 'sString') to the client in this situation. Currently it sends 'char buffer[1024]' to the client:

    strcat_s(buffer, "MESSAGE");

    But after I send the buffer to secondaryCommand(sString), I need to send sString to the client.

    But 'strcat_s()' won't let me send a string, and it'll just give me an error if I try. Is there a different function I should use instead of this? Or is there a different way I should try to send sString to the client?

    I also tried changing sString to a char array (I think this is kind of the code I used), but it also didn't send correctly.
    char newbuffer[1024];
    newbuffer[1024] = sString[1024];
    strcat_s(newbuffer, "MESSAGE");

    So I realized that the problem is in secondaryCommand()'s return value. It returns 0, so nothing goes back to the main function. What do you think I should make the return value, if I need to send sString from secondaryCommand to main?

    also, I named it secondaryCommand because it's the 'secondary' to the main program (acts as a middleman between the C code in WinServer.cpp and C++ code in my other functions), not because it's a second function. So I agree that 'secondCommand' would be a very stupid name for a function

    And thanks for the tip about indentation. I'll indent four spaces instead from now on since it looks better.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Quote Originally Posted by Pikmeir
    But 'strcat_s()' won't let me send a string, and it'll just give me an error if I try. Is there a different function I should use instead of this?
    Instead of using strcat_s, use the interface of std::string.

    Quote Originally Posted by Pikmeir
    So I realized that the problem is in secondaryCommand()'s return value. It returns 0, so nothing goes back to the main function. What do you think I should make the return value, if I need to send sString from secondaryCommand to main?
    Ah, but this is related to my comment on how secondaryFunction is a useless name. and that assigning to sString in secondaryFunction could be a mistake. What is secondaryFunction supposed to do (besides acting as a "middleman")? Name it based on that.
    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

  5. #20
    Registered User
    Join Date
    May 2012
    Posts
    28
    secondaryFunction() only acts as a middleman, and nothing else. When it receives 'buffer', it will just call another function (I wrote some other functions in another file, but I'm not using them yet).
    For example, (I'm sure you already understand) if the client types 'say hello' it will send this as buffer to the server, the server will send it to secondaryCommand(), and secondaryCommand() will change it to a string and send it to another function that examines it, that other function will choose the reply and then send the reply to the main function to go to the client. Sorry it's my first client/server program, so it's simple but so frustrating (programming was so much easier before I tried adding networking^^). I just wish I had a tutor to walk me through it, but I can't hire anyone because I don't want to spend money, I can only teach foreign languages instead of payment :P

    Do you know what is the function for sending a string, instead of strcat_s? (I'm not 100% sure, but it seems strcat_s is the function that sends 'buffer' to the client.)

  6. #21
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    strcat_s stands for "string concatenation, secure" (where secure means that it protects against buffer overflow) and is used to concatenate one string to another. send and recv are the functions that send and receive data.
    Pikmeir likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #22
    Registered User
    Join Date
    May 2012
    Posts
    28
    Quote Originally Posted by oogabooga View Post
    strcat_s stands for "string concatenation, secure" (where secure means that it protects against buffer overflow) and is used to concatenate one string to another. send and recv are the functions that send and receive data.
    So if((bytecount = send(*csock, buffer, strlen(buffer), 0))==SOCKET_ERROR){ is the code that's actually sending the data to the client (meaning I've been looking at the wrong function)? If so, what do I have to do to get this function to send my string 'sString'?

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Quote Originally Posted by Pikmeir
    secondaryFunction() only acts as a middleman, and nothing else. When it receives 'buffer', it will just call another function (I wrote some other functions in another file, but I'm not using them yet).
    For example, (I'm sure you already understand) if the client types 'say hello' it will send this as buffer to the server, the server will send it to secondaryCommand(), and secondaryCommand() will change it to a string and send it to another function that examines it, that other function will choose the reply and then send the reply to the main function to go to the client.
    Ah. In that case, I'll just tell you that the problem is that you are passing the string by value. You want to pass by reference:
    Code:
    int secondaryCommand(string& sString)
    {
        cout << sString << endl; // just a temporary code to check and see if the Server can manipulate the data
        sString = "If you see this message on the client, then everything works!";
        return 0;
    }
    You need to be clear: are you using the std::string object or the char[1024] buffer? You have to pick one, not both, or you will only confuse yourself.

    Quote Originally Posted by Pikmeir
    If so, what do I have to do to get this function to send my string 'sString'?
    Something like this:
    Code:
    if ((bytecount = send(*csock, &sString[0], sString.length(), 0)) == SOCKET_ERROR) {
    Pikmeir likes this.
    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

  9. #24
    Registered User
    Join Date
    May 2012
    Posts
    28
    Aaah, that makes sense to use reference instead >.<

    I'd like to just use std::string object, and not buffer, since it'd be much easier to just work with strings instead of chars.

    edit: the reason I have buffer is because the code was originally written in C, and I know very little about C, so I didn't understand how to change the code. Instead, I just tried to make a new string (sString) that I could use. It's stupid, but it's the only way I could think of without knowing C.
    Last edited by Pikmeir; 05-02-2012 at 10:42 PM.

  10. #25
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by Cynic View Post
    I don't agree with this, but I will add that using a goto should be the absolute last resort when nothing else will work. I think I've used a goto once, ever.
    Ah, but I sneakily worded what I wrote such that I was only implying that this piece of code would be fine if the gotos were eliminated and never used again.
    In other words, should he use one some day later on in a different project, this project will still be fine.
    You caught on to what I intended to imply, but didn't want to explicitly state.

    However, for the record, I have never used a goto in production code ever, i.e. in the last 11 years, nor do I believe I would ever do so in future.

    Laserlight: The Microsoft networking code I remember seeing that had a goto in it had three, and one of them was to the next line, i.e. it did not skip anything.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #26
    Registered User
    Join Date
    Oct 2006
    Posts
    2,296
    even the oft-cited case of breaking from a nested loop can be handled without a goto. try/catch is a valid alternative, as is the carefully crafted use of if/else.

    the point being that your main() function still has all those goto lines, and they can (and should!) all be replaced with return 0;

    as for the cleanup in SocketHandler, replace the line
    Code:
    int *csock = (int*)lp;
    with
    Code:
    std::shared_ptr<int> csock(reinterpret_cast<int*>(lp);
    make sure to include <memory>, and then you can get rid of the delete, and simply return from any point where there was a goto.
    Pikmeir likes this.

  12. #27
    Registered User
    Join Date
    May 2012
    Posts
    28
    When I change it, I get a new error:

    1>WinServer.obj : error LNK2001: unresolved external symbol "int __cdecl secondaryCommand(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?secondaryCommand@@YAHV?$basic_string@DU?$char_tra its@D@std@@V?$allocator@D@2@@std@@@Z)

    I found a few other examples of it online, but couldn't figure out why my project would be getting this error too.

  13. #28
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    looks like you still have the function declaration
    Code:
    int secondaryCommand(string sString);
    somewhere in you code.
    Change it to use a reference as well.
    Kurt
    Pikmeir likes this.

  14. #29
    Registered User
    Join Date
    May 2012
    Posts
    28
    Yes!!!! Thank you everyone for your help! Everything works great now. I think I can handle it from here. If I get any more hiccups I'll post again here. Thanks!!

  15. #30
    Registered User
    Join Date
    May 2012
    Posts
    28
    Off-related question, but does anyone have any idea why my console program doesn't work (doesn't even open the console at all) on any other computer but my own?
    I'm running Windows Vista, and I tried testing it on a couple other machines with Vista installed, but neither could open the file.
    I'm guessing there must be some simple answer but I can't find it by searching Google. Is this a common problem?

Page 2 of 3 FirstFirst 123 LastLast
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