alright, I've been working on this for about 5 hours today alone, and I still can't get anything working. Can someone tell me what I'm doing wrong? I'm getting no output from the server at all, nor the client.
Client:
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
// Create the main rendering window
sf::IPAddress addy("76.121.76.188");
sf::SocketUDP Socket;
// Create bytes to send
char Buffer[] = "Player joining.";
// Send data to "192.168.0.2" on port 4444
if (Socket.Send(Buffer, sizeof(Buffer), addy, 4444) != sf::Socket::Done)
{
cout << "Sending failed." << endl;
}
while (true)
{
// Process events
if (addy.IsValid()){
//recieve enemy posititon
sf::SocketUDP receiverSocket;
// Bind it (listen) to the port 4444
if (!receiverSocket.Bind(4444))
{
cout << "binding failed" << endl;
}
//get data
char buffer[128];
std::size_t Received;
sf::IPAddress Sender;
unsigned short Port;
if (receiverSocket.Receive(buffer, sizeof(buffer), Received, Sender, Port) != sf::Socket::Done)
{
cout << "error receiveing packet" << endl;
}
else{
cout<<buffer<<endl;
}
//send player position
sf::SocketUDP Socket;
ostringstream sin;
sin << "Goodbye.";
sin.str().c_str();
// Send data to "192.168.0.2" on port 4444
if (Socket.Send(sin.str().c_str(), sizeof(sin.str().c_str()), addy, 4444) != sf::Socket::Done)
{
cout << "Sending failed" << endl;
}
}
}
return EXIT_SUCCESS;
}
Server:
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <sstream>
#include <vector>
#include "player.h"
#include "playerthread.h"
using namespace std;
int main()
{
bool done = false;
int numConnected = 0;
sf::IPAddress playerIP[2];
Playerthread *pthread[2];
// Start game loop
while (!done)
{
sf::SocketUDP receiverSocket;
// Bind it (listen) to the port 4444
if (!receiverSocket.Bind(4444))
{
cout << "binding failed" << endl;
}
//get data
char buffer[128];
std::size_t Received;
sf::IPAddress Sender;
unsigned short Port;
if (receiverSocket.Receive(buffer, sizeof(buffer), Received, Sender, Port) != sf::Socket::Done)
{
cout << "error receiveing packet" << endl;
}
else{
playerIP[numConnected] = Sender;
pthread[numConnected] = new Playerthread(playerIP[numConnected]);
pthread[numConnected]->startThread();
numConnected++;
}
// Show the address of the sender
std::cout << Sender << std::endl;
// Show the message
std::cout << buffer << std::endl; // "Hi guys !"
//close socket
receiverSocket.Close();
}
return EXIT_SUCCESS;
}
Server Thread:
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
class Playerthread : private sf::Thread{
public:
int get_RetLevel();
sf::IPAddress playerIP;
Playerthread(sf::IPAddress &nplayerIP){
playerIP = nplayerIP;
}
void startThread(){
Launch();
}
private:
virtual void Run(){
while(true){
if (playerIP.IsValid()){
//send enemy position
sf::SocketUDP Socket;
ostringstream sin;
sin << "enemy local";
sin.str().c_str();
// Send data to "192.168.0.2" on port 4444
if (Socket.Send(sin.str().c_str(), sizeof(sin.str().c_str()), playerIP, 4444) != sf::Socket::Done)
{
cout << "Sending failed" << endl;
}
}
//recieve player posititon
sf::SocketUDP receiverSocket;
// Bind it (listen) to the port 4444
if (!receiverSocket.Bind(4444))
{
cout << "binding failed" << endl;
}
//get data
char buffer[128];
std::size_t Received;
sf::IPAddress Sender;
unsigned short Port;
if (receiverSocket.Receive(buffer, sizeof(buffer), Received, Sender, Port) != sf::Socket::Done)
{
cout << "error receiveing packet" << endl;
}
else{
if(Sender.ToString().compare(playerIP.ToString())){
cout << "thread" << buffer << endl;
}
}
}
}
};