1
Network / Server does not receive packets
« on: May 11, 2014, 01:04:23 pm »
client has successfully connected to server,
sends "Hi, I'm client" to the server without error.
But the server does not receive the packet.
What the problem is?
Server source using socket selector :
sends "Hi, I'm client" to the server without error.
But the server does not receive the packet.
What the problem is?
Server source using socket selector :
#include "SFML/Network.hpp"
#include <iostream>
#include <vector>
#include <list>
int main(){
sf::TcpListener listener;
listener.listen(55001);
std::list<sf::TcpSocket*> clients;
sf::SocketSelector selector;
selector.add(listener);
bool running = true;
std::cout << "server started" << std::endl;
while (running)
{
if (selector.wait())
{
if (selector.isReady(listener))
{
sf::TcpSocket* client = new sf::TcpSocket;
if (listener.accept(*client) == sf::Socket::Done)
{
std::cout << "new connection received from " << client->getRemoteAddress() << std::endl;
sf::Packet receivedPacket;
if (client->receive(receivedPacket) == sf::Socket::Done)
{
std::string message;
receivedPacket >> message;
std::cout << message << std::endl;
}
clients.push_back(client);
selector.add(*client);
}
else
{
std::cout << "error : server has no connection" << std::endl;
delete client;
}
}
else
{
for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
{
sf::TcpSocket& client = **it;
if (selector.isReady(client))
{
sf::Packet packet;
switch (client.receive(packet))
{
case sf::Socket::Done :
{
//...
std::string message;
packet >> message;
std::cout << client.getRemoteAddress() << " said: " << message << std::endl;
break;
}
case sf::Socket::Disconnected :
{
std::cout << "disconnect from " << client.getRemoteAddress() << std::endl;
selector.remove(client);
client.disconnect();
delete(&client);
clients.erase(it);
it--;
break;
}
}
}
}
}
}
}
return 0;
}
and this is my client source#include <iostream>
#include <vector>
#include <list>
int main(){
sf::TcpListener listener;
listener.listen(55001);
std::list<sf::TcpSocket*> clients;
sf::SocketSelector selector;
selector.add(listener);
bool running = true;
std::cout << "server started" << std::endl;
while (running)
{
if (selector.wait())
{
if (selector.isReady(listener))
{
sf::TcpSocket* client = new sf::TcpSocket;
if (listener.accept(*client) == sf::Socket::Done)
{
std::cout << "new connection received from " << client->getRemoteAddress() << std::endl;
sf::Packet receivedPacket;
if (client->receive(receivedPacket) == sf::Socket::Done)
{
std::string message;
receivedPacket >> message;
std::cout << message << std::endl;
}
clients.push_back(client);
selector.add(*client);
}
else
{
std::cout << "error : server has no connection" << std::endl;
delete client;
}
}
else
{
for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
{
sf::TcpSocket& client = **it;
if (selector.isReady(client))
{
sf::Packet packet;
switch (client.receive(packet))
{
case sf::Socket::Done :
{
//...
std::string message;
packet >> message;
std::cout << client.getRemoteAddress() << " said: " << message << std::endl;
break;
}
case sf::Socket::Disconnected :
{
std::cout << "disconnect from " << client.getRemoteAddress() << std::endl;
selector.remove(client);
client.disconnect();
delete(&client);
clients.erase(it);
it--;
break;
}
}
}
}
}
}
}
return 0;
}
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
sf::TcpSocket socket;
if (socket.connect("192.168.0.8", 55001) != sf::Socket::Done){
std::cout << "An error ocurred connecting to server" << std::endl;
}
else
{
std::cout << "connected" << std::endl;
}
socket.setBlocking(false);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed){
socket.disconnect();
window.close();
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Return)
{
std::cout << "a" << std::endl;
std::string message = "Hi, I am a client";
socket.send(message.c_str(), message.size() + 1);
}
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
#include <SFML/Network.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
sf::TcpSocket socket;
if (socket.connect("192.168.0.8", 55001) != sf::Socket::Done){
std::cout << "An error ocurred connecting to server" << std::endl;
}
else
{
std::cout << "connected" << std::endl;
}
socket.setBlocking(false);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed){
socket.disconnect();
window.close();
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Return)
{
std::cout << "a" << std::endl;
std::string message = "Hi, I am a client";
socket.send(message.c_str(), message.size() + 1);
}
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}