It could be how your #including your includes.
Try declaring stuff like this:
#include <string>
#include "MikeNet.h"
using namespace std;
Tell me if you can compile these two programs.
server.cpp
// Send Message (server)
// A program that creates a server, and if a client has joined, allows you to broadcast messages to them.
// The basic libraries that we will need for this program
#include <iostream>
#include <string>
#include <MikeNet.h>
using namespace std;
int main() {
// Basic variables.
int clientID; // Stores the ID of a client.
bool joined = false; // This is used to tell if the client has joined.
bool serverRunning; // This variable is used to monitor if the server was successfully started, and running.
string message; // The message we want to send to the clinet.
// Setup the packets. Were not reciving any real messages, so we don't need to setup a reciving packet.
long long int sendPacket = mnCreatePacket();
mnSetMemorySize(sendPacket, 256);
// Start the server.
cout << "Starting the server...\n\n";
mnStart(1, 0);
// Set the IPs to use, and the ports.
mnSetLocal(0, "127.0.0.1", 1770, "127.0.0.1", 1771);
serverRunning = mnStartServer(0, 1, 5, UM_CATCH_ALL_NO); // Right now, we only want one client connecting.
// Displays the basic server connection info.
cout << "\tLocal TCP IP: " << mnGetLocalIPTCP(0) << "\n";
cout << "\tLocal TCP Port: " << mnGetLocalPortTCP(0) << "\n\n";
cout << "\tLocal UDP IP: " << mnGetLocalIPUDP(0) << "\n";
cout << "\tLocal UDP Port: " << mnGetLocalPortUDP(0) << "\n\n";
cout << "Server is now monitoring if clients join the server:\n----------\n\n";
// Now we check if the server is running, and if clients have joined the server
while (serverRunning == 0) {
// If the client has joined, we will log their clientID, then send a message to it.
clientID = mnClientJoined(0);
if (clientID > 0) {
joined = true;
cout << "Client Joined, ClientID: ";
cout << clientID;
}
// If the client is "online", then send it messages.
if (joined == true) {
cout << "\n\nEnter a message for the client to recive: ";
getline(cin, message);
if (message != "") {
mnAddStringSTD(sendPacket, message, true);
mnSendTCP(0, sendPacket, 1, false, true);
}
}
// We here check to see if the client left.
clientID = mnClientLeft(0);
if (clientID > 0) {
joined = false;
cout << "Client has left, ClientID: ";
cout << clientID;
}
}
// This just cleans up DarkNet, it ends the server in this case.
mnFinish(0);
return 0;
}
client.cpp
// Send Message (client)
// A program that recives messages sent out from the server
// The basic libraries that we will need for this program
#include <iostream>
#include <string>
#include <MikeNet.h>
using namespace std;
// Function prototypes.
bool connectionMessage(int connected); // This function is here just to clean up the main() function a bit. It returns true if the connections works, false if it does not.
int main() {
// Basic variables.
bool running = true; // This variables is just use to run the main loop.
bool connMessageDisplayed = false; // Here, were just creating this variable to see if the connection message was displayed.
string message; // The message we will recive from the server.
int check; // Dheck is used to see if there is a new message.
// Setup the packets. Were not sending any messages, so we don't need to setup a sending packet.
long long int recvPacket = mnCreatePacket();
// Start DarkNet and connect to the server.
mnStart(1, 0);
cout << "Connecting to the server...\n\n";
int connected = mnConnect(0, "127.0.0.1", 1770, "127.0.0.1", 1771, 5, true);
// Run the main loop.
while (running == true) {
// Here, we try to connect to the server, display a message based upon if it was a success or not.
if (connMessageDisplayed == false) {
running = connectionMessage(connected);
connMessageDisplayed = true;
}
check = mnRecvTCP(0, recvPacket, NULL);
if (check > 0) {
message = mnGetStringSTD(recvPacket, 0);
cout << "Message from server:\n";
cout << "\t" << message << "\n\n";
}
}
// This just cleans up DarkNet, it disconnects from the server in this case.
mnFinish(0);
return 0;
}
bool connectionMessage(int connected) {
if (connected == 1) {
// If connected is 1, this means we've got a successful connection and are currently in the server.
cout << "Connection successful.\n";
cout << "ClientID: " << mnGetClientID(0) << "\n\n";
return true;
}
else if (connected == 0) {
// If connected is 0, we didn't join the server, out connection timmed out.
cout << "Connection unsuccessful, connection timed out.\n";
system("PAUSE");
return false;
}
else if (connected == -1) {
// If connected is -1, it means an error occured during connection.
cout << "Connection unsuccessful, an error occured during the connection.\n";
system("PAUSE");
return false;
}
else if (connected == -2) {
// If connected is -2, we've made connection to the server, but it's full, so we can't join it.
cout << "Not able to join the server, it is full.\n";
system("PAUSE");
return false;
}
}
http://www.darkgdk.us/ <- You can now submit pages, upload images, yet were lacking content. We need your help!