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