ACT-CV - Machine Vision for Cognitive Modeling
client.cpp
Go to the documentation of this file.
1 // -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4; coding: iso-8859-1; -*-
2 
3 /*
4 ACT-CV - Machine Vision for Cognitive Modeling
5 Copyright (c) 2008 Marc Halbruegge
6 
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
26 #include <iostream>
27 #include <vector>
28 #include <algorithm>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 
34 
35 
36 #include "TcpClient.h"
37 #include <config.h>
38 
39 using namespace std;
40 
41 
43 struct CharAtPos {
44  char c;
45  int x,y;
46 
47  bool operator < (const CharAtPos &c) const {
48  if (y==c.y) {
49  return x<c.x;
50  } else {
51  return y<c.y;
52  }
53  }
54 };
55 
56 
58 void ReadData(SOCKET handle) {
59  if (send(handle, "X", 1, 0) < 0) {
60  perror("write");
61  exit(1);
62  }
63  int numBytes;
64  if(recv(handle, (char*)&numBytes, sizeof(numBytes), 0) < 0) {
65  perror("read");
66  exit(1);
67  }
68  if (numBytes==0) return;
69 
70  char *data= new char[numBytes+1];
71  if(recv(handle, data, numBytes, 0) < 0) {
72  perror("read");
73  exit(1);
74  }
75  data[numBytes]=0;
76 
77 #if 0
78  vector<CharAtPos> chars;
79  char *p;
80  p = strchr(data+1, '(');
81  while (p) {
82  CharAtPos cap;
83  cap.c = *(p+1);
84  cap.x = atoi(p+3);
85  char *p2 = strchr(p+4,' ');
86  if (p2) {
87  cap.y = atoi(p2+1);
88  chars.push_back(cap);
89  }
90 
91  p = strchr(p+1, '(');
92  }
93 
94  sort(chars.begin(),chars.end());
95 
96  cout << endl;
97  for (unsigned int i=0;i<chars.size();i++) {
98  if (i>0 && chars[i-1].y != chars[i].y) cout << endl;
99  cout << chars[i].c;
100  }
101  cout << endl;
102 #endif
103 
104  cout << data << endl;
105  delete[] data;
106 
107 #if 0
108  int keys[3] = {0x42, 0x4d, 0x4e};
109  if (chars.size()>0) {
110  SendKey(handle, keys[rand()%3]);
111  }
112 #endif
113 }
114 
115 
116 
117 int main(int argc, const char **argv) {
118  if (argc < 2){
119  printf ("Bitte Zielhost angeben\n");
120  exit(1);
121  }
122 
123  ActCvClient client;
124  try {
125  client.ConnectTo(argv[1], ACTCVSERVER_PORTNUM);
126  cout << "connected to " << argv[1] << endl;
127  for (;;) {
128  client.ReadData();
129 #ifdef _WINDOWS
130  Sleep(500);
131 #else
132  usleep(500000);
133 #endif
134  client.SendKey(37);
135  }
136  } catch (ClientError &ce) {
137  cerr << "Error: " << ce.what() << endl;
138  }
139 
140  return 0;
141 }
142 
143 #ifdef _WINDOWS
144 struct WSAAutoStart {
145  WORD wVersionRequested;
146  WSADATA wsaData;
147  int err;
148 
149  WSAAutoStart() {
150  wVersionRequested = MAKEWORD( 2, 2 );
151 
152  WSAStartup(wVersionRequested, &wsaData);
153  }
154  ~WSAAutoStart() {
155  WSACleanup();
156  }
157 } wsaAutoStart;
158 #endif


ACT-CV - Machine Vision for Cognitive Modeling
© 2015 Marc Halbruegge (actcvlibrary@googlemail.com)