Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by Craig Sheahan ( 16 years ago )
#define TCPLIB_MES
#include <tcplib.h>
#include <stringlib.h>
#include <iostream>
#include <vector>
using namespace std;

static boost::asio::io_service io_service;

static void process_client() {
 while (1) {
  if (boost::this_thread::interruption_requested()) {
   //mes(MES_STATUS,"Network worker thread 2 (Read/Write) ended");
   break;
  }
  io_service.run();
  Sleep(1); //0 to allow more than one thousand connections per second, DDoS though?
 }
}
static void test_tcp() {
 char selection;
 cout << "Select service type" << endl << endl;
 cout << "1: Client" << endl;
 cout << "2: Server" << endl << endl;
 cout << "Selection: ";
 cin >> selection;
 system&#40;"cls"&#41;;
 if (selection == '1') {
  cout << "Commands:" << endl << "Quit: quit, cancel" << endl << "Connect: connect <ip> <port>" << endl << "Disconnect: disconnect, dc, quit" << endl << endl;
  boost::thread *worker = new boost::thread(boost::bind(&process;_client));
  tcpnet::client tcpc("192.168.1.114","2030");
  tcpc.start(&io;_service);
  char *temp1, temp2[81];
  while (1) {
   cin.getline(temp2,80);
   if (!strcmp(temp2,"quit") || !strcmp(temp2,"cancel")) {
    tcpc.disconnect();
    break;
   } else if (str::beginswith(temp2,"connect")) { //and params?
   } else if (!strcmp(temp2,"disconnect") || !strcmp(temp2,"dc")) { //Test disconnection
    tcpc.disconnect();
   } else {
    if (tcpc.is_dead()) {
     mes(MES_INFO,"TCP test will now end due to server disconnection...");
     tcpc.disconnect();
    } else {
     tcpc.send(temp2);
     if ((temp1 = tcpc.receive()) != nullptr) {
      mes(MES_INFO,str::concatenate(3,"Received message '",temp1,"'"));
      //delete temp1; //Won't need cuz it's static?
     }
    }
   }
   Sleep(1);
  }
  worker->interrupt();
  worker->join();
 } else if (selection == '2') {
  vector<tcpnet::client::pointer> clients;
  tcpnet::server tcps(2030);
  char* temp = nullptr;
  while (1) {
   int i = 0;
   for (vector<tcpnet::client::pointer>::iterator it = clients.begin(); it < clients.end(); it++, i++)
    if ((*it)->is_dead())
     clients.erase(it);
    else if ((temp = (*it)->receive()) != nullptr) {
     message::mes(MES_INFO,str::concatenate(4,"Client ", str::itoa(i), ": ", temp)); //temp=Mem leak?
     delete temp;
     (*it)->send(str::concatenate(3,"Received message from you client ",i ,"!"));
    }
   //mes(MES_INFO,"Loop");
   tcps.push_all(&clients;);
   //Sleep(50);
   //cout << clients.size() << endl;
  }
 }
 return;
}

 

Revise this Paste

Your Name: Code Language: