okay, i found one problem, server added data to the packet without cleaning it so the reply packet contained something like this (photo 1). I have fixed it, but it didn't fix the problem.
Client sends message (photo 2) and then server replies with correct data (photo 3), but then client just..ignores it? its not updating its variable (connectionstatus) so it remains 0 (I have added setting variable to 0 and then updating it with received packet, but it remains 0 for some reason... Server however is working perfectly fine for me).
Client code here:
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <windows.h>
#include "TextureHolder.h"
int main()
{
sf::UdpSocket socket;
sf::IpAddress sendingip;
unsigned short sendingport;
std::cout << "IP: ";
std::cin >> sendingip;
std::cout << "Port: ";
std::cin >> sendingport;
unsigned int receivingport = socket.getLocalPort();
if (socket.bind(receivingport) != sf::Socket::Done) {
std::cout << "Failed to bind a socket!" << std::endl;
exit(4);
}
sf::Packet initpacket;
int connectionstatus = 1; //0-brak 1-proba 2-nawiazana
initpacket << connectionstatus;
if (socket.send(initpacket, sendingip, sendingport) != sf::Socket::Done) {
std::cout << "Failed to send connection initialization packet!" << std::endl;
exit(2);
}
sf::IpAddress senderip;
unsigned short senderport;
Sleep(1000);
if (socket.receive(initpacket, senderip, senderport) != sf::Socket::Done) {
std::cout << "Failed to recieve initialization packet!" << std::endl;
exit(2);
}
connectionstatus = 0;
if (!initpacket >> connectionstatus) {
std::cout << "Failed to read initpacket!" << std::endl;
exit(2);
}
std::cout << "CONNSTATUS: " << connectionstatus << std::endl;
if (connectionstatus != 2) {
std::cout << "Connection not stablilized!" << std::endl;
exit(3);
}
//inicjalizacja wszystkiego
TextureHolder TXHOLDER;
sf::RenderWindow _window(sf::VideoMode(800, 800 ), "Chess", sf::Style::Titlebar | sf::Style::Close);
_window.setFramerateLimit(75);
sf::Sprite bG;
bG.setTexture(TXHOLDER.bg);
sf::Event event;
while (_window.isOpen()) {
while (_window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
_window.close();
}
}
_window.clear(sf::Color::White);
_window.draw(bG);
_window.display();
}//isopen
return 0;
}
Server code here:
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <windows.h>
int main()
{
int connectionstatus = 0;
sf::Packet initpacket;
sf::UdpSocket socket1;
unsigned int receivingport = 57000;
unsigned short senderport;
sf::IpAddress senderip;
if (socket1.bind(receivingport) != sf::Socket::Done) {
std::cout << "Failed to bind a socket!" << std::endl;
exit(4);
}
if (socket1.receive(initpacket, senderip, senderport) != sf::Socket::Done) {
std::cout << "Failed to recieve initialization packet!" << std::endl;
exit(2);
}
initpacket >> connectionstatus;
std::cout << connectionstatus << std::endl;
std::cout << senderip<<"||"<<senderport<<std::endl;
if (connectionstatus == 1) {
connectionstatus = 2;
initpacket.clear();
initpacket << connectionstatus;
if (socket1.send(initpacket, senderip, senderport) != sf::Socket::Done) {
std::cout << "Failed to send connection initialization packet!" << std::endl;
exit(2);
}
}
system("pause");
return 0;
}