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

Author Topic: Error while using sf::Packet  (Read 4564 times)

0 Members and 1 Guest are viewing this topic.

4s5a5sin

  • Newbie
  • *
  • Posts: 2
    • View Profile
Error while using sf::Packet
« on: October 22, 2018, 09:40:10 pm »
Hi there,

I get an runtime error, if want to use sf::Packet to send extended packets over a UDP Socket.
The issue is, that the program crashes, if the program insert data by using the << operator.

The error is:
German:
Ausnahme ausgelöst bei 0x00007FF9EDE6C2AE (vcruntime140.dll) in Packet.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x0000004B89500000.
English:
Exception triggered at 0x00007FF9EDE6C2AE (vcruntime140.dll) in Packet.exe: 0xC0000005: Access violation while reading at position 0x0000004B89500000.

Has anyone an idea, how to fix it?

BTW: I'm using Visual Studio Community 2017 with the x64 Compiler.

Heres the complete Code (The Issue is at Line 21 [packet << x << s << d;] ):
#include <iostream>
#include <string>
#include <SFML/Network.hpp>

void Client() {
        sf::UdpSocket socket;

        std::cout << "Enter Server IP-Adress: ";
        sf::IpAddress server;
        std::cin >> server;

        std::cout << "Enter Port: ";
        int port;
        std::cin >> port;

        sf::Uint16 x = 10;
        std::string s = "Hello";
        double d = 0.6;

        sf::Packet packet;
        packet << x << s << d;

        if (socket.send(packet, server, port) != sf::Socket::Done) {
                return;
        }
        std::cout << "Message sent to the server: \"" << packet << "\"" << std::endl;

        sf::IpAddress sender;
        unsigned short senderPort;
        if (socket.receive(packet, sender, senderPort) != sf::Socket::Done) {
                return;
        }
        std::cout << "Message received from " << sender << ": \"" << packet << "\"" << std::endl;

}

void Server() {
        sf::UdpSocket socket;

        std::cout << "Enter Port: ";
        int port;
        std::cin >> port;

        if (socket.bind(port) != sf::Socket::Done) {
                return;
        }
        std::cout << "Server runs on Port: " << port << std::endl;

    std::size_t received;
        sf::IpAddress sender;
        unsigned short senderPort;
        sf::Packet packet;

        bool done = false;

        while (done != true) {
                if (socket.receive(packet, sender, senderPort) != sf::Socket::Done) {
                        return;
                }
                std::cout << "Message received from client " << sender << ": \"" << packet << "\"" << std::endl;

                if (socket.send(packet, sender, senderPort) != sf::Socket::Done) {
                        return;
                }
                std::cout << "Message sent to the client: \"" << packet << "\"" << std::endl;

                std::cout << "Continue? No (0) | Yes (1)";
                std::cin >> done;
                if (done == 1) {
                        std::cout << "Stopped";
                        break;
                } else {
                        std::cout << "Continue";
                        done = 0;
                }
        }
}


int main() {

        std::cout << "Server (0) | Client (1)" << std::endl;
        int networktype;
        std::cin >> networktype;

        if (networktype == 0) {
                Server();
        }
        else {
                Client();
        }

        system("pause");
        return 0;
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Error while using sf::Packet
« Reply #1 on: October 22, 2018, 10:43:48 pm »
Make sure that your SFML libraries match the configuration of your project (VS 2017, x64, debug or release).
Laurent Gomila - SFML developer

4s5a5sin

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Error while using sf::Packet
« Reply #2 on: October 24, 2018, 06:53:55 pm »
Make sure that your SFML libraries match the configuration of your project (VS 2017, x64, debug or release).

That helps! Many Thanks. :)