ACT-CV - Machine Vision for Cognitive Modeling
TcpClient.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 
22 
27 #include <iostream>
28 #include "TcpClient.h"
29 
30 using namespace std;
31 
32 void ActCvClient::ConnectTo(const char* name, int port) {
33  struct hostent *hp; /* ptr to host info for remote */
34  struct sockaddr_in srv; /* used by connect() */
35  srv.sin_family = AF_INET;
36 
37  hp = gethostbyname(name);
38  srv.sin_addr.s_addr = ((struct in_addr*)(hp->h_addr))->s_addr;
39 
40  /* connect: use the Internet address family */
41  srv.sin_family = AF_INET;
42  /* connect: socket fd to user specified port */
43  srv.sin_port = htons(port);
44  if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
45  cerr << "socket";
46  throw ClientError("socket");
47  }
48 #if 1
49  int flag = 1;
50  int result = setsockopt(sock,
51  IPPROTO_TCP,
52  TCP_NODELAY,
53  (char *) &flag,
54  sizeof(int));
55  if (result < 0) {
56  cerr << "setsockopt";
57  throw ClientError("setsockopt");
58  }
59 #endif
60 
61  if(connect(sock, (struct sockaddr*) &srv, sizeof(srv)) < 0) {
62  cerr << "connect";
63  throw ClientError("connect");
64  }
65 
66  isConnected=true;
67 }
68 
69 void ActCvClient::SendKey(int keyCode) {
70  if (!isConnected) {
71  throw ClientError("not connected");
72  }
73 
74  if (send(sock, "K", 1, 0) < 0) {
75  cerr << "write";
76  throw ClientError("write");
77  }
78  // B -> 0x42
79  // M -> 0x4d
80  // N -> 0x4e
81  // left arrow -> 37
82  // right arrow -> 39
83  if (send(sock, (char*)&keyCode, sizeof(int), 0) < 0) {
84  cerr << "write";
85  throw ClientError("write");
86  }
87 }
88 
90  if (!isConnected) {
91  throw ClientError("not connected");
92  }
93 
94  if (send(sock, "X", 1, 0) < 0) {
95  cerr << "write";
96  throw ClientError("write");
97  }
98  int numBytes;
99  if(recv(sock, (char*)&numBytes, sizeof(numBytes), 0) < 0) {
100  cerr << "read";
101  throw ClientError("read");
102  }
103  if (numBytes>0) {
104  char *data= new char[numBytes+1];
105  if(recv(sock, data, numBytes, 0) < 0) {
106  cerr << "read";
107  throw ClientError("write");
108  }
109  data[numBytes]=0;
110 
111  cout << data << endl;
112  delete[] data;
113  }
114 }


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