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

Author Topic: Help with sending/receiving packets on LAN/WAN  (Read 3568 times)

0 Members and 1 Guest are viewing this topic.

Zephilinox

  • Newbie
  • *
  • Posts: 43
    • View Profile
Help with sending/receiving packets on LAN/WAN
« on: August 05, 2012, 01:58:28 pm »
When I run this code on myself, it works fine, but when I run it on two computers and try to connect to each other, both can send but only one can receive, what's wrong? it's not a problem with the router/firewalls, I've set it up properly and it shouldn't affect the LAN anyway.

sf::IpAddress ip;
unsigned short int port = 2222;
void client()
{
    sf::TcpSocket clientSocket;
    sf::Packet clientPacket;

    clientSocket.connect(ip, port);

    while (true)
    {
        std::string send;
        std::getline(std::cin, send);
        clientPacket << send;
        clientSocket.send(clientPacket);
        clientPacket.clear();
    }
}

void server()
{
    sf::TcpListener listener;
    sf::TcpSocket serverSocket;
    sf::Packet serverPacket;

    listener.listen(port);
    listener.accept(serverSocket);
    while (true)
    {
        std::string receive;
        serverSocket.receive(serverPacket);
        serverPacket >> receive;
        std::cout << receive << "[received]" << std::endl;
        serverPacket.clear();
    }
}

int main()
{
    std::cin >> ip;
    std::cin.clear();
    std::cin.ignore();
    sf::Thread cThread(client);
    sf::Thread sThread(server);
    cThread.launch();
    sThread.launch();
}
 
« Last Edit: August 05, 2012, 04:03:35 pm by Laurent »

Zephilinox

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: Help with sending/receiving packets on LAN/WAN
« Reply #1 on: August 05, 2012, 03:46:32 pm »
same problem, but with UDP sockets, and a lot more minimal.

#include <SFML/Network.hpp>
#include <string>
#include <iostream>


int main()
{
    sf::UdpSocket sSocket;
    sf::Packet sPacket;
    std::string sIP = "localhost";
    unsigned short int sPort = 2222;
    std::string sText = "hello";

    std::cout << "Enter IP" << std::endl;
    std::getline(std::cin, sIP);

    sSocket.bind(sPort);

    while (true)
    {
        sf::IpAddress IP = sIP;
        std::cout << "Enter Message" << std::endl;
        std::getline(std::cin, sText);
        sPacket << sText;
        sSocket.send(sPacket, sIP, sPort);
        sPacket.clear();

        sSocket.receive(sPacket, IP, sPort);
        sPacket >> sText;
        std::cout << IP << ": " << sText << std::endl;
        sf::sleep(sf::seconds(1));
    }
}
 
« Last Edit: August 05, 2012, 04:03:44 pm by Laurent »

Zephilinox

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: Help with sending/receiving packets on LAN/WAN
« Reply #2 on: August 09, 2012, 02:49:16 pm »
I noticed you(Laurent) edited my posts, but I don't see any difference in the code?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Help with sending/receiving packets on LAN/WAN
« Reply #3 on: August 09, 2012, 02:57:52 pm »
I probably just added the [code=cpp] tags.
Laurent Gomila - SFML developer

Zephilinox

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: Help with sending/receiving packets on LAN/WAN
« Reply #4 on: August 09, 2012, 03:04:50 pm »
Ah, okay.