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

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

0 Members and 1 Guest are viewing this topic.

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
problem when trying to send a packet in UDP
« Reply #15 on: September 12, 2011, 04:06:31 pm »
Quote from: "Laurent"
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.


yes its the same exact error and i check the validity of the address and its valid.

this thing is really really driving me crazy i dont know what the hell is wrong with it.

in the first SFML tutorial where you send a "char Buffer[]="i'm client" it works perfectly with both UDP and TCP, but when i try to use packets it doesn't work.

i'll post the updated code above.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
problem when trying to send a packet in UDP
« Reply #16 on: September 12, 2011, 04:10:33 pm »
Quote
in the first SFML tutorial where you send a "char Buffer[]="i'm client" it works perfectly with both UDP and TCP, but when i try to use packets it doesn't work.

You should use the sample directly, and only replace the char[] buffer with a packet -- rather than rewriting it entirely. That would seriously limit the number of potential mistakes.
Laurent Gomila - SFML developer

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
problem when trying to send a packet in UDP
« Reply #17 on: September 12, 2011, 04:56:31 pm »
Quote from: "Laurent"
Quote
in the first SFML tutorial where you send a "char Buffer[]="i'm client" it works perfectly with both UDP and TCP, but when i try to use packets it doesn't work.

You should use the sample directly, and only replace the char[] buffer with a packet -- rather than rewriting it entirely. That would seriously limit the number of potential mistakes.


its not working, there is something wrong either with my PC or the class packet.
i'm getting the same Socket state which is 3 (error).

when i do this i get Socket state 3 (error)

Server:
Code: [Select]

SendPacket << message;


Client:
Code: [Select]

ReceivePacket >> message;



However when i do this i get Socket state 1 which is (Done) but the data is corrupted.

Code: [Select]

SendPacket >>message;


Client:
Code: [Select]

ReceivePacket << message;


Please try take my code and run it on your machine and see if you get the same results, maybe i have a virus or something.

here is the code:

Client
Code: [Select]

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

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 << "Socket Status:" << status << std::endl;
 break;

case sf::Socket::Done:
{
std::string message;
ReceivePacket >> message;
std::cout << "IP: " << Address << std::endl << "Port: "<< Port << std::endl << "Message: " << message<< std::endl;
break;
}
case sf::Socket::NotReady:
 std::cout << "NotReady" << std::endl;
 break;
}
}
Client.Close();
}

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


Server:
Code: [Select]

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

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();

std::string message = "PLEASE WORK!!!";

SendPacket << message;

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;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
problem when trying to send a packet in UDP
« Reply #18 on: September 12, 2011, 05:45:12 pm »
I can't test your code, I don't maintain SFML 1.6 anymore (only have 2.0 builds ready for tests), and I don't know where this getchar() function is coming from.

If you already tried to modify the Sockets sample, can you show the corresponding code?
Laurent Gomila - SFML developer

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
problem when trying to send a packet in UDP
« Reply #19 on: September 13, 2011, 03:15:14 am »
i give up -_-

i'm going to learn Winsock 2. i know its 10 times harder but i might have some luck learning it.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
problem when trying to send a packet in UDP
« Reply #20 on: September 13, 2011, 04:43:19 am »
Quote from: "newbie123"
i give up -_-

i'm going to learn Winsock 2. i know its 10 times harder but i might have some luck learning it.
Or you could do the smart thing and use SFML2, which is FAR more stable than 1.x.
I use the latest build of SFML2

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
problem when trying to send a packet in UDP
« Reply #21 on: September 13, 2011, 07:47:47 am »
I don't know how copying/pasting a sample and modifying 2 lines of code is more complicated than learning Winsocks. But for some reason you don't want to do that...

And yes, SFML 1.6 might have bugs with UDP and packets (although this very simple code works for me), so try SFML 2 ;)
Laurent Gomila - SFML developer

 

anything