Gdy podaje to samo IP na tym samym PC, czyli np. 192.168.0.9 to wszystko łączy się bez problemu.
Jak programy są już na 2 różnych PC, i serwer wpisuje IP klienta, a klient wpisuje IP serwera to program nie działa. IP w tym wypadku podawane jest publiczne. Jak to wszystko naprawić?
#include <SFML\Graphics.hpp>
#include <SFML\Network.hpp>
#include <string>
#include <iostream>
using namespace sf;
using namespace std;
void func()
{
}
int main()
{
const int WIDTH = 600;
const int HEIGHT = 315;
IpAddress ip;
RenderWindow window(VideoMode(600,315),"Program",Style::Close);
//czcionka i pierwszy wyświetlony napis
Font font;
font.loadFromFile("robotastic.ttf");
Text napis("Serwer ([S])/Klient ([K])",font,20);
napis.setPosition(WIDTH/2-napis.getGlobalBounds().width/2,(HEIGHT/2-napis.getGlobalBounds().height/2));
bool serwer; //flaga mowiaca o tym czy gracz jest klientem czy serwerem
//wybor czym jestesmy (wiem, dziwnie to brzmi (: )
bool status = true;
while(status)
{
Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case Event::KeyReleased:
switch (event.key.code)
{
case Keyboard::S:
{
serwer = true;
status = false;
break;
}
case Keyboard::K:
{
serwer = false;
status = false;
break;
}
}
}
break;
}
window.clear();
window.draw(napis);
window.display();
}
//podajemy ip o ile wczesniej nie zostalo wczytane z pliku
if(ip.toString() == "0.0.0.0")
{
napis.setString("Podaj ip klienta/serwera");
napis.setPosition(WIDTH/2-napis.getGlobalBounds().width/2,(HEIGHT/2-napis.getGlobalBounds().height/2)-30);
Text text_ip("",font,15);
string str_ip;
bool b = false;
while(!b)
{
Event event;
while(window.pollEvent(event))
{
if(event.type == Event::Closed)
b = true;
if(event.type == Event::KeyReleased && event.key.code == Keyboard::Return)
{
ip = str_ip;
b = true;
}
else if(event.type == Event::TextEntered)
{
if (event.text.unicode == 8) //backspace
{
if(str_ip.size()>0)
str_ip.erase(str_ip.size() - 1, 1);
}
else if (event.text.unicode < 128 && event.text.unicode != 13)
str_ip+= (char)event.text.unicode;
text_ip.setString(str_ip);
text_ip.setPosition(WIDTH/2-text_ip.getGlobalBounds().width/2,HEIGHT/2-text_ip.getGlobalBounds().height/2);
}
}
window.clear();
window.draw(napis);
window.draw(text_ip);
window.display();
}
}
//Probne polaczenie i odebranie pliku
unsigned short s_port = 50001; //port na ktorym dane odbiera serwer
unsigned short k_port = 50002; //port na ktorym dane odbiera klient
UdpSocket socket;
if(serwer) socket.bind(s_port);
else socket.bind(k_port);
if(serwer)
{
string tmp;
Packet pakiet;
socket.receive(pakiet,ip,s_port);
pakiet >> tmp;
cout<<tmp<<endl;
pakiet.clear();
tmp = "Polaczono z serwerem!n";
pakiet << tmp;
socket.send(pakiet,ip,k_port);
}
else
{
string tmp = "Polaczono z klientemn";
Packet pakiet;
pakiet << tmp;
socket.send(pakiet,ip,s_port);
pakiet.clear();
socket.receive(pakiet,ip,k_port);
pakiet >> tmp;
cout<<tmp;
}
system("pause");
}