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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - average

Pages: [1]
1
Network / TCP Socket won't block. Am I missing something
« on: January 04, 2021, 02:27:48 am »
Basically title. My UDP sockets block fine but my TCP Sockets wont block. I'm sure they can block but am I wrong?  I've tried using a selector but have the same results.

Client Side:
        sf::TcpSocket serverSocket;
        serverSocket.setBlocking(true);
        sf::Socket::Status status = serverSocket.connect(serverIP, 5645);


        if(serverSocket.receive(serverData) != sf::Socket::Done)
        {
                //error
        }
 


Server Side:
sf::SocketSelector selector;
        sf::TcpListener listenerSocket;
        if (listenerSocket.listen(5645) != sf::Socket::Done)
        {
                std::cout << "Failed to bind listener socket\n";
        }
        selector.add(listenerSocket);
if (selector.wait(portCheckTime))
                {
                        // Test the listener
                        if (selector.isReady(listenerSocket))
                        {

                                sf::TcpSocket client;

                                if (listenerSocket.accept(client) == sf::Socket::Done)
                                {

                                        std::cout << "A client has joined\n";

                                }
if (client.send(serverData) == sf::Socket::Done)
                                {
                               
                                        if (iteration == 1)
                                        {
                                                iteration = 2;
                                        }
                                        else if (iteration == 2)
                                        {
                                                iteration = 1;
                                        }
                                        socket++;
                                }
 

2
Network / UDP Socket not recieving updated data.
« on: January 01, 2021, 03:43:56 pm »
I am tryng to send a packet of data to another instance of a program on the same PC using UDP Socket but when I update the data and send it the recieve only recievs the intial data.  I have gotr my TCP stuff working but i dont know if im missing something on the UDP side. I have followed along with the SFML tutorials for sockets and packets so I'm a little stuck now.
I'm setting up the sockets like this:
        sf::TcpSocket serverSocket;
        serverSocket.setBlocking(true);
        sf::Socket::Status status = serverSocket.connect("127.0.0.1", 1000);

        if (status != sf::Socket::Done)
        {
                //error...
        }


        while(serverSocket.receive(serverData) != sf::Socket::Done)
        {
                //error
        }

        serverData >> playerID >> mapSeed >> recievedPort;
       
        std::cout << recievedPort;

        std::cout << mapSeed << "\n";
        std::cout << playerID << "\n";

        if (playerID == 1)
        {
                ship.setPosition(100, 100);
                sendPort = recievedPort + 1;
                bindPort = recievedPort;
        }

        if (playerID == 2)
        {
                ship.setPosition(1600, 900);
                sendPort = recievedPort - 1;
                bindPort = recievedPort;
        }

        sf::UdpSocket playerSocket;
        playerSocket.setBlocking(false);
        if (playerSocket.bind(bindPort) != sf::Socket::Done)
        {
                // error...
        }

        srand(mapSeed);

        for (int i = 0; i < XTILES; i++)
        {
                for (int j = 0; j < YTILES; j++)
                {

                        tileMap[i][j].setRenderValue(rand() % 20 + 1);

                }
        }
 
Which recieves fine and I send my data like this (All the packet data is float):
playerData.x = ship.getPosition().x;
                playerData.y = ship.getPosition().y;
                playerData.shipOrientation = ship.getOrientation();
                playerData.cannonOrientation = ship.getCannonOrientation();
                playerData.time = 0;

                playerPacket << playerData;

                if (playerSocket.send(playerPacket,sf::IpAddress::LocalHost , sendPort) != sf::Socket::Done)
                {
                        // error...
                }


                sf::IpAddress sender;
                if (playerSocket.receive(enemyPacket, sender, bind) != sf::Socket::Done)
                {
                        // error...
                }

                enemyPacket >> enemyData;

                eShip.setPosition(enemyData.x, enemyData.y);
 

The enemy data recieved is just always the the initial data sent. Am I missing anything?

Pages: [1]