...

View Full Version : In need of C++ sockets help



shell6
01-15-2005, 09:48 PM
Ok, I checked my code, included the winsock lib in the build params, but it crashes on run. Can someone help me out with this?


#include <winsock2.h>
#include <stdio.h>
#include <iostream.h>
#include <string.h>
int mirror(int s, char *input)
{
int 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 server()
{
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);
}
// this will probably never happen
return 2;
}

aman
01-15-2005, 10:49 PM
Ok its crashing because of the way you are treating your strings. You need to allocate memory to your strings before you can use them, or you need to declare them as a char array.

char *p;
strcpy(p, "some text"); <--- this will crash your program

char p[256];
strcpy(p, "some text"); <--- this is ok

char *p;
p = new char[256];
strcpy(p, "some text"); <--- this is ok (C++)
delete [] p;

char *p;
p = (char*)malloc(256*sizeof(char));
strcpy(p, "some text"); <--- this is ok (C)
free(p);


Also, you can't compare C strings the way you are..
char string1[32];
char string2[32];
if( string1 == string2 ) <--- no good
if( !strcmp(string1, string2) ) <--- ok

You can compare strings that way if you use the C++ basic_string class.
#include <string>
string string1;
string string2;
if( string1 == string2 ) <--- ok


One other note, socket() returns INVALID_SOCKET on error, not zero.

shell6
01-15-2005, 11:58 PM
Thanks, but now the socket call always fails :confused: can you help? :(

shell6
01-16-2005, 12:17 AM
Nm! Fixed!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum