Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Separate Compilation

This is my HPP file
[code]
#ifndef NETWORK_HPP
#define NETWORK_HPP

#include
#include
#include
#include
#define DEBUG false
using namespace std;

class MyHost
{
public:
MyHost(unsigned short port = 50001)
: myPort(port)
{
mySocket.setBlocking(false);
}
sf::Socket::Status initiateAsServer();
sf::Socket::Status connectToServer(sf::IpAddress server);
sf::Socket::Status sendMessage(string outMsg);
sf::Socket::Status receiveMessage(string& inMsg);
private:
unsigned short myPort;
sf::TcpSocket mySocket;
};

sf::Socket::Status MyHost::initiateAsServer();
sf::Socket::Status MyHost::connectToServer(sf::IpAddress server);
sf::Socket::Status MyHost::sendMessage(string outMsg);
sf::Socket::Status MyHost::receiveMessage(string& inMsg);
void mainNetwork();




#endif
[/code]

This is my the other CPP file
[code]
#include "network.hpp"

class MyHost
{
public:
MyHost(unsigned short port = 50001)
: myPort(port)
{
mySocket.setBlocking(false);
}
sf::Socket::Status initiateAsServer();
sf::Socket::Status connectToServer(sf::IpAddress server);
sf::Socket::Status sendMessage(string outMsg);
sf::Socket::Status receiveMessage(string& inMsg);
private:
unsigned short myPort;
sf::TcpSocket mySocket;
};

sf::Socket::Status MyHost::initiateAsServer()
{
// Create a server socket to accept new connections
sf::TcpListener listener;
sf::Socket::Status status;

// Listen to the given port for incoming connections
status = listener.listen(myPort);
if (status != sf::Socket::Done)
{
if (DEBUG) cout << "initiateAsServer(): Listen Failed!" << endl;
if (DEBUG) cout << status << endl;
return status;
}
if (DEBUG) cout << "Server is listening to port " << myPort << ", waiting for connections... " << endl;

// Wait for a connection
status = listener.accept(mySocket);
if (status != sf::Socket::Done)
{
if (DEBUG) cout << "initiateAsServer(): Accept Failed!" << endl;
if (DEBUG) cout << status << endl;
return status;
}
if (DEBUG) cout << "Client connected: " << mySocket.getRemoteAddress() << endl;
if (DEBUG) cout << status << endl;
return status;
}

sf::Socket::Status MyHost::connectToServer(sf::IpAddress server)
{
sf::Socket::Status status;

// Connect to the server
status = mySocket.connect(server, myPort);
if (status != sf::Socket::Done)
{
if (DEBUG) cout << "connectToServer(): Connect Failed!" << endl;
if (DEBUG) cout << status << endl;
return status;
}
if (DEBUG) cout << "Connected to server " << server << endl;
if (DEBUG) cout << status << endl;
return status;
}

sf::Socket::Status MyHost::sendMessage(string outMsg)
{
sf::Socket::Status status;

// Send a message to the connected client
status = mySocket.send(outMsg.c_str(), outMsg.length());
if (status != sf::Socket::Done)
{
if (DEBUG) cout << "sendMessage(): Send Failed!" << endl;
if (DEBUG) cout << status << endl;
return status;
}
//cout << "Message [" << outMsg << "] sent." << endl;
if (DEBUG) cout << status << endl;
return status;
}

sf::Socket::Status MyHost::receiveMessage(string& inMsg)
{
sf::Socket::Status status;

// Receive a message back from the client
char in[128];
size_t sizeReceived;
status = mySocket.receive(in, sizeof(in), sizeReceived);
if (status != sf::Socket::Done)
{
if (DEBUG) cout << "receiveMessage(): Receive Failed!" << endl;
if (DEBUG) cout << status << endl;
return status;
}
inMsg = in;
inMsg = inMsg.substr(0,sizeReceived);
//cout << "Message [" << inMsg << "] received." << endl;
if (DEBUG) cout << status << endl;
return status;
}

void mainNetwork()
{
bool hostTypeValid;
char hostType;
do{
cout<<"Enter s for server "<<endl;
cout<<"Enter c for client "<<endl;
cout<<" -> ";
cin>>hostType;

if( hostType == 's' || hostType == 'S' || hostType == 'c' || hostType == 'C' )
hostTypeValid = true;
else
{
cout<<"Please enter correctly"<<endl;
hostTypeValid = false;
}
}while( hostTypeValid == false );

MyHost host;
sf::Socket::Status hostStatus;
if( hostType == 's' || hostType == 'S' )
{
cout<<"Turning ON server"<<endl;
cout<<"Waiting for another user to connect..."<<endl;
hostStatus = host.initiateAsServer();
if( hostStatus == sf::Socket::Done )
cout<<"USER CONNECTED..."<<endl;
}

else if( hostType == 'c' || hostType == 'C' )
{
string serverString;
sf::IpAddress iPaddress;
do{
cout<<"Enter server IP address -> ";
cin>>serverString;
iPaddress = sf::IpAddress(serverString);
}while(iPaddress == sf::IpAddress::None);

cout<<"Connecting to server..."<<endl;
do{
hostStatus = host.connectToServer(iPaddress);
}while( hostStatus != sf::Socket::Done );

cout<<"You have connect to the server"<<endl;
cout<<"ENJOY!!!"<<endl;
}




system("pause");



}
[/code]

This is my main CPP file
[code]
#include "network.hpp"

int main()
{

void mainNetwork();


}
[/code]

i have just started doing separate compilation, so i need your help.
My question is, what's wrong with my aboce code here, the compiler show me something like [ifpermissive]

Sign In or Register to comment.