1
Network / Connected UDP
« on: October 02, 2010, 04:53:31 pm »
Thanks buddy. It really helped. Now I've got both way working transmission.
Bye
Bye
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;
using namespace sf;
void server(int);
void client(int);
IPAddress IPClient;
int main()
{
cout << "Type: \'s\' to run server or \'c\' to run client." <<endl;
char input;
cin >> input;
if(input == 's')
{
server(0);
client(1);
}
else if(input == 'c')
{
client(0);
server(1);
}
system("Pause");
}
void server(int param)
{
SocketUDP Server;
if(param == 0)
{
if (!Server.Bind(10023))
{
cout << "Error" << endl;
}
}
else if(param == 1)
{
if (!Server.Bind(10024))
{
cout << "Error" << endl;
}
}
cout << "Server initialized." << endl;
IPAddress SenderIP;
unsigned short SenderPort;
sf::Packet Packet;
while (true)
{
sf::Socket::Status SocketStatus = Server.Receive(Packet, SenderIP, SenderPort);
cout << endl <<endl << "Socket status: " << SocketStatus << endl << "---------------" <<endl;
if(SocketStatus == sf::Socket::Done)
{
// Extract the message and display it
string message;
Packet >> message;
cout << "IP: " << SenderIP << endl << "Port: "<< SenderPort << endl << "Message: " << message<< endl;
IPClient = SenderIP;
return;
}
}
Server.Close();
}
void client(int param)
{
cout << "Client Initialized." << endl;
int ServPort;
IPAddress ServIP;
if(param == 0)
{
IPAddress ServIPBuff("here comes server IP"); // !!!
ServIP = ServIPBuff;
ServPort = 10023;
}
else if(param == 1)
{
ServIP = IPClient;
ServPort = 10024;
}
if (!ServIP.IsValid()){
cout << "Niepoprawny adres serwera" << endl;
}
SocketUDP Client;
string Message = "Hi, I'm a client !";
sf::Packet PacketToSend;
PacketToSend << Message;
cout << "Press Enter to send packet." <<endl;
getchar();
char input = getchar();
while(input == 10)
{
if (Client.Send(PacketToSend, ServIP, ServPort) != Socket::Done)
cout << "Pierdolony error!" << endl;
else
cout << "Message sent to server : \"" << Message << "\"" << endl;
input = getchar();
if(input == 'z') break;
}
Client.Close();
}
bugy wrote:
I get an error: "error: 'class sf::RenderWindow' has no member named 'SetTitle'|"
But the documentation claim it has.
Then you're probably looking at a documentation that doesn't correspond to your SFML version.
Resources res;
int main()
{
struct Resources
{
public:
sf::RenderWindow App;
sf::Image Image;
sf::Sprite Sprite;
sf::String DebugTextBlock;
};
res.DebugTextBlock.SetFont(sf::Font::GetDefaultFont());
res.DebugTextBlock.SetText("---------------");//buffer.str());
res.DebugTextBlock.SetSize(50);
res.DebugTextBlock.SetCenter(res.DebugTextBlock.GetRect().GetWidth(), 0);
res.DebugTextBlock.SetColor(sf::Color(100, 100, 100));
res.DebugTextBlock.SetRotation(0.f);
res.DebugTextBlock.SetScale(1.f, 1.f);
res.DebugTextBlock.Move(780.f, 10.f);
res.App.Draw(res.DebugTextBlock);
sf::RenderWindow object(sf::VideoMode(800,500), "hello world");
object.RenderWindow(sf::VideoMode(800,500), "hello world");
sf::RenderWindow::RenderWindow ( VideoMode Mode,
const std::string & Title,
unsigned long WindowStyle = Style::Resize | Style::Close,
const WindowSettings & Params = WindowSettings()
)
sf::RenderWindow::RenderWindow ( WindowHandle Handle,
const WindowSettings & Params = WindowSettings()
)
Text.SetStyle(sf::String::Bold | sf::String::Italic | sf::String::Underlined);