Using sockets to receive the client TCP , UDP and address which will allow a flow through the database connection.
I used it using my compiler and it works fine but I haven't added all the functions yet that is needed like "change face" change race and more.
Tell me if it works for you.
I used it using my compiler and it works fine but I haven't added all the functions yet that is needed like "change face" change race and more.
Tell me if it works for you.
- This Admin Panel Includes:
- Serach for characters
- Manage Accoutns
- Mangage Server
- Ban / Ip Ban
- & Logout
Code:
#include <windows.h>
#include <winsock2.h>
#include <stdlib.h>
#include <iostream>
#include <cmath>
#include <fstream>
#include <winsock2.h>
using namespace std;
int main()
{
// Some other standard libraries i'll use
int main()
{
cout << "Part 1: Creating a Socket!\n";
cout << "Server Creation! \n\n";
int error; // Used to return any errors found. Only needed for debugging purposes.
WSAData wsaData; // This holds information about our network, we wont use this really, its mostly there for completeness
error = WSAStartup(MAKEWORD(2, 2), &wsaData); // Make a call to start up winsock, MAKEWORD simply tells the version we are going to us. Version 2.2 for us
if (error == SOCKET_ERROR) // Almost EVERY Winsock function will reutrn a number on error, so we should always check for errors
{
cout << "Server: We Errored Starting Winsock!!!\n";
return 1;
}
cout << "Server: WinSock Successfully Started.\n"; // Good so far!
int mySocket; // We will use this one to talk to another computer
mySocket = socket(AF_INET, SOCK_STREAM, 0); // We make it. Just know, AF_INET means set up for the internet, SOCK_STREAM means a streaming socket (connection),
// you could aslo have used SOCK_DGRAM (connectionless). But that causes alot fo problems, so we wont use it.
// The 0 is there for a extra qualifier, we WONT use this, we just pass 0 to complete it
if (mySocket == SOCKET_ERROR)
{
cout << "Server: Error Something Bad Happened!\n";
}
cout << "Server: Doing Good...\n";
closesocket(mySocket); // Now we need to close our socket.
cout << "Server: Socket Closed!\n";
WSACleanup(); // This is called to shut down the dll we loaded earlier. If we dont do this, we get memory leaks, not pretty.
cout << "Server: WinSock shut down successfully!\n";
return 0;
}
SetConsoleTitle("Server Admin Panel");
int option;
char username[30];
char password[30];
using namespace std;
int main()
{
cout <<"Server: Part 2a\n";
cout <<"SERVER LOGIN\n\n";
int error;
WSAData wsaData;
error = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (error == SOCKET_ERROR)
{
cout << "Server: Winsock Failed to begin!\n";
return 1;
}
cout << "Server: WinSocket Started Correctly!\n";
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
if (mySocket == SOCKET_ERROR)
{
cout << "Server: Socket Failed to initialize!\n";
}
cout << "Server: Socket Opened Properly!\n";
struct sockaddr_in server; // New! Our network socket structure
server.sin_family = AF_INET; // Always have ti as this for internet
server.sin_port = htons(7654); // Dont forget the byte ordering
server.sin_addr.s_addr = INADDR_ANY; // Lets us accept a conenction on ANY internet
error = bind(mySocket, (sockaddr*)&server, sizeof(server)); // Attempt to bind the socket
if (error == SOCKET_ERROR)
{
cout << "Server: Ah ****... Somethings wrong with binding\n";
}
error = listen(mySocket, 5); // Listen for connections
if (error == SOCKET_ERROR)
{
cout << "Server: Too deaf to listen...\n"; // Did we error?!?!
}
cout << "Server: Waiting for a client to connect ...\n"; // Just to keep us up to date - ish
int clientSocket; // Used for the client
clientSocket = accept(mySocket, 0, 0); // Accepts the client
if (clientSocket == SOCKET_ERROR)
{
cout << "I DUN WANNA!\n"; // did we Fu** up again?
}
cout << "Server: Client Connected!\n";
closesocket(mySocket);
cout << "Server: Socket Closed\n";
WSACleanup();
cout << "Server: WinSocket Shutdown\n";
cout << "Server: Press any key to shutdown server ...";
getchar();
return 0;
}
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t \tType [1] to Register To Create A new Account!\n\t \tType [2] to Login To Get into your previous account.|\n";
cout<<"\t+-----------------------------------------------------------+\n";
//Below User Input!
cout <<"\n\tOption: ";
cin >> option;
//The If Statements!
if ( option == 2 ) {
system("cls");
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t|Login to your account!|\n";
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t| Username: ";
cin >> username;
cout<<"\t| |\n";
cout<<"\t| Password: ";
cin >> password;
cout<<"\t+-----------------------------------------------------------+\n";
}
else if ( option == 1 ) {
system("cls");
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t|Register your new account!|\n";
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t| Username: ";
cin >> username;
cout<<"\t| |\n";
cout<<"\t| Password: ";
cin >> password;
cout<<"\t+-----------------------------------------------------------+\n";
}
else {
cout <<"Incorrect Syntax, Try Again!";
}
//Creating The Save Profile!
{
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t|Saving Profile.... Please Wait!|\n";
cout<<"\t+-----------------------------------------------------------+\n";
char data[100];
ofstream data_file ("AdminFile.txt");
data_file << "Your Account User: " << username << "\nYour Account Password: " << password << endl;
data_file.close();
ifstream b_file ("AdminFile.txt");
b_file >> data;
cout << data << endl;
system("pause");
}
{
int home;
system("cls");
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t|Your account as been created, You may now login.|\n";
cout<<"\t+-----------------------------------------------------------+\n";
cout <<"\n\nType [1] to return the to main menu.\n";
cout <<"Selection: ";
cin >> home;
if ( home == 1 ) {
system("cls");
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t \tType [1] to Register To Create A new Account!\n\t \tType [2] to Login To Get into your previous account.|\n";
cout<<"\t+-----------------------------------------------------------+\n";
}
//Below User Input!
cout <<"\n\tOption: ";
cin >> option;
if ( option == 2 ) {
system("cls");
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t|Login to your account!|\n";
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t| Username: ";
cin >> username;
cout<<"\t| |\n";
cout<<"\t| Password: ";
cin >> password;
cout<<"\t+-----------------------------------------------------------+\n";
system("cls");
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t|Logging into " << username << " Please Wait""!|\n";
cout<<"\t+-----------------------------------------------------------+\n";
}
else if ( option == 1 ) {
system("cls");
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t|Register your new account!|\n";
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t| Username: ";
cin >> username;
cout<<"\t| |\n";
cout<<"\t| Password: ";
cin >> password;
cout<<"\t+-----------------------------------------------------------+\n";
}
else {
cout <<"Incorrect Syntax, Try Again!";
}
}
//Enterting The Panel
char name[30];
int selection;
system("cls");
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t|\t Gunz Admin Panel 2010 !!|\n";
cout<<"\t+-----------------------------------------------------------+\n";
{
cout <<"\n\t\t\t\t\tAdmin Panel Status: ";
HANDLE font;
font = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(font,FOREGROUND_GREEN);
cout <<" Online";
}
{
cout <<"\n[1] - Search\n[2] - Manage Accounts\n[3] - Manage Characters\n[4] - Manage Clans\n\n\n[5] - Logout";
cout <<"\n\nSelection: ";
cin >> selection;
if ( selection == 1 ) {
system("cls");
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t|\t Character Name: ";
cin >> name;
cout<<"\t+-----------------------------------------------------------+\n\n";
cout << "Name: " << name << endl;
cout <<"Level: 60\n";
cout <<"Experience Points: 496049314\n";
cout <<"Bounty: 24170423\n";
cout <<"Clan: Raptors\n";
cout <<"Finished Gathering All information.\n\n\n";
system("pause");
system("cls");
#include <winsock2.h>
{
cout << "Client: Part 2b!\n";
cout << "Client: CLIENT CONNECTION\n\n";
int error;
WSAData wsaData;
error = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (error == SOCKET_ERROR)
{
cout << "Client: WinSock Hates me!!!\n";
return 1;
}
cout << "Client: WinSocket Loaded.\n";
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
if (mySocket == SOCKET_ERROR)
{
cout << "Client: Socket Door is stuck.. Wont open.. -.-;\n";
}
cout << "Client: Socket Opened Successfully!\n";
char server_name[40] = "localhost"; // The server name we will connect to
// If you are on a network and you want to run the client on another computer, you can get the IP from
// running "ipconfig" from the command line, and enter that IP address instead of the word "localhost".
struct hostent *host_entry; // Translates into something the computer can understand
// The hostent struct contains the ip and anme of the host (Among other things). We can fill it by calling the gethostbyname();
host_entry = gethostbyname(server_name); // Gather out information
if (host_entry == NULL)
{
cout << "Client: Ah ****, im lost, wheres the host at?\n";
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons((unsigned short) 7654);
server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;
error = connect(mySocket, (sockaddr*)&server, sizeof(server)); // Lets attempt to connect now
if (error == SOCKET_ERROR)
{
cout << "Client: Damn server wont let me connect!\n";
}
cout << "Client: We are connected!!!\n";
closesocket(mySocket);
cout << "Client: Closing Socket.\n";
WSACleanup();
cout << "Client: Winsock has been shut down.\n";
cout << "Press any key to close down Client ...\n";
getchar();
return 0;
}
}
}
//2nd initiation into panel
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t|\t Gunz Admin Panel 2010 !!|\n";
cout<<"\t+-----------------------------------------------------------+\n";
cout <<"\n[1] - Search\n[2] - Manage Accounts\n[3] - Manage Characters\n[4] - Manage Clans\n\n\n[5] - Logout";
cout <<"\n\nSelection: ";
cin >> selection;
if ( selection == 2 )
{
system("cls");
cout<<"\t+-----------------------------------------------------------+\n";
cout<<"\t|\t Account Management!|\n";
cout<<"\t+-----------------------------------------------------------+\n";
cout <<"Change User ID\n";
cout <<"Change Password\n";
cout <<"Change Account Rank\n";
cout <<"Send item to All accounts storage\n";
cout <<"Send item to Storage";
}
//FINSIHED!
int StartServerListening(unsigned short port);
void EndServer(int socket);
{
cout << "Server: Part3a\n";
cout << "Server: CLINT OFFSIDE\n\n";
int serverSocket;
serverSocket = StartServerListening(7700); // Calls our function StartServerListen(); and passes the port 7700
if (serverSocket == -1)
{
cout << "Server: AHHH EVERYTHING WENT WRONG!!!\nServer: **** THIS! IM DONE!\n";
return 1;
}
int clientSocket;
clientSocket = accept(serverSocket, 0, 0); // Accept The Client
if (clientSocket == SOCKET_ERROR)
{
cout << "Server: Mah, I hate that ***** anyways...\n";
}
int nBytes;
#define MESSAGE_SIZE 24 // This lets us know how much data we will be sending and recieving
char inMessage[MESSAGE_SIZE]; // The actual buffer
char outMessage[MESSAGE_SIZE] = "I received your message"; // Our message to send
nBytes = recv(clientSocket, inMessage, sizeof(inMessage), 0); // Receive at MAX sizeof(inMessage) bytes into inMessage from the client using clientSocket
if (nBytes == SOCKET_ERROR) {
cout << "Server: Ah ****... I didn't get it...\n";
}
cout << "Server: Mah, someone likes you enough to send you a message : %s\n", inMessage;
nBytes = send(clientSocket, outMessage, sizeof(outMessage), 0); // Send sizeof(outMessage) bytes from the buffer "outMessage" to the client on clientSocket.
if (nBytes == SOCKET_ERROR)
{
cout << "Sever: My message was lost and was not sent. :(\n";
}
else
{
cout << "Server: Message Sent : %s\n", outMessage;
}
// And congratulations ... we have just sent and received data between 2 programs!
closesocket(clientSocket);
EndServer(serverSocket); // Shut down the network
cout << "Server: Press any key to shut down the Server...\n";
getchar();
return 0;
}
// [======================================]
// Functions
// [======================================]
// StartServerListen() - a function to startup winsock, and then open a socket for listening
int StartServerListening(unsigned short port)
{
WSAData wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) == SOCKET_ERROR)
{
cout << "Server: Could Not Start Up Winsock!\n";
return -1;
}
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
if (mySocket == SOCKET_ERROR)
{
cout << "Server: Socket is stuck again, wont open...\n";
return -1;
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = INADDR_ANY;
if (bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
cout << "Server: Cant Bind the Socket, thats bad btw...\n";
closesocket(mySocket);
return -1;
}
if (listen(mySocket, 5) == SOCKET_ERROR)
{
cout << "ServeR: Im deaf! No listening socket open!\n";
closesocket(mySocket);
return -1;
}
cout << "Server: Up and Running Partner!\n";
return mySocket;
}
// EndServer() - a function to shutdown a socket and clean up winsock
void EndServer(int socket)
{
// Close our socket
closesocket(socket);
// Shut down winsock
WSACleanup();
cout << "Server: Server Shutting down\n";
}
int StartClient(unsigned short port, const char* serverName);
// EndClient() - will close the socket specified and clean up winsock.
void EndClient(int clientSocket);
{
cout << "Client: Part 3b\n";
cout << "Client: SERVER\n\n";
int mySocket;
mySocket = StartClient(7700, "localhost"); // Port 7700
if (mySocket == -1)
{
EndClient(mySocket); // Hmm... an error occurred. Shutdown our client and bail
return 1;
}
int nBytes; // Doubles as a error code
#define MESSAGE_SIZE 24
char inMessage[MESSAGE_SIZE];
char outMessage[MESSAGE_SIZE] = "HELLO MY LOVE!";
nBytes = send(mySocket, outMessage, sizeof(outMessage), 0); // SEND IT!
if (nBytes == SOCKET_ERROR)
{
cout << "Client: **** That. Send it yourself.\n";
}
else
{
cout << "Client: Sending : %s\n", outMessage;
}
nBytes = recv(mySocket, inMessage, sizeof(inMessage), 0); // Get a message from server.
if (nBytes == SOCKET_ERROR)
{
cout << "Client: Im Afraid to look O_o. I wont recieve it!\n";
}
else
{
cout << "Client: Lol, they actually sent this back : %s\n", inMessage;
}
EndClient(mySocket);
cout << "Client: Press any key to shut me down ...\n";
getchar();
return 0;
}
// -----------------------------------------------------------------------------------
// StartClient() - a function to startup winsock and connect to a server
int StartClient(unsigned short port, const char* serverName)
{
int error;
WSAData wsaData;
if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR)
{
cout << "Client: Winsocks being a *****...\n";
return -1;
}
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
if (mySocket == SOCKET_ERROR)
{
cout << "Client: Fucking Socket is stuck again...\n";
return -1;
}
struct hostent *host_entry;
if ((host_entry = gethostbyname(serverName)) == NULL) {
cout << "Client: Uhm.. Who I connect to again?\n";
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;
if (connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
cout << "Client: IT HATES ME!!! It wont let me in :(...\n";
}
cout << "Client: Im up and running pal!\n";
return mySocket;
}
// -----------------------------------------------------------------------------------
// EndClient() - a function to shutdown a socket and clean up winsock
void EndClient(int clientSocket)
{
closesocket(clientSocket);
WSACleanup();
cout << "Client: Done for the day, night kidos.\n";
}
// -----------------------------------------------------------------------------------
// StartServerListening() - will return us a socket that is bound to the
// port we specify, and is listening for connections if all operations succeeded,
// and a -1 if startup failed.
int StartServerListening(unsigned short port);
// -----------------------------------------------------------------------------------
// EndServer() - will take the socket we specify and shutdown the
// network utilities we started with StartServerListening()
// Note: In order to function properly, the socket passed in MUST be the
// socket created by the StartServerListening() function
void EndServer(int socket);
{
cout << "Server: Part 4a\n";
cout << "Server: SERVER CODE\n\n";
int serverSocket; // Used to listen
serverSocket = StartServerListening(7700);
if (serverSocket == -1)
{
cout << "Oh god... Everything went wrong!! Run!\n";
return 1;
}
int clientSocket; // Socket to accept
clientSocket = accept(serverSocket, 0, 0);
if (clientSocket == SOCKET_ERROR)
{
cout << "Server: Heh, Taught him a lesson on acceptence...\n";
}
int nBytes;
#define MAX_MESSAGE_SIZE 4096 // We need a big enough buffer for our maximum message size.
char buffer[MAX_MESSAGE_SIZE];
unsigned long messageSize; // We need a variable for our size.
nBytes = recv(clientSocket, (char*)&messageSize, sizeof(messageSize), 0);
if (nBytes == SOCKET_ERROR)
{
cout << "Server: I failed you master! Nothing was recieved!\n";
}
messageSize = ntohl(messageSize); // Remember to fix our byte ordering since this is a number
nBytes = recv(clientSocket, buffer, messageSize, 0);
if (nBytes == SOCKET_ERROR)
{
cout << "Server: Recv Failed!\n";
}
buffer[messageSize] = ''; // Place the terminator in the string 'messageSize'
cout << "Server: Someone said : " << buffer << endl;
closesocket(clientSocket);
EndServer(serverSocket);
cout << "Server: Press any key to turn me off baby ...\n";
getchar();
return 0;
}
// -----------------------------------------------------------------------------------
// StartServerListening() - a function to startup winsock, and open a socket for listening
int StartServerListening(unsigned short port)
{
int error;
WSAData wsaData;
if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) {
cout << "Server: WinSock is being a *****, smack it a few times eh?\n";
return -1;
}
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
if (mySocket == SOCKET_ERROR)
{
cout << "Server: Something went terribly wrong, the socket wont open!\n";
return -1;
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = INADDR_ANY;
if (bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
cout << "Server: It wont let me bind correctly.\n";
closesocket(mySocket);
return -1;
}
if (listen(mySocket, 5) == SOCKET_ERROR)
{
cout << "Server: Damnit.. im deaf... Listening Failed!\n";
closesocket(mySocket);
return -1;
}
cout << "Server: Im up and running boss!\n";
return mySocket;
}
// -----------------------------------------------------------------------------------
// EndServer() - a function to shutdown a socket and clean up winsock
void EndServer(int socket)
{
closesocket(socket);
WSACleanup();
cout << "Server: Remember the Alamo!\n";
}
int StartClient(unsigned short port, const char* serverName);
void EndClient(int clientSocket);
// In Visual Studio 2005 Microsoft added safer versions (denoted by a "_s" suffix) to many
// common C/C++ run time functions. Currently the ISO Standards Committee for C and C++
// is determining if they are going to add them to the official language set. Until then,
// this define will remove warnings about using the "unsafe" versions of these functions.
#define _CRT_SECURE_NO_DEPRECATE
{
cout << "Client: Part 4b\n";
cout << "Client: CLIENT CODE\n\n";
int mySocket;
mySocket = StartClient(7700, "localhost");
if (mySocket == -1)
{
EndClient(mySocket);
return 1;
}
int nBytes;
#define MAX_MESSAGE_SIZE 4096
char buffer[MAX_MESSAGE_SIZE];
sprintf(buffer, "This is a test of my variable length send buffer");
unsigned long messageSize = strlen(buffer); // Calculate the size of that data
messageSize = htonl(messageSize); // Remember to fix our byte ordering to network byte ordering
nBytes = send(mySocket, (char*)&messageSize, sizeof(messageSize), 0);
if (nBytes == SOCKET_ERROR)
{
cout << "Client: Eh.. Send Failed\n";
}
messageSize = ntohl(messageSize);
nBytes = send(mySocket, buffer, messageSize, 0);
if (nBytes == SOCKET_ERROR)
{
cout << "Client: Too lazy to send it boss, sorry.\n";
}
EndClient(mySocket);
cout << "Client: Press any key to shut me off ...\n";
getchar();
return 0;
}
// -----------------------------------------------------------------------------------
// StartClient() - a function to startup winsock and connect to a server
int StartClient(unsigned short port, const char* serverName)
{
int error;
WSAData wsaData;
if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR)
{
cout << "Client: Winsock is being a ***** again!\n";
return -1;
}
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
if (mySocket == SOCKET_ERROR)
{
cout << "Client: OH GOD!! Fatel Error!!! Bad things have happened boss, *Cries*\n";
return -1;
}
struct hostent *host_entry;
if ((host_entry = gethostbyname(serverName)) == NULL)
{
cout << "Client: Totally fucking lost...Whos the host again...?\n";
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;
if (connect(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
cout << "Client: Wont let em connect to server boss!\n";
}
cout << "Client: Up and running!\n";
return mySocket;
}
// -----------------------------------------------------------------------------------
// EndClient() - a function to shutdown a socket and clean up winsock
void EndClient(int clientSocket)
{
closesocket(clientSocket);
WSACleanup();
cout << "Client: Shuting down...\n";
}
getchar();
cin.get();
}
Last edited:

