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

Author Topic: problem when trying to send a packet in UDP  (Read 9270 times)

0 Members and 2 Guests are viewing this topic.

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
problem when trying to send a packet in UDP
« on: September 10, 2011, 06:12:48 am »
Hello

i have a small problem, when the client receive data its not the same data that i send, its different.

here what the server send:
Age: 12, Name: Bill, Hight: 1.32f

and here what the client receives:
Age: 52428 Name: -blank- Hight: -1.07374e+008

Updated Code 9-12-2011 2:06 pm :-

Server
Code: [Select]

#include <SFML\Network.hpp>
#include <iostream>

struct PersonData
{
sf::Uint16 Age;
std::string Name;
float Height;
};

sf::Packet& operator <<(sf::Packet& Packet, const PersonData& PD)
{
return Packet << PD.Age << PD.Name << PD.Height;
}

sf::Packet& operator >>(sf::Packet& Packet, PersonData& PD)
{
return Packet >> PD.Age >> PD.Name >> PD.Height;
}

void RunServer(unsigned short Port)
{
sf::SocketUDP Server;
sf::IPAddress Address;
sf::Packet SendPacket;

std::cout << "type the ip address you want to send data to" << std::endl;
std::cin >> Address;

if (Address.IsValid())
std::cout << "Valid" << std::endl;

std::cout << "Press Enter to send packet." <<std::endl;
getchar();

char input = getchar();

PersonData PersonData1 = {12, "Bill", 1.32f};

SendPacket << PersonData1;

while(input == 10)
{
if (Server.Send(SendPacket, Address , Port) != sf::Socket::Done)
{
std::cout<<"Could not send data";
}
input = getchar();

        if(input == 'z')
break;
}
Server.Close();
}

int main()
{
const unsigned short Port = 4000;
RunServer(Port);
system("pause");
return 0;
}




Client
Code: [Select]

#include <SFML\Network.hpp>
#include <iostream>

struct PersonData
{
sf::Uint16 Age;
std::string Name;
float Height;
};

sf::Packet& operator <<(sf::Packet& Packet, const PersonData& PD)
{
return Packet << PD.Age << PD.Name << PD.Height;
}

sf::Packet& operator >>(sf::Packet& Packet, PersonData& PD)
{
return Packet >> PD.Age >> PD.Name >> PD.Height;
}

void RunClient(unsigned short Port)
{
sf::SocketUDP Client;
sf::IPAddress Address;
sf::Packet ReceivePacket;

if (!Client.Bind(Port))
{
std::cout<<"Could not listen";
}

while (true)
{
sf::Socket::Status status = Client.Receive(ReceivePacket, Address, Port);
switch(status)
{
case sf::Socket::Disconnected:
 std::cout << "Disconnected" << std::endl;
 break;

case sf::Socket::Error:
 std::cout << "Error sending" << std::endl;
 break;

case sf::Socket::Done:
std::cout << "Done" << std::endl;
 break;

case sf::Socket::NotReady:
 std::cout << "NotReady" << std::endl;
 break;
}

PersonData PersonData2;

if (ReceivePacket >> PersonData2)
{
std::cout << "Age: " << PersonData2.Age << "Name: " << PersonData2.Name << "Hight: "<< PersonData2.Height << std::endl;
}

}
Client.Close();
}

int main()
{
const unsigned short Port = 4000;
RunClient(Port);
system("pause");
return 0;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
problem when trying to send a packet in UDP
« Reply #1 on: September 10, 2011, 10:07:26 am »
Quote
its not working

Can you be more precise? Where is it failing?
Laurent Gomila - SFML developer

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
problem when trying to send a packet in UDP
« Reply #2 on: September 10, 2011, 03:14:39 pm »
Quote from: "Laurent"
Quote
its not working

Can you be more precise? Where is it failing?

well i really dont know what happens.
when i run the client it just sites there waiting to receive the data and nothing shows up on the CMD window. then when i run the server it just give me "Press any key to continue ...".
In the SFML network - packet tutorial source code its just showing how to send packet in TCP connection, i took it and tried to modify it to make it work with UDP but as i told you its not working.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
problem when trying to send a packet in UDP
« Reply #3 on: September 10, 2011, 04:04:22 pm »
You should try the Sockets sample from the SFML SDK. It works with raw data but it can easily be replaced with packets.
Laurent Gomila - SFML developer

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
problem when trying to send a packet in UDP
« Reply #4 on: September 10, 2011, 05:21:17 pm »
ok i found the problem.
so what i'm doing is making the server send data and the client receives it and print it out.

so what i did in the client side is:
Code: [Select]

if (RegularPacket >> RP1)
{
 //do...
}


which is wrong, i should have done this
Code: [Select]

if (RegularPacket << RP1)
{
 //do...
}


same in the server side i did this
Code: [Select]

RegularPacket << C1;


which is wrong i should have do this
Code: [Select]

(RegularPacket >>RP1)


so now the server sends the data and the client receive it FINALY ! :D .

But i have a small problem now, when the client receive the data its not the same data that i send, its different.

here what the server send:
Age: 12, Name: Bill, Hight: 1.32f

and here what the client receives:
Age: 52428 Name: -blank- Hight: -1.07374e+008

Why ?!
i'll post the updated code above.
Code: [Select]

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
problem when trying to send a packet in UDP
« Reply #5 on: September 12, 2011, 01:04:18 pm »
please guys someone help its been 3 days and i still could not fix this problem .

i alos posted here but no one could fix my problem too.
http://www.gamedev.net/topic/610454-problem-when-trying-to-send-a-packet-in-udp-sfml/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
problem when trying to send a packet in UDP
« Reply #6 on: September 12, 2011, 01:26:39 pm »
If you have updated your code, you should show us the new version.
Laurent Gomila - SFML developer

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
problem when trying to send a packet in UDP
« Reply #7 on: September 12, 2011, 01:43:45 pm »
Quote from: "Laurent"
If you have updated your code, you should show us the new version.


i posted the new version above, but i'm still getting Socket state = 3 (error).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
problem when trying to send a packet in UDP
« Reply #8 on: September 12, 2011, 01:57:02 pm »
So it's a new problem, you don't receive garbage anymore? And where does it return an error state?
Laurent Gomila - SFML developer

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
problem when trying to send a packet in UDP
« Reply #9 on: September 12, 2011, 02:06:02 pm »
Quote from: "Laurent"
So it's a new problem, you don't receive garbage anymore


yah the new problem is that i don't receive garbage anymore because i changed the operator overloading signs "<<".


Quote from: "Laurent"
where does it return an error state?

after i press Enter key 3 times in the server i get an error in the client.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
problem when trying to send a packet in UDP
« Reply #10 on: September 12, 2011, 02:16:06 pm »
What's the output of both the client and the server?
Laurent Gomila - SFML developer

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
problem when trying to send a packet in UDP
« Reply #11 on: September 12, 2011, 02:30:27 pm »
Quote from: "Laurent"
What's the output of both the client and the server?


Client EXE
http://www.mediafire.com/?7585wacih8grhhy

Server EXE
http://www.mediafire.com/?ou2ms3whwma8mgh



By fantasyxii


By fantasyxii


By fantasyxii


By fantasyxii


By fantasyxii


By fantasyxii

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
problem when trying to send a packet in UDP
« Reply #12 on: September 12, 2011, 02:33:36 pm »
The address that you pass to Send in the server is uninitialized, you're sending to a random address.

The "choose the address you want to connect to" piece of code should be in the server, not the client since the client only receives.
Laurent Gomila - SFML developer

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
problem when trying to send a packet in UDP
« Reply #13 on: September 12, 2011, 02:51:50 pm »
Quote from: "Laurent"
The address that you pass to Send in the server is uninitialized, you're sending to a random address.

The "choose the address you want to connect to" piece of code should be in the server, not the client since the client only receives.


ok i'm not sure if i understood you correctly, is this what you mean i should do?

move this piece of code from client to server

Code: [Select]

std::cout << "type the server ip address you want to connect to" << std::endl;
std::cin >> Address;


i did that but i still get the some error. should i use other ip than 127.0.0.1?

Server
Code: [Select]

void RunServer(unsigned short Port)
{
sf::SocketUDP Server;
sf::IPAddress Address;
sf::Packet SendPacket;

std::cout << "type IP " << std::endl;
std::cin >> Address;

std::cout << "Press Enter to send packet." <<std::endl;
getchar();

char input = getchar();

PersonData PersonData1 = {12, "Bill", 1.32f};

SendPacket << PersonData1;

while(input == 10)
{
if (Server.Send(SendPacket, Address , Port) != sf::Socket::Done)
{
std::cout<<"Could not send data";
}
input = getchar();

        if(input == 'z')
break;
}
Server.Close();
}


Client
Code: [Select]

void RunClient(unsigned short Port)
{
sf::SocketUDP Client;
sf::IPAddress Address;
sf::Packet ReceivePacket;

if (!Client.Bind(Port))
{
std::cout<<"Could not listen";
}

while (true)
{
sf::Socket::Status status = Client.Receive(ReceivePacket, Address, Port);
switch(status)
{
case sf::Socket::Disconnected:
 std::cout << "Disconnected" << std::endl;
 break;

case sf::Socket::Error:
 std::cout << "Error sending" << std::endl;
 break;

case sf::Socket::Done:
std::cout << "Done" << std::endl;
 break;

case sf::Socket::NotReady:
 std::cout << "NotReady" << std::endl;
 break;
}

PersonData PersonData2;

if (ReceivePacket >> PersonData2)
{
std::cout << "Age: " << PersonData2.Age << "Name: " << PersonData2.Name << "Hight: "<< PersonData2.Height << std::endl;
}

}
Client.Close();
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
problem when trying to send a packet in UDP
« Reply #14 on: September 12, 2011, 03:23:31 pm »
Is it exactly the same error, after pressing enter 3 times?

127.0.0.1 should be ok. But check the validity of the address in your code, just to be sure you don't mistype it.
Laurent Gomila - SFML developer

 

anything