Luigi Auriemma

aluigi.org (ARCHIVE-ONLY FORUM!)
It is currently 19 Jul 2012 14:52

All times are UTC [ DST ]





Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Sending UDP Packet to a COD2 Server
PostPosted: 26 Jun 2010 03:13 

Joined: 25 Jun 2010 06:22
Posts: 14
Hey,

I'm trying to send a packet to a cod2 server, but i can't get any answer from the server, i tried different ones without any success. My code is probably wrong somewhere :
Code:
#include <iostream>
#include <winsock2.h>

#pragma comment(lib,"ws2_32.lib")

using namespace std;

static int initSocket(void);
static void endSocket(void);
char buffer[65000];
int rbuffersize = 0;

int main()
{
    initSocket();
    SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if(sock == INVALID_SOCKET)
    {
        cout << "Error : Creating Socket." << endl;
        endSocket();
        return 1;
    }

    strcpy(buffer, "\xFF\xFF\xff\xFFrcon login abcd");

    sockaddr_in sin;
    sin.sin_addr.s_addr = inet_addr("88.198.59.173");
    sin.sin_port = htons(20008);
    sin.sin_family = AF_INET;

    int sent= 0;
    if((sent = sendto(sock, buffer, strlen(buffer),0, (struct sockaddr*)&sin, sizeof(struct sockaddr_in))) != strlen(buffer))
    {
       printf("Error : Connexion Impossible on %s:%d",inet_ntoa(sin.sin_addr), htons(sin.sin_port));
       printf("Error : %s", WSAGetLastError());
        endSocket();
        return 3;
    }

    int tempsize = sizeof(sin);
    char rbuffer[8192];
    rbuffersize=recvfrom(sock,buffer,sizeof(buffer)-1,0,NULL, NULL);

    rbuffer[rbuffersize]=0; /
    printf("\nReceived Datas : %s",rbuffer);

    endSocket();
    return 0;
}

static int initSocket(void)
{

    WSADATA WSAData;
    int err = WSAStartup(MAKEWORD(2,2), &WSAData);
    if(err < 0)
    {
        cout << "Error : Initialization Socket." << endl;
        return 1;
    }

    return 0;
}

static void endSocket(void)
{
    WSACleanup();
}


So if you can help, would be cool. Thanks in advance !


Top
 Profile  
 
 
 Post subject: Re: Sending UDP Packet to a COD2 Server
PostPosted: 26 Jun 2010 04:46 

Joined: 24 Jun 2010 10:04
Posts: 70
Location: aluigi not @ home
at a first look the code seems correct, remember that the rcon string should be like
"\xFF\xFF\xff\xFF" "rcon password command"


Top
 Profile  
 
 Post subject: Re: Sending UDP Packet to a COD2 Server
PostPosted: 26 Jun 2010 10:37 

Joined: 25 Jun 2010 06:22
Posts: 14
I changed the command as you said, and i also added a Timeout Function that waits 3sec (hope its ok).

Now here is my code :

Code:
#include <iostream>
#include <winsock2.h>

#pragma comment(lib,"ws2_32.lib")

using namespace std;

static int initSocket(void);
static void endSocket(void);
int rbuffersize(0);
char buffer[32];
char rbuffer[256];
SOCKET sock;

int waitTimeOut(SOCKET &sock, unsigned int time);

int main()
{
    initSocket();
    sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if(sock == INVALID_SOCKET)
    {
        cout << "Error : Creating Socket." << endl;
        endSocket();
        return 1;
    }

    strcpy(buffer, "\xFF\xFF\xff\xFF" "rcon status");

    sockaddr_in sin;
    sin.sin_addr.s_addr = inet_addr("88.198.5.173");
    sin.sin_port = htons(20008);
    sin.sin_family = AF_INET;

    int sent= 0;
    if( (sent = sendto(sock, buffer, strlen(buffer),0, (SOCKADDR *)&sin, sizeof(sin) )) != strlen(buffer) )
    {
       printf("Error : Connexion Impossible on %s:%d\n",inet_ntoa(sin.sin_addr), htons(sin.sin_port));
       printf("Error : %s", WSAGetLastError());
        endSocket();
        return 3;
    }

    cout << sent << " Bytes Sent." << endl;


    if(waitTimeOut(sock, 3))
    {
        cout << "Error : Time out." << endl;
        endSocket();
        return 10;
    }

    int tempsize = sizeof(sin);
    rbuffersize = recvfrom(sock,rbuffer,256,0, NULL, NULL); // Program gets stuck here...
    if(rbuffersize != SOCKET_ERROR)
    {
        cout << "Received " << rbuffersize  << " Bytes"<< endl;

    }
    else
    {
           cout << "Error : Nothing Received" << endl;
    }


    rbuffer[rbuffersize]=0;
    printf("\nReceived Datas : %s",rbuffer);

    endSocket();
    return 0;
}

static int initSocket(void)
{

    WSADATA WSAData;
    int err = WSAStartup(MAKEWORD(2,2), &WSAData);
    if(err < 0)
    {
        cout << "Error : Initialization Socket." << endl;
        return 1;
    }

    return 0;
}

static void endSocket(void)
{
    closesocket(sock);
    WSACleanup();
}

int waitTimeOut(SOCKET &sock, unsigned int time)
{

    fd_set readSet;
    FD_ZERO(&readSet);
    FD_SET(sock, &readSet);
    struct timeval timeVal;
    timeVal.tv_sec = time;
    timeVal.tv_usec = 0;
    if(select(sock+1, &readSet, NULL, NULL, &timeVal) > 0)
    {
        return true;
    }
    return false;


}


Still nothing is hapenning, it doesn't even say "Invalid Password" (Because i just send a Status command without logging in rcon)

Thanks in advance


Top
 Profile  
 
 Post subject: Re: Sending UDP Packet to a COD2 Server
PostPosted: 26 Jun 2010 11:44 

Joined: 24 Jun 2010 10:04
Posts: 70
Location: aluigi not @ home
exactly you missed the password field, ANY rcon packet must contain the password as first argument:
rcon mypassword status


Top
 Profile  
 
 Post subject: Re: Sending UDP Packet to a COD2 Server
PostPosted: 26 Jun 2010 19:55 

Joined: 25 Jun 2010 06:22
Posts: 14
It worked, you was right. Thanks for this !


Top
 Profile  
 
 Post subject: Re: Sending UDP Packet to a COD2 Server
PostPosted: 21 Jul 2010 12:26 

Joined: 16 Jul 2010 18:43
Posts: 10
You can save some time by just creating a string of "????????rcon password status".
I'm sure you already knew this, but responses will contain line feed characters. Status in Enemy Territory opposed to Jedi Academy responds with less line feed(if any? I only tested a little bit). Not sure how it will be in Call of Duty 2 though.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: