etisserant@407: /* etisserant@407: This file is part of CanFestival, a library implementing CanOpen Stack. etisserant@407: etisserant@407: Copyright (C): Edouard TISSERANT etisserant@407: etisserant@407: See COPYING file for copyrights details. etisserant@407: etisserant@407: This library is free software; you can redistribute it and/or etisserant@407: modify it under the terms of the GNU Lesser General Public etisserant@407: License as published by the Free Software Foundation; either etisserant@407: version 2.1 of the License, or (at your option) any later version. etisserant@407: etisserant@407: This library is distributed in the hope that it will be useful, etisserant@407: but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@407: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@407: Lesser General Public License for more details. etisserant@407: etisserant@407: You should have received a copy of the GNU Lesser General Public etisserant@407: License along with this library; if not, write to the Free Software etisserant@407: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@407: */ etisserant@407: etisserant@407: #include "Socket.h" etisserant@407: #include etisserant@407: #include etisserant@407: #include etisserant@407: #include etisserant@407: etisserant@407: using namespace std; etisserant@407: etisserant@407: typedef std::list socket_list; etisserant@407: etisserant@407: socket_list g_connections; etisserant@407: etisserant@407: unsigned __stdcall Connection(void* a) { etisserant@407: Socket* s = (Socket*) a; etisserant@407: etisserant@407: g_connections.push_back(s); etisserant@407: etisserant@407: //s->SendLine("Welcome to the Message Distributor"); etisserant@407: printf("Accepted new connection (0x%x).\n"); etisserant@407: while (1) { etisserant@407: std::string r = s->ReceiveLine(); etisserant@407: if (r.empty()) break; etisserant@407: //cout << r << endl; etisserant@407: for (socket_list::iterator os =g_connections.begin(); etisserant@407: os!=g_connections.end(); etisserant@407: os++) { etisserant@407: if (*os != s) (*os)->SendLine(r); etisserant@407: } etisserant@407: } etisserant@407: etisserant@407: g_connections.remove(s); etisserant@407: etisserant@407: delete s; etisserant@407: etisserant@407: printf("Connection closed (0x%x).\n",s); etisserant@407: etisserant@407: return 0; etisserant@407: } etisserant@407: etisserant@407: int main() { etisserant@407: printf("************************************************\n" etisserant@407: "* TCP broadcasting chat server for CanFestival *\n" etisserant@407: "* for use with can_tcp_win32 CAN driver DLL *\n" etisserant@407: "************************************************\n" etisserant@407: "\n" etisserant@407: "Accepts connections on port 11898\n" etisserant@407: "and repeat '\\n' terminated lines \n" etisserant@407: "to each connected client\n" etisserant@407: "\n" etisserant@407: "Use netcat to monitor trafic\n" etisserant@407: " nc 127.0.0.1 11898\n" etisserant@407: "\n"); etisserant@407: fflush(stdout); etisserant@407: SocketServer in(11898,5); etisserant@407: etisserant@407: while (1) { etisserant@407: Socket* s=in.Accept(); etisserant@407: etisserant@407: unsigned ret; etisserant@407: _beginthreadex(0,0,Connection,(void*) s,0,&ret); etisserant@407: } etisserant@407: etisserant@407: return 0; etisserant@407: }