Ok, here is my code, please excuse the debug variables etc.
I converted you "Chat Server" DBP example. I'm using this with DBP compiled Chat client exe.
I also have a problem setting Multi-threaded debug, using just Multi-threaded seems ok.
Just copy it and run it. Let me know if I do something wrong..
// Chat Server
#include "DarkGDK.h"
#include "winsock_plugin.h"
class CClient
{
public:
CClient() {}
CClient(int def_id, DWORD def_channel) : id(def_id), channel(def_channel) {}
virtual ~CClient() {}
int id;
DWORD channel;
};
void DarkGDK ( void )
{
// set sync on and sync rate to 60 frames per second
dbSyncOff ( );
dbSyncRate ( 60 );
//specify max. amount of clients.
#define MAX_CLIENTS 3970
//specify local port to listen for connection requests.
#define LOCAL_PORT 4321
std::vector<CClient*> clients;
DWORD listen_channel;
int clients_connected = 0;
size_t buffer_size = 512;
LPSTR buffer = new char[buffer_size]; // Error messages buffer.
dbDisableEscapekey();
dbSetWindowOn();
//initialize winsock
ws::make();
if (ws::get_error())
{
//an error has occurred, print the error message and quit.
dbPrint( ws::get_error_msg(buffer, buffer_size) );
dbWaitKey();
ws::clean_up();
dbEnd();
}
listen_channel = ws::listen_on(LOCAL_PORT);
if (ws::get_channel_error(listen_channel))
{
//an error has occurred, print the error message and quit.
dbPrint ( ws::get_channel_error_msg(listen_channel, buffer, buffer_size) );
dbWaitKey();
ws::clean_up();
dbEnd();
}
dbPrint ("Chat server is UP and listening for connections/messages...");
dbSync();
dbSync();
// make a cube
dbMakeObjectCube ( 1, 10 );
//the main server loop consists of following:
//1. wait for connection requests, accept them.
//2. receive data, and transmit to all clients.
// loop until the escape key is pressed
while ( LoopGDK ( ) )
{
//refresh all channels. do this once in the main loop.
ws::refresh_channels();
if (ws::get_error())
{
//an error has occurred, print the error message and quit.
dbPrint ( ws::get_error_msg(buffer, buffer_size) );
dbWaitKey();
ws::clean_up();
dbEnd();
}
if (ws::channel_data_waiting(listen_channel))
{
//someone wants to connect.
if (clients_connected < MAX_CLIENTS)
{
//accept connection request.
clients.push_back(new CClient(0, 0) );
// new_client IS clients.size()
int len2 = ws::channel_data_len(listen_channel);
clients[clients.size()-1]->channel = ws::accept_connection(listen_channel);
clients[clients.size()-1]->id = clients.size();
if (!ws::get_channel_error(clients[clients.size()-1]->channel) )
{
clients_connected++;
//send a message to all clients that a new client has connected.
for (int i=0; i<clients_connected; i++)
{
//LPSTR aaa = strcat("client_", dbStr(clients[clients.size()].id) );
ws::send_string( clients[i]->channel, "client_ has connected!" );
//ws::send_string( clients[i].channel, "client_" + dbStr(clients[clients.size()].id) + " has connected!" );
}
dbPrint( "client_" ); //, dbStr(clients[clients.size()].id)));
}
else
{
dbPrint( ws::get_channel_error_msg(clients[clients.size()-1]->channel, buffer, buffer_size) );
ws::delete_channel(clients[clients.size()-1]->channel);
delete &clients[clients.size()-1];
clients.pop_back();
}
}
else
{
//max clients reached.
dbPrint( "connection request rejected because max amount of clients are already connected." );
}
}
// Check for incoming chat messages
for (int i=0; i < clients_connected; i++)
{
DWORD c = clients[i]->channel;
int r = ws::channel_data_waiting(c);
if (r)
//if ( ws::channel_data_waiting(clients[i]->channel) )
{
int len1 = ws::channel_data_len(c);
LPSTR message = ws::recv_string( clients[i]->channel, buffer, buffer_size );
if ( strlen(message) == 0 )
{
dbPrint("disconnected"); // "client_" + str$(clients(i).id) + " has disconnected."
// Send a message to all clients that client has disconnected.
for (int j=0; j < clients_connected; j++)
{
if (j != i)
{
ws::send_string( clients[j]->channel, "client_ has disconnected."); // + str$(clients(i).id) + " has disconnected."
}
}
clients_connected--;
ws::delete_channel( clients[i]->channel);
delete &clients[i];
clients.erase(&clients[i]);
//dec i `need to do this because the next array element is now at the current position.
i--;
}
else
{
//forward messsage to all other clients (including the sender).
message = "client_"; // + str$(clients(i).id) + ": " + message$
for (int j=0; j<clients_connected; j++)
{
ws::send_string( clients[j]->channel, message );
}
}
}
}
if ( dbEscapeKey ( ) )
return;
// rotate the object
dbRotateObject ( 1, dbObjectAngleX ( 1 ) + 0.1f, dbObjectAngleY ( 1 ) + 0.1f, dbObjectAngleZ ( 1 ) + 0.1f );
// update screen
//dbSync ( );
}
//shutdown winsock
ws::clean_up();
}
This gets the connection of the client, switches to new port/channel but doesn't get the string from the client..
But it sends strings to the client(s) ok.