Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

Separate Compilation

$
0
0
Hey, guys, I have just started using separate compilation,and i need your help guys. The following project is what i have done by using separate compiling and it goes wrong.

This is my "HPP" file
#ifndef NETWORK_HPP
#define NETWORK_HPP

#include <iostream>
#include <cstdlib>
#include <string>
#include <SFML/Network.hpp>
#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


This is my "CPP file
#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 main_Network()
{
	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");



}


This is my "Main CPP" File
#include "network.hpp"

int main()
{

	void mainNetwork();


}


There have some errors when compiling this code, it show [-fpermissive]

Ur help be appreciated.

Viewing all articles
Browse latest Browse all 2703

Trending Articles