Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: I have problem receiving Data from UDP Socket  (Read 2460 times)

0 Members and 1 Guest are viewing this topic.

Vita

  • Newbie
  • *
  • Posts: 6
    • View Profile
I have problem receiving Data from UDP Socket
« on: August 31, 2011, 04:45:39 pm »
Hi,

I've done something wrong I'm trying to Query a HLDS server.

More info about query:
http://developer.valvesoftware.com/wiki/Server_queries

Here is the code:
Code: [Select]

#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
#pragma comment(lib,"sfml-network-s-d.lib")
#pragma comment(lib,"sfml-system-s-d.lib")
using namespace std;
#define A2S_INFO "\xFF\xFF\xFF\xFF\x54Source Engine Query"
#define A2S_INFO_LENGTH 25
//int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpArguments, int NumParameters)
int main()
{
cout << "PROGRAM STARTING..." << endl;
sf::Packet rcvPaket;
sf::IPAddress rcvIP;
unsigned short rcvPort;
cout << A2S_INFO << endl;

cout << "CREATING SOCKET..." << endl;
sf::SocketUDP sckClient;
cout << "SOCKET CREATED!" << endl;

cout << "BINDING PORT 6586..." << endl;
if(!sckClient.Bind(6586))
{
cout << "BIND FAILED !" << endl;
system("pause");
return 0;
}
cout << "PORT 6586 BINDED..." << endl;

cout << "SENDING PACKET..." << endl;
if(sckClient.Send(A2S_INFO,A2S_INFO_LENGTH,"85.25.35.235",27015) != sf::Socket::Done)
{
cout << "PACKET SENDING ERROR !" << endl;
system("pause");
return 0;
}
cout << "PACKET SENT!" << endl;

//sf::Sleep(1000);

cout << "RECEIVING PACKET..." << endl;
unsigned int rcvLen;
char * data = NULL;

if(sckClient.Receive(data,1028,rcvLen,rcvIP,rcvPort) != sf::Socket::Done)
{
cout << "PACKET RECEIVE ERROR !" << endl;
system("pause");
return 0;
}
//cout << "Data Len: " << rcvLen << " Data: " << data << endl;
printf("Data len: %d Data: %s\n",rcvLen,data);
cout << "PACKET RECEIVED!" << endl;

cout << "CLOSING SOCKET..." << endl;
sckClient.Close();
cout << "SOCKET CLOSED!" << endl;
cout << "END OF PROGRAM!" << endl;

system("pause");
return 0;
}


And Here is the program result:
Code: [Select]

PROGRAM STARTING...
    TSource Engine Query
CREATING SOCKET...
SOCKET CREATED!
BINDING PORT 6586...
PORT 6586 BINDED...
SENDING PACKET...
PACKET SENT!
RECEIVING PACKET...
Cannot receive data from the network (invalid parameters) <-- PROBLEM?
PACKET RECEIVE ERROR !
Press any key to continue . . .


If someone knows what is the problem please tell me.

Thanks in advance!
SFML is the best! :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
I have problem receiving Data from UDP Socket
« Reply #1 on: August 31, 2011, 04:55:26 pm »
The buffer that you pass to Receive is a NULL pointer. It must be a pointer to an existing byte array of 1028 bytes at least (the second argument that you pass).

Code: [Select]
char data[1028];
sckClient.Receive(data, sizeof(data), ...);
Laurent Gomila - SFML developer

 

anything