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 ...

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.

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.)

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.

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.

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.

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.)

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.

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'?

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.

Originally Posted by Pikmeir

If so, what do I have to do to get this function to send my string 'sString'?

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.

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.

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?