This is my "communications.h" from my project. It sends strings only. It can use either TCP or UDP and can have a server and client on the same program. But isnt perfect, might help you though.
#include <MikeNet.h>
#include <string>
#include <string.h>
using namespace std;
string Local_Host_Name = "127.0.0.1"; // host name or IP address of server, or null string to accept any IP
string MyIPAddress = "";
unsigned short ServerPort = 5001; // server port
int UdpMode = 4; //UM_PER_CLIENT;
int NumThreads = 0;
int NumInstances = 10;
// Setup packets
long long int RecvPacket = 0;
long long int SendPacket = 0;
#define MAX_CONNECT 10
int ActiveConnections = 0;
int ConnectSock[MAX_CONNECT]; // server
int ConnectInstance[MAX_CONNECT];
int iNextClientPort = 1;
int iAwaiting = -1;
int iData = -1;
int ComAwaitConnect()
{
iAwaiting = mnClientJoined(0);
if (iAwaiting > 0)
{
return 1;
}
return 0;
}
int ComAcceptConnect()
{
int i, s;
int iTemp = -1;
for(i = 0; i < MAX_CONNECT; i++)
{
s = ConnectSock[i];
if(s == -1)
{
iTemp = i;
i = MAX_CONNECT + 1;
}
} // Find Free Sock Slot
if (iTemp > -1)
{
ConnectInstance[iTemp] = 0;
ConnectSock[iTemp] = iAwaiting;
return 1;
}
return 0;
}
int ComIsConnected(int datasock)
{
if (ConnectSock[datasock] == -1 || ConnectInstance[datasock] == -1) return 1;
return mnClientConnected(ConnectInstance[datasock], ConnectSock[datasock]);
}
void ComClose(int datasock)
{
ConnectInstance[datasock] = -1;
ConnectSock[datasock] = -1;
}
void ComDisconnect(int datasock)
{
// This crashes for some reason???
//mnDisconnectClient(ConnectInstance[datasock], ConnectSock[datasock]);
}
void ComRelease()
{
int Result = mnFinish(-1);
}
int ComPutData(int datasock, string *information, string *error)
{
*error = "";
mnAddStringSTD(SendPacket, *information, true);
mnSendTCP(ConnectInstance[datasock], SendPacket, ConnectSock[datasock], false, false);
return 0;
}
int ComPutDataFast(int datasock, string *information, string *error)
{
*error = "";
mnAddStringSTD(SendPacket, *information, true);
mnSendUDP(ConnectInstance[datasock], SendPacket, ConnectSock[datasock], false, false);
return 0;
}
int ComAwaitData(int datasock)
{
iData = mnRecvTCP(ConnectInstance[datasock], RecvPacket, ConnectSock[datasock]);
if(iData > 0)
{
return 1;
}
iData = mnRecvUDP(ConnectInstance[datasock], RecvPacket, ConnectSock[datasock], 0);
if(iData > 0)
{
return 1;
}
return 0;
}
int ComGetData(int DataSock, string *information, string *error)
{
*error = "";
string sInfo = "";
sInfo = mnGetStringSTD(RecvPacket, 0);
*information = sInfo;
return sInfo.length();
}
int ComGetIpAddress(string *IPAddress)
{
*IPAddress = mnGetLocalIP(0);
return 0;
}
int ComLoad(int Connections, int *error)
{
*error = 0;
// Setup Who Connected Information
for (int a = 0; a < MAX_CONNECT; a++)
{
ConnectInstance[a] = -1;
ConnectSock[a] = -1; // server
}
// Setup packets
RecvPacket = mnCreatePacket();
SendPacket = mnCreatePacket();
mnSetMemorySize(SendPacket,1024);
// Start MikeNet
mnStart(NumInstances,NumThreads);
// Disable the Error Window Box
if (mnGetErrorMode(EM_MESSAGE_BOX)) mnToggleErrorMode(EM_MESSAGE_BOX);
int Code = ComGetIpAddress(&MyIPAddress);
if(Code < 0)
{
*error = Code;
return 0;
}
return 1;
}
int ComServerStart(short portmod, string *error)
{
*error = "";
unsigned short LocalPort = ServerPort + portmod;
char *LocalIP;
LocalIP = &MyIPAddress[0];
mnSetLocal(0, LocalIP, LocalPort, LocalIP, LocalPort);
// Attempt to start the server
int Result = mnStartServer(0, MAX_CONNECT, 0, UdpMode);
return Result + 1;
}
int ComClientStart(int datasock, string ServerIPAddress, short portmod)
{
unsigned short ConnectPort = ServerPort + portmod;
char *ConnectIP;
ConnectIP = &ServerIPAddress[0];
int Result = mnConnect(iNextClientPort,ConnectIP,ConnectPort,ConnectIP,ConnectPort,10, true);
ConnectInstance[datasock] = iNextClientPort;
ConnectSock[datasock] = Result;
iNextClientPort++;
return Result;
}