1
Network / Sending Message
« on: December 04, 2018, 07:53:14 pm »
Hello everyone i have a question about sending message. I wrote a code for sending a message and receiving it but after sent the message you have to wait for the other side's but i need to send as much as i want and when a message sent i need to receive it even while writing a message from other side. How may i do that using console screen?
This is what i've written. Logic is: If the ip is different than which i've chosen then computer that connects is client. Please help me.
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
using namespace sf;
class Connection
{
public:
int Port;
bool isDoneServer, isDoneClient;
char buffer[2000];
std::string Text;
std::size_t Received;
public:
TcpListener listener;
IpAddress ipAddress;
TcpSocket Socket;
public:
inline Connection();
inline ~Connection();
inline auto Ip_Match(IpAddress) -> bool;
};
inline Connection::Connection()
{
this->Port = 2000;
this->isDoneServer = true;
this->isDoneClient = true;
this->Text = "";
}
inline Connection::~Connection(){}
inline auto Connection::Ip_Match(IpAddress ipAddress) -> bool
{
if(ipAddress == "AnyIp") { return true; }
else { return false; }
}
int main()
{
Connection *Call = new Connection();
Call->ipAddress = IpAddress::getLocalAddress();
if(Call->Ip_Match(Call->ipAddress))
{
Call->listener.listen(Call->Port);
Call->listener.accept(Call->Socket);
while(Call->isDoneServer)
{
std::cout << "Server: ";
std::getline(std::cin, Call->Text);
Call->Socket.send(Call->Text.c_str(), Call->Text.length() + 1);
Call->Socket.receive(Call->buffer, sizeof(Call->buffer), Call->Received);
if(Call->Received)
{
std::cout << "Client: " << Call->buffer << std::endl;
}
if(Call->Text == "out" || Call->Text == "Out" || Call->Text == "OUT")
{
Call->isDoneServer = false;
}
}
}
else
{
Call->Socket.connect("AnyIp", Call->Port);
while(Call->isDoneClient)
{
Call->Socket.receive(Call->buffer, sizeof(Call->buffer), Call->Received);
if(Call->Received)
{
std::cout << "Server: " << Call->buffer << std::endl;
}
std::cout << "Client: ";
std::getline(std::cin, Call->Text);
Call->Socket.send(Call->Text.c_str(), Call->Text.length() + 1);
}
}
return EXIT_SUCCESS;
}
#include <SFML/Network.hpp>
#include <iostream>
using namespace sf;
class Connection
{
public:
int Port;
bool isDoneServer, isDoneClient;
char buffer[2000];
std::string Text;
std::size_t Received;
public:
TcpListener listener;
IpAddress ipAddress;
TcpSocket Socket;
public:
inline Connection();
inline ~Connection();
inline auto Ip_Match(IpAddress) -> bool;
};
inline Connection::Connection()
{
this->Port = 2000;
this->isDoneServer = true;
this->isDoneClient = true;
this->Text = "";
}
inline Connection::~Connection(){}
inline auto Connection::Ip_Match(IpAddress ipAddress) -> bool
{
if(ipAddress == "AnyIp") { return true; }
else { return false; }
}
int main()
{
Connection *Call = new Connection();
Call->ipAddress = IpAddress::getLocalAddress();
if(Call->Ip_Match(Call->ipAddress))
{
Call->listener.listen(Call->Port);
Call->listener.accept(Call->Socket);
while(Call->isDoneServer)
{
std::cout << "Server: ";
std::getline(std::cin, Call->Text);
Call->Socket.send(Call->Text.c_str(), Call->Text.length() + 1);
Call->Socket.receive(Call->buffer, sizeof(Call->buffer), Call->Received);
if(Call->Received)
{
std::cout << "Client: " << Call->buffer << std::endl;
}
if(Call->Text == "out" || Call->Text == "Out" || Call->Text == "OUT")
{
Call->isDoneServer = false;
}
}
}
else
{
Call->Socket.connect("AnyIp", Call->Port);
while(Call->isDoneClient)
{
Call->Socket.receive(Call->buffer, sizeof(Call->buffer), Call->Received);
if(Call->Received)
{
std::cout << "Server: " << Call->buffer << std::endl;
}
std::cout << "Client: ";
std::getline(std::cin, Call->Text);
Call->Socket.send(Call->Text.c_str(), Call->Text.length() + 1);
}
}
return EXIT_SUCCESS;
}
This is what i've written. Logic is: If the ip is different than which i've chosen then computer that connects is client. Please help me.