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

Author Topic: Getting a window and networking to work  (Read 24853 times)

0 Members and 2 Guests are viewing this topic.

Pwndja

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Getting a window and networking to work
« on: January 31, 2008, 10:13:54 am »
Hi I was wondering if you would make a simple program that would display a blank window and would allow you to press the a key to send a message from the server to a client and visa versus.

I have been trying to get a window and some networking working but cant seem to get the input to work correctly. I have basically this:

Code: [Select]
while(1)
{
if( app.GetInput().IsKeyDown( sf::Key::A ) && key != true )
{
ToSend = "a";
key = true;
}
if( !app.GetInput().IsKeyDown( sf::Key::A ) && key != true )
{
key = true;
}

if (Client.Send(ToSend.c_str(), sizeof(ToSend.c_str())) != sf::Socket::Done)
return;
std::cout << "Message sent to the client : \"" << ToSend << "\"" << std::endl;

// Receive a message back from the client

if (Client.Receive((char*)Message.c_str(), sizeof(Message.c_str()), Received) != sf::Socket::Done)
return;

// Show the message
std::cout << "Message received from the client : \"" << Message << "\"" << std::endl;
app.Display();
}



in hopes that pressing the 'a' key would change teh sent data to "a" instead of "test" or "test client".

If you have time and it is not too much of a hassle could you put together a tutorial or some code which sends data across when you hit a key.
Thank you.
[/code]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Getting a window and networking to work
« Reply #1 on: January 31, 2008, 10:18:32 am »
It doesn't change much the code to send on key press or directly. The network code will be exactly the same.

You have an error : sizeof(ToSend.c_str()) will always return the size of a pointer, which is probably 4 or 8. Replace this with ToSend.length().
Laurent Gomila - SFML developer