Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    In need of C++ sockets help

    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?

    Code:
    #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;
    }

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, but now the socket call always fails can you help?

  • #4
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nm! Fixed!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •