Hello, I just started C++ and I need a multitthreaded server which broadcasts all messages by clients to every other client.
The piece of crap I came up with looks like this - and shows lots of errors, so I'm pretty sure its totally wrong - i just dont know Whats wrong.
Code:
#include "ServerSocket.h"
#include "SocketException.h"
#include <string>
#include <iostream>
int main ( int argc, int argv[] )
{
std::cout << "running....\n";
try
{
while ( true )
{
connection::open();
try
{
while ( true )
{
std::string data;
new_sock >> data;
broadcast(data);
}
}
catch ( SocketException& ) {}
}
}
catch ( SocketException& e )
{
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}
return 0;
};
struct connection
{
ServerSocket server(3000);
ServerSocket socket[100];
bool i[100];
int reserve()
{
for(int iid=0;iid < 100;iid++)
{
if(!i[iid])
{
i[iid] = true;
return iid;
}
}
};
bool broadcast(std::string data)
{
for(bool iid=0;iid < 100;iid++)
{
if(i[iid])
{
socket[iid] << data;
}
}
return true;
};
bool open()
{
int iid=reserve();
server.accept (socket[iid]);
try
{
while ( true )
{
std::string data;
socket[iid] >> data;
broadcast(data);
}
}
catch ( SocketException& ) {}
release(iid);
return true;
};
bool release(int iid)
{
i[iid] = false;
return true;
};
};