I made this for a suite that I'm working on with a coder. It is my first socket program. I'm pretty sure that it is correct, but it crashes after I cin the admin password. Can someone help me out here?
Code:
#include <winsock2.h>
#include <stdio.h>
#include <iostream.h>
#include <string.h>
int mirror(int s, char *input, int se)
{
se = send(s, input, sizeof(input), 0);
if(send != 0)
return -1;
else
return 0;
}
struct rcon_strings { //remote command strings (not mirrored)
char *shutdown; // /admin/<password>/shutdown (kills server)
char *flush; // /admin/<password>/flush (flushes *buffer)
};
int main()
{
int sstart; // WSAStartup
WORD ver; // version
ver = MAKEWORD (2,2); // version number
WSADATA wsaData;
sstart = WSAStartup(ver, &wsaData);
if(sstart != 0)
{
cout << "PANIC!: Could not run WSAStartup (00).\n";
return 0;
}
rcon_strings rcon;
struct sockaddr_in serv;
int s; // socket
int bi, list; // bind and listen
int se, re; // send/recv vars
int acce; // accept
int mir; // mirror
int i; // iterations
char *buffer, *admin; // input buffer and admin password plus comparison
serv.sin_family = AF_INET;
serv.sin_port = htons(8060); // The server runs on 8060
serv.sin_addr.s_addr = htonl(INADDR_ANY);
cout << "Admin password: ";
cin >> admin;
rcon.shutdown = "/admin/";
strcat(rcon.shutdown, admin);
strcpy(rcon.shutdown, "/shutdown");
rcon.flush = "/admin/";
strcat(rcon.flush, admin);
strcpy(rcon.flush, "/flush");
s = socket(AF_INET, SOCK_STREAM, 0);
if(s != 0)
{
cout << "PANIC!: Socket call failed (01).\n";
return -1;
}
bi = bind(s, (struct sockaddr *)&serv, sizeof(serv));
if(bi != 0)
{
cout << "PANIC!: Bind failed (02).\n";
return -2;
}
list = listen(s, 6);
if(list != 0)
{
cout << "PANIC!: Listen call failed (03).\n";
return -3;
}
acce = accept(s, NULL, NULL);
if(acce != 0)
{
cout << "PANIC!: Accept call failed (04).\n";
return -4;
}
while(i == 0)
{
re = recv(s, buffer, sizeof(buffer), 0);
if(buffer == rcon.shutdown)
{
cout << "rcon::shutdown has been used. Shutting down...\n";
return 1;
}
if(buffer == rcon.flush)
{
cout << "rcon::flush has been used, buffer being flushed.\n";
buffer = "";
}
else
mirror(s, buffer, se);
}
// this will probably never happen
return 2;
}