SFML community forums
Help => Network => Topic started by: Srejv on January 09, 2008, 11:31:52 pm
-
server.cpp: In function 'int main()':
server.cpp:25: error: no matching function for call to 'sf::SocketUDP::Receive(char [128], unsigned int, size_t&, sf::IPAddress&, short unsigned int&)'
/usr/include/SFML/Network/SocketUDP.hpp:107: note: candidates are: sf::Socket::Status sf::SocketUDP::Receive(char*, size_t, size_t&, sf::IPAddress&)
/usr/include/SFML/Network/SocketUDP.hpp:131: note: sf::Socket::Status sf::SocketUDP::Receive(sf::Packet&, sf::IPAddress&)
And when I remove the port parameter it says "please specify port". I'm really confused.
-
The port is now specified with the Bind function, that you have to call before Receive.
-
Ahaa! Thanks. :D
I was also wondering, is there any way to make recieveing a packet an event? Or maybe I should put the networking in a new thread? :)
-
Okay so I tried to create some small app to send and one to recieve small messages, but I'm not sure the client sends the message. :?
server.cpp
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
sf::Mutex GlobalMutex;
bool ThreadRunning = true;
void ServerPoll(void* UserData)
{
GlobalMutex.Lock();
std::cout << "Starting up Server." << std::endl;
GlobalMutex.Unlock();
sf::SocketUDP Server;
Server.Bind(6739);
sf::IPAddress ip(127,0,0,1);
GlobalMutex.Lock();
std::cout << "Finished setting up for Thread." << std::endl;
GlobalMutex.Unlock();
while(ThreadRunning)
{
char Message[128];
std::size_t Received;
GlobalMutex.Lock();
std::cout << "Waiting for message." << std::endl;
GlobalMutex.Unlock();
if(!Server.Receive(Message, sizeof(Message), Received, ip));
return;
GlobalMutex.Lock();
std::cout << "Message recieved." << std::endl;
GlobalMutex.Unlock();
GlobalMutex.Lock();
std::cout << Message << std::endl;
GlobalMutex.Unlock();
}
GlobalMutex.Lock();
std::cout << "Finished thread." << std::endl;
GlobalMutex.Unlock();
GlobalMutex.Lock();
std::cout << "Closing server." << std::endl;
GlobalMutex.Unlock();
Server.Close();
GlobalMutex.Lock();
std::cout << "Closed server." << std::endl;
GlobalMutex.Unlock();
}
int main()
{
sf::RenderWindow App(sf::VideoMode(800,600), "Target lul");
sf::Thread Thread(&ServerPoll);
bool Running = true;
bool ThreadRunning = true;
Thread.Launch();
while(Running)
{
sf::Event Event;
if(App.GetEvent(Event))
{
if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Escape)
{
Running = false;
ThreadRunning = false;
}
}
App.Display();
}
return 0;
}
client.cpp
#include <SFML/Network.hpp>
#include <iostream>
int main()
{
sf::IPAddress target(127,0,0,1);
unsigned short port = 6739;
sf::SocketUDP Client;
Client.Bind(port);
char Buffer[] = "HEYAHO :D:DDd.Dd:D";
std::cout << "Sending message: " << Buffer << std::endl;
if (!Client.Send(Buffer, sizeof(Buffer), target, port))
{
std::cout << "Msg fail" << std::endl;
return 0;
}
Client.Close();
std::cout << "Finished." << std::endl;
return EXIT_SUCCESS;
}
Any ideas? The output I get from client.cpp is "Msg fail", so I guess I might have some parameters wrong.
-
The socket mustn't be bound to send, only to receive.
-
Okay, but does that matter? :|
-
Yes, as you can't bind two sockets on the same port (the second will fail).
-
Okay, I removed the Bind from client.cpp, but it still doesn't work. :/
Edit:
So I changed the recieve and send ifstatements to:
if(Server.Receive(Message, sizeof(Message), Received, ip) != sf::Socket::Done)
return;
And
if(Client.Send(Buffer, sizeof(Buffer), target, port) != sf::Socket::Done)
{
std::cout << "Msg fail" << std::endl;
return 0;
}
And it works. :)
-
I'm having the same problem. Why isn't this compiling?
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;
int main()
{
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(640, 480, 32), "blah");
sf::IPAddress addy("127.0.0.1");
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
}
if (addy.IsValid()){
sf::SocketUDP Socket;
// Create bytes to send
char Buffer[] = "Hi guys !";
// Send data to "192.168.0.2" on port 4444
if (Socket.Send(Buffer, sizeof(Buffer), addy, 4444) != sf::Socket::Done)
{
cout << "sending failed";
}
//bind socket before recieveing data
sf::SocketUDP socket2;
// Bind it (listen) to the port 4444
if (!socket2.Bind(4444))
{
cout << "binding failed";
}
//get data
char buffer2[128];
std::size_t Received;
sf::IPAddress Sender;
if (socket2.Receive(buffer2, sizeof(buffer2), Received, Sender) != sf::Socket::Done)
{
// Error...
}
// Show the address of the sender
std::cout << Sender << std::endl;
// Show the message
std::cout << buffer2 << std::endl; // "Hi guys !"
//close socket
socket2.Close();
}
// Clear the screen (fill it with black color)
App.Clear();
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}
Errors:
/home/ubuntu/Desktop/projects/Metaship/main.cpp|51|error: no matching function for call to ‘sf::SocketUDP::Receive(char [128], unsigned int, size_t&, sf::IPAddress&)’|
-
Because there's no sf::SocketUDP::Receive function with such a prototype. Please read the doc.
-
Yeah, my bad. I had ver 1.6 and reading the tutorial for 1.2 :\