I have been looking into the SFML libs for a while and im currently learning TCP IP connect but im stuck at this error: Run-Time Check Failure #2 - Stack around the variable 'Client' was corrupted. I just can't figure this one out.
#include "stdafx.h"
#include <SFML/Network.hpp>
#include <iostream>
void DoClientTCP(unsignedshort Port) //short
{
// Ask for server address
sf::IPAddress ServerAddress;
do
{
std::cout << "Type address or name of the server to connect to : ";
std::cin >> ServerAddress;
}
while (!ServerAddress.IsValid());
// Create a TCP socket for communicating with server
sf::SocketTCP Client;
// Connect to the specified server
if (Client.Connect(Port, ServerAddress) != sf::Socket::Done)
return;
{
std::cout << "Connected to server " << ServerAddress << std::endl;
}
// Receive a message from the client
char Message[128];
std::size_t Received;
if (Client.Receive(Message, sizeof(Message), Received) != sf::Socket::Done)
return;
// Show it
{
std::cout << "Message received from server : \"" << Message << "\"" << std::endl;
}
// Define a message to send back to the server
char ToSend[128] = "Hi, I'm a client !";
// Send the message
if (Client.Send(ToSend, sizeof(ToSend)) != sf::Socket::Done)
return;
{
std::cout << "MEssage sent to server : \"" << ToSend << "\"" << std::endl;
}
// Close the socket when we're done
//Client.Close();
}
int main ()
{
int x=4567;
DoClientTCP(x);
int a;
std::cin >> a;
return 0;
}
Which program is crashing? The Client or the Server?
Your if statements are a little screwy. The syntax of an if statement is:
1 2 3
if(/* condition */ {
/* what to do if condition is true */
}
As it is, you have some statements like so (example using the Client send):
1 2 3 4 5 6
if (Client.Send(ToSend, sizeof(ToSend)) != sf::Socket::Done) {
return;
}
{ // begin arbitrary scope
std::cout << "MEssage sent to server : \"" << ToSend << "\"" << std::endl;
} // end arbitrary scope
This doesn't make sense to me.
I would replace you char[] with std::strings as the arrays are probably sending garbage data since they are longer then the string you actually supply them.
I know there is alot of parts that can be changed to make it more efficient and so on, but the truth of the matter is that this code snippet is part of a tutorial just slightly modified and i dident wanna revamp it totally to take it out of context from the tutorial.