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;
}