Networking with SFML

Hi!

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.

Here is the Server code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "stdafx.h"
#include <SFML/Network.hpp>
#include <iostream>

void DoServerTCP(unsigned short Port) // short
{
    // Create a TCP socket for communicating with clients
    sf::SocketTCP Server;

    // Listen to a port for incoming connections
    if (!Server.Listen(Port))
        return;
	{
    std::cout << "Server is listening to port " << Port << ", waiting for connections... " << std::endl;
	}
	

    // Wait for a connection
    sf::IPAddress ClientAddress;
    sf::SocketTCP Client;
    Server.Accept(Client, &ClientAddress);
    std::cout << "Client connected : " << ClientAddress << std::endl;

    // Send a message to the client
    char ToSend[128] = "Hi, I'm the server";
    if (Client.Send(ToSend, sizeof(ToSend)) != sf::Socket::Done)
        return;
	{
    std::cout << "Message sent to the client : \"" << ToSend << "\"" << std::endl;
	}

    // Receive a message back from the client
    char Message[128]; //128
    std::size_t Received;
    if (Client.Receive(Message, sizeof(Message), Received) != sf::Socket::Done)
        return;
	{
    // Show the message
    std::cout << "Message received from the client : \"" << Message << "\"" << std::endl;
	}

    // Close the sockets when we're done
    Client.Close();
    Server.Close();
}


int main()
{
	using namespace std;

	cout << "Identifying PC ...\n" << endl;

	sf::IPAddress Address1 = sf::IPAddress::GetLocalAddress();
	sf::IPAddress Address2 = sf::IPAddress::GetPublicAddress();

	cout << "Local  IP:" << Address1 << endl;
	cout << "Public IP:" << Address2 << endl << endl;
	cout << "Identification stage 1 complete ...\n";

	int x=4567;
	DoServerTCP(x);

	int a;
	cin >> a;
	return 0;
}


and here is the Client code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "stdafx.h"
#include <SFML/Network.hpp>
#include <iostream>

void DoClientTCP(unsigned short 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;
}


Best regards
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.
Hi,

the program that is crashing is the server side.

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.
Anyone?
Topic archived. No new replies allowed.