1
Network / Re: Client can't reconnect to server
« on: March 30, 2013, 12:46:15 am »
I changed my server code to this and it works fine now, but I don't know exactly why :
Also, for future reference :
#include <iostream>
#include <list>
#include <SFML/Network.hpp>
int main(int argc, char **argv) {
std::cout << "Update server initiated!" << std::endl;
std::vector<sf::TcpSocket*> clients;
sf::TcpListener listener;
if(listener.listen(90) != sf::Socket::Status::Done)
std::cout << "Couldn't listen to port 90!" << std::endl;
listener.setBlocking(false);
sf::SocketSelector selector;
selector.add(listener);
while(true) {
if(selector.wait()) {
if(selector.isReady(listener)) {
sf::TcpSocket* client = new sf::TcpSocket;
if(listener.accept(*client) == sf::Socket::Status::Done) {
std::cout << "Connection from client " << client->getRemoteAddress() << "!" << std::endl;
clients.push_back(client);
selector.add(*client);
}
} else {
for(int i = 0; i < clients.size(); ++i) {
if(selector.isReady(*clients[i])) {
sf::Packet packet;
if(clients[i]->receive(packet) == sf::Socket::Status::Done) {
std::string data;
packet >> data;
if(data[0] == '1') {
std::string message = data.substr(1);
if(message == "abstergo") {
sf::Packet response;
std::string responseString = "1";
response << responseString;
std::cout << "Client " << clients[i]->getRemoteAddress() << " logged in." << std::endl;
clients[i]->send(response);
}
} if(data[0] == '2') {
std::cout << "Received hearbeat from " << clients[i]->getRemoteAddress() << "." << std::endl;
}
} else {
std::cout << "Timeout detected from " << clients[i]->getRemoteAddress() << std::endl;
selector.remove(*clients[i]);
delete clients[i];
}
}
}
}
}
}
std::cin.get();
return 0;
}
#include <list>
#include <SFML/Network.hpp>
int main(int argc, char **argv) {
std::cout << "Update server initiated!" << std::endl;
std::vector<sf::TcpSocket*> clients;
sf::TcpListener listener;
if(listener.listen(90) != sf::Socket::Status::Done)
std::cout << "Couldn't listen to port 90!" << std::endl;
listener.setBlocking(false);
sf::SocketSelector selector;
selector.add(listener);
while(true) {
if(selector.wait()) {
if(selector.isReady(listener)) {
sf::TcpSocket* client = new sf::TcpSocket;
if(listener.accept(*client) == sf::Socket::Status::Done) {
std::cout << "Connection from client " << client->getRemoteAddress() << "!" << std::endl;
clients.push_back(client);
selector.add(*client);
}
} else {
for(int i = 0; i < clients.size(); ++i) {
if(selector.isReady(*clients[i])) {
sf::Packet packet;
if(clients[i]->receive(packet) == sf::Socket::Status::Done) {
std::string data;
packet >> data;
if(data[0] == '1') {
std::string message = data.substr(1);
if(message == "abstergo") {
sf::Packet response;
std::string responseString = "1";
response << responseString;
std::cout << "Client " << clients[i]->getRemoteAddress() << " logged in." << std::endl;
clients[i]->send(response);
}
} if(data[0] == '2') {
std::cout << "Received hearbeat from " << clients[i]->getRemoteAddress() << "." << std::endl;
}
} else {
std::cout << "Timeout detected from " << clients[i]->getRemoteAddress() << std::endl;
selector.remove(*clients[i]);
delete clients[i];
}
}
}
}
}
}
std::cin.get();
return 0;
}
Also, for future reference :
#include <iostream>
#include <vector>
#include <string>
#include <SFML/Network.hpp>
void doServer() {
std::cout << "Started server. " << std::endl;
std::vector<sf::TcpSocket*> clients;
sf::TcpListener listener;
listener.listen(90);
sf::SocketSelector selector;
selector.add(listener);
while(true) {
if(selector.wait()) {
if(selector.isReady(listener)) {
sf::TcpSocket* client = new sf::TcpSocket();
if(listener.accept(*client) == sf::Socket::Status::Done) {
std::cout << "Accepted new connection from " << client->getRemoteAddress() << ". " << std::endl;
clients.push_back(client);
selector.add(*client);
}
} else {
for(int i = 0; i < clients.size(); ++i) {
if(selector.isReady(*clients[i])) {
sf::Packet packet;
if(clients[i]->receive(packet) == sf::Socket::Status::Done) {
std::string message;
packet >> message;
std::cout << "Received message from " << clients[i]->getRemoteAddress() << " : " << message << std::endl;
} else {
std::cout << "Client " << clients[i]->getRemoteAddress() << " disconnected." << std::endl;
selector.remove(*clients[i]);
delete clients[i];
}
}
}
}
}
}
}
void doClient() {
std::cout << "Started client. " << std::endl;
sf::TcpSocket socket;
if(! socket.connect("127.0.0.1", 90) == sf::Socket::Status::Done) {
std::cout << "Couldn't connect to server." << std::endl;
return;
} else
std::cout << "Connected to server." << std::endl;
std::cin.ignore(1000);
std::cin.get();
}
int main(int argc, char **argv) {
std::cout << "c/s ? ";
std::string input;
std::cin >> input;
if(input == "c")
doClient();
if(input == "s")
doServer();
std::cin.get();
return 0;
}
#include <vector>
#include <string>
#include <SFML/Network.hpp>
void doServer() {
std::cout << "Started server. " << std::endl;
std::vector<sf::TcpSocket*> clients;
sf::TcpListener listener;
listener.listen(90);
sf::SocketSelector selector;
selector.add(listener);
while(true) {
if(selector.wait()) {
if(selector.isReady(listener)) {
sf::TcpSocket* client = new sf::TcpSocket();
if(listener.accept(*client) == sf::Socket::Status::Done) {
std::cout << "Accepted new connection from " << client->getRemoteAddress() << ". " << std::endl;
clients.push_back(client);
selector.add(*client);
}
} else {
for(int i = 0; i < clients.size(); ++i) {
if(selector.isReady(*clients[i])) {
sf::Packet packet;
if(clients[i]->receive(packet) == sf::Socket::Status::Done) {
std::string message;
packet >> message;
std::cout << "Received message from " << clients[i]->getRemoteAddress() << " : " << message << std::endl;
} else {
std::cout << "Client " << clients[i]->getRemoteAddress() << " disconnected." << std::endl;
selector.remove(*clients[i]);
delete clients[i];
}
}
}
}
}
}
}
void doClient() {
std::cout << "Started client. " << std::endl;
sf::TcpSocket socket;
if(! socket.connect("127.0.0.1", 90) == sf::Socket::Status::Done) {
std::cout << "Couldn't connect to server." << std::endl;
return;
} else
std::cout << "Connected to server." << std::endl;
std::cin.ignore(1000);
std::cin.get();
}
int main(int argc, char **argv) {
std::cout << "c/s ? ";
std::string input;
std::cin >> input;
if(input == "c")
doClient();
if(input == "s")
doServer();
std::cin.get();
return 0;
}