4 Attachment(s)
Setting up server for game - initialization is skipped by 'goto'
Attachment 11639Attachment 11640Attachment 11641Attachment 11642
Hi, I'm currently altering the following tutorial files for my own use (Tidy Tutorials: Windows C++ Socket Example Using Client Server and Multi-Threading) using VC++ 2010 Express.
I've got them to run as the tutorial shows fine, and went ahead and changed a couple of things. Everything works so far, but the next thing I'd like to do is get the message sent from the client (char buffer, I believe) to be changed into a string and communicated to another function, called secondaryCommand(), so that I can work with it further and incorporate it into the game. I can get WinServer.cpp to send basic integers to secondaryCommand(), but I can't get it to send the string it receives from the client :( I haven't eaten for 5 hours just trying to fix this, so I hope someone here knows what's going on.
Here's the error I'm getting:
Code:
1> WinServer.cpp
1> WinServer.cpp(152): error C2362: initialization of 'sString' is skipped by 'goto FINISH'
1> WinServer.cpp(140) : see declaration of 'sString'
1> WinServer.cpp(152) : see declaration of 'FINISH'
Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
Here's all of my code. Sorry it's kind of long. There are 3 pieces (Linkerfile.h, Secondary.cpp, and WinServer.cpp). I've highlighted the area in WinServer.cpp that's giving me a compiler error in blue.
I also attached all of the files to the top of this, including WinClient (if you'd like to try it out yourself and see).
Linkerfile.h
Code:
#ifndef LINKERFILE_H
#define LINKERFILE_H#include <string>
using namespace std;
int secondaryCommand(string sString);
#endif
Secondary.cpp
Code:
// Used as a middleman from the game to Winserver.cpp
#include "linkerfile.h"
#include <string>
#include <fstream>
#include <istream>
#include <iostream>
using namespace std;
int secondaryCommand(string sString)
{
cout << "If you see WORKS! below then it works." << endl; // actually it won't say "WORKS!" it'll just show the buffer
cout << sString << endl;
return 0;
}
WinServer.cpp
Code:
#include <winsock2.h>
#include <windows.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string>
#include "linkerfile.h"
DWORD WINAPI SocketHandler(void*);
int serverPasswordCheck()
{
char serverPass[256],
*guess = "pass\n"; // Change the password here
puts("Enter your password: " );
while(fgets(serverPass, 256, stdin) != NULL )
{
if(strcmp(serverPass, guess) == 0 )
{
puts("Password is correct. Initializing server.");
break;
}
else
{
puts( "Password is incorrect. Please try again.");
}
}
return 0;
}
int welcomeMessage()
{
static int x = 1;
if(int x = 1)
{ printf("This is the server program for Leujor RPG.\n"); }
x--;
serverPasswordCheck(); // checks a password before going back to main()
return 0;
}
int main(int argv, char** argc){
//Runs only once
welcomeMessage();
//The port you want the server to listen on
int host_port= 1101;
//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());
goto FINISH;
}
//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());
goto FINISH;
}
p_int = (int*)malloc(sizeof(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());
free(p_int);
goto FINISH;
}
free(p_int);
//Bind and listen
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 = INADDR_ANY ;
if( bind( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == -1 ){
fprintf(stderr,"Error binding to socket, make sure nothing else is listening on this port %d\n",WSAGetLastError());
goto FINISH;
}
if(listen( hsock, 10) == -1 ){
fprintf(stderr, "Error listening %d\n",WSAGetLastError());
goto FINISH;
}
//Now lets go to the server stuff
int* csock;
sockaddr_in sadr;
int addr_size = sizeof(SOCKADDR);
while(true){
printf("*Ready to receive more players*\n");
csock = (int*)malloc(sizeof(int));
if((*csock = accept( hsock, (SOCKADDR*)&sadr, &addr_size))!= INVALID_SOCKET ){
printf("Received connection from %s \n",inet_ntoa(sadr.sin_addr));
CreateThread(0,0,&SocketHandler, (void*)csock , 0,0);
}
else{
fprintf(stderr, "Error accepting %d\n",WSAGetLastError());
}
}
FINISH:
;
}
DWORD WINAPI SocketHandler(void* lp)
{
int *csock = (int*)lp;
char buffer[1024];
int buffer_len = 1024;
int bytecount;
memset(buffer, 0, buffer_len);
if((bytecount = recv(*csock, buffer, buffer_len, 0))==SOCKET_ERROR){
fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
goto FINISH;
}
printf("From username: \"%s\"\n", buffer); // this will display their username soon
std::string sString = buffer;
// Send the data to another .cpp file (Secondary.cpp)
secondaryCommand(sString);
strcat_s(buffer, " SERVER ECHO");
if((bytecount = send(*csock, buffer, strlen(buffer), 0))==SOCKET_ERROR){
fprintf(stderr, "Error sending data %d\n", WSAGetLastError());
goto FINISH;
}
FINISH:
free(csock);
return 0;
}
Note: you'll need to add Ws2_32.lib for it to work, although I'm pretty sure you already know that. Sorry I'm pretty new to C++ and this is my first attempt at networking a program with a purpose. Thank you so much for helping (or just reading even).