drivers/can_tcp_win32/can_tcp_win32_server.cpp
author Christian Taedcke <hacking@taedcke.com>
Thu, 22 Dec 2011 11:42:44 +0100
changeset 677 54e47953df3e
parent 633 2c43383a1d57
child 771 4c74be252095
permissions -rwxr-xr-x
FIXED: - the macro SUB_PROG_CFLAGS was overwritten for 64bit machines using gcc. Because of this the xenomai compilation failed.
407
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     1
/*
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     3
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     4
Copyright (C): Edouard TISSERANT
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     5
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     6
See COPYING file for copyrights details.
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     7
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     8
This library is free software; you can redistribute it and/or
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    10
License as published by the Free Software Foundation; either
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    12
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    13
This library is distributed in the hope that it will be useful,
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    16
Lesser General Public License for more details.
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    17
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    19
License along with this library; if not, write to the Free Software
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    21
*/
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    22
633
2c43383a1d57 Fix mingw32 cross build for can_peak_win32 and can_tcp_win32 server.
edouard
parents: 407
diff changeset
    23
#include <stdio.h>
407
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    24
#include "Socket.h"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    25
#include <iostream>
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    26
#include <process.h>
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    27
#include <string>
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    28
#include <list>
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    29
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    30
using namespace std;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    31
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    32
typedef std::list<Socket*> socket_list;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    33
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    34
socket_list g_connections;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    35
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    36
unsigned __stdcall Connection(void* a) {
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    37
  Socket* s = (Socket*) a;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    38
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    39
  g_connections.push_back(s);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    40
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    41
  //s->SendLine("Welcome to the Message Distributor");
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    42
  printf("Accepted new connection (0x%x).\n");
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    43
  while (1) {
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    44
    std::string r = s->ReceiveLine();
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    45
    if (r.empty()) break;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    46
    //cout << r  << endl;    
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    47
    for (socket_list::iterator os =g_connections.begin();
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    48
                               os!=g_connections.end(); 
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    49
                               os++) {
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    50
      if (*os != s) (*os)->SendLine(r);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    51
    }
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    52
  }
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    53
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    54
  g_connections.remove(s);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    55
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    56
  delete s;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    57
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    58
  printf("Connection closed (0x%x).\n",s);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    59
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    60
  return 0;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    61
}
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    62
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    63
int main() {
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    64
  printf("************************************************\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    65
		 "* TCP broadcasting chat server for CanFestival *\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    66
		 "*  for use with can_tcp_win32 CAN driver DLL   *\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    67
		 "************************************************\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    68
		 "\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    69
		 "Accepts connections on port 11898\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    70
		 "and repeat '\\n' terminated lines \n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    71
		 "to each connected client\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    72
		 "\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    73
		 "Use netcat to monitor trafic\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    74
		 " nc 127.0.0.1 11898\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    75
		 "\n");
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    76
  fflush(stdout);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    77
  SocketServer in(11898,5);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    78
  
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    79
  while (1) {
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    80
    Socket* s=in.Accept();
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    81
  
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    82
    unsigned ret;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    83
    _beginthreadex(0,0,Connection,(void*) s,0,&ret);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    84
  }
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    85
  
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    86
  return 0;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    87
}