Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ttcpip.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#include <winsock2.h></winsock2.h>
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#include <errno.h> /* obligatory includes */</errno.h>
Toshihiro Shimizu 890ddd
#include <signal.h></signal.h>
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include <unistd.h></unistd.h>
Toshihiro Shimizu 890ddd
#include <sys types.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys socket.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys wait.h=""></sys>
Toshihiro Shimizu 890ddd
#include <netinet in.h=""></netinet>
Toshihiro Shimizu 890ddd
#include <netdb.h></netdb.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifndef _WIN32
Toshihiro Shimizu 890ddd
#define SOCKET_ERROR -1
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TTcpIpClient::TTcpIpClient() {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
  WSADATA wsaData;
Shinya Kitaoka 120a6e
  WORD wVersionRequested = MAKEWORD(1, 1);
Shinya Kitaoka 120a6e
  int irc                = WSAStartup(wVersionRequested, &wsaData);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TTcpIpClient::~TTcpIpClient() {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
  WSACleanup();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int TTcpIpClient::connect(const QString &hostName, const QString &addrStr,
Shinya Kitaoka 120a6e
                          int port, int &sock) {
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
  if (!addrStr.empty())
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
    unsigned long ipAddr = inet_addr(addrStr.c_str());
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050500
Shinya Kitaoka 120a6e
  struct hostent *he = gethostbyname(hostName.toUtf8());
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  struct hostent *he = gethostbyname(hostName.toAscii());
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  if (!he) {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
    int err = WSAGetLastError();
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
    return HOST_UNKNOWN;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int socket_id = socket(AF_INET, SOCK_STREAM, 0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  struct sockaddr_in addr;
Shinya Kitaoka 120a6e
  memset((char *)&addr, 0, sizeof addr);
Shinya Kitaoka 120a6e
  addr.sin_family = he->h_addrtype;
Shinya Kitaoka 120a6e
  addr.sin_port   = htons(port);
Shinya Kitaoka 120a6e
  memcpy((char *)&(addr.sin_addr), he->h_addr, he->h_length);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int rcConnect = ::connect(socket_id, (struct sockaddr *)&(addr), sizeof addr);
Shinya Kitaoka 120a6e
  if (rcConnect == SOCKET_ERROR) {
Shinya Kitaoka 120a6e
    sock = -1;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
    int err = WSAGetLastError();
Shinya Kitaoka 120a6e
    switch (err) {
Shinya Kitaoka 120a6e
    case WSAECONNREFUSED:
Shinya Kitaoka 120a6e
      err = CONNECTION_REFUSED;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case WSAETIMEDOUT:
Shinya Kitaoka 120a6e
      err = CONNECTION_TIMEDOUT;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    default:
Shinya Kitaoka 120a6e
      err = CONNECTION_FAILED;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    closesocket(socket_id);
Shinya Kitaoka 120a6e
    return err;
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
    close(socket_id);
Shinya Kitaoka 120a6e
    return CONNECTION_FAILED;
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  sock = socket_id;
Shinya Kitaoka 120a6e
  return OK;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 120a6e
int TTcpIpClient::connect(const std::string &hostName, const std::string
Shinya Kitaoka 120a6e
&addrStr, int port, int &sock)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
  struct hostent *he = gethostbyname (hostName.c_str());
Toshihiro Shimizu 890ddd
  if (!he)
Toshihiro Shimizu 890ddd
  {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
    int err = WSAGetLastError();
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
    return HOST_UNKNOWN;
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  int socket_id = socket (AF_INET, SOCK_STREAM,0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  struct sockaddr_in addr;
Toshihiro Shimizu 890ddd
  memset((char*)&addr, 0, sizeof addr);
Toshihiro Shimizu 890ddd
  addr.sin_family = he->h_addrtype;
Toshihiro Shimizu 890ddd
  addr.sin_port = htons(port);
Toshihiro Shimizu 890ddd
  memcpy((char *)&(addr.sin_addr), he->h_addr, he->h_length);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int rcConnect = ::connect (socket_id, (struct sockaddr *)&(addr), sizeof
Shinya Kitaoka 120a6e
addr);
Toshihiro Shimizu 890ddd
  if (rcConnect == SOCKET_ERROR)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
    sock = -1;
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
    int err = WSAGetLastError();
Toshihiro Shimizu 890ddd
    switch (err)
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
     case WSAECONNREFUSED:
Toshihiro Shimizu 890ddd
        return CONNECTION_REFUSED;
Toshihiro Shimizu 890ddd
     case WSAETIMEDOUT:
Toshihiro Shimizu 890ddd
        return CONNECTION_TIMEDOUT;
Toshihiro Shimizu 890ddd
     default:
Toshihiro Shimizu 890ddd
        return CONNECTION_FAILED;
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
    return CONNECTION_FAILED;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  sock = socket_id;
Toshihiro Shimizu 890ddd
  return OK;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int TTcpIpClient::disconnect(int sock) {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
  closesocket(sock);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  close(sock);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return OK;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int TTcpIpClient::send(int sock, const QString &data) {
Shinya Kitaoka 120a6e
  std::string dataUtf8 = data.toStdString();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QString header("#$#THS01.00");
Shinya Kitaoka 120a6e
  header += QString::number((int)dataUtf8.size());
Shinya Kitaoka 120a6e
  header += "#$#THE";
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::string packet = header.toStdString() + dataUtf8;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //  string packet = data;;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int nLeft = packet.size();
Shinya Kitaoka 120a6e
  int idx   = 0;
Shinya Kitaoka 120a6e
  while (nLeft > 0) {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
    int ret = ::send(sock, packet.c_str() + idx, nLeft, 0);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
    int ret = write(sock, packet.c_str() + idx, nLeft);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    if (ret == SOCKET_ERROR) {
Toshihiro Shimizu 890ddd
// Error
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
      int err = WSAGetLastError();
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
      return SEND_FAILED;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    nLeft -= ret;
Shinya Kitaoka 120a6e
    idx += ret;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  shutdown(sock, 1);
Shinya Kitaoka 120a6e
  return OK;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int readData(int sock, QString &data) {
Shinya Kitaoka 120a6e
  int cnt = 0;
Shinya Kitaoka 120a6e
  char buff[1024];
Shinya Kitaoka 120a6e
  memset(buff, 0, sizeof(buff));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
  if ((cnt = recv(sock, buff, sizeof(buff), 0)) < 0) {
Shinya Kitaoka 120a6e
    int err = WSAGetLastError();
Shinya Kitaoka 120a6e
    // GESTIRE L'ERRORE SPECIFICO
Shinya Kitaoka 120a6e
    return -1;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  if ((cnt = read(sock, buff, sizeof(buff))) < 0) {
Shinya Kitaoka 120a6e
    printf("socket read failure %d\n", errno);
Shinya Kitaoka 120a6e
    perror("network server");
Shinya Kitaoka 120a6e
    close(sock);
Shinya Kitaoka 120a6e
    return -1;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (cnt == 0) return 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::string aa(buff);
Shinya Kitaoka 120a6e
  int x1 = aa.find("#$#THS01.00");
Shinya Kitaoka 120a6e
  x1 += sizeof("#$#THS01.00") - 1;
Shinya Kitaoka 120a6e
  int x2 = aa.find("#$#THE");
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::string ssize;
Shinya Kitaoka 120a6e
  for (int i = x1; i < x2; ++i) ssize.push_back(buff[i]);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int size = std::stoi(ssize);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  data = QString(buff + x2 + sizeof("#$#THE") - 1);
Shinya Kitaoka 120a6e
  size -= data.size();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  while (size > 0) {
Shinya Kitaoka 120a6e
    memset(buff, 0, sizeof(buff));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
    if ((cnt = recv(sock, buff, sizeof(buff), 0)) < 0) {
Shinya Kitaoka 120a6e
      int err = WSAGetLastError();
Shinya Kitaoka 120a6e
      // GESTIRE L'ERRORE SPECIFICO
Shinya Kitaoka 120a6e
      return -1;
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
    if ((cnt = read(sock, buff, sizeof(buff))) < 0) {
Shinya Kitaoka 120a6e
      printf("socket read failure %d\n", errno);
Shinya Kitaoka 120a6e
      perror("network server");
Shinya Kitaoka 120a6e
      close(sock);
Shinya Kitaoka 120a6e
      return -1;
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
    else if (cnt == 0) {
Shinya Kitaoka 120a6e
      break;  // break out of loop
Shinya Kitaoka 120a6e
    } else if (cnt < (int)sizeof(buff)) {
Shinya Kitaoka 120a6e
      data += QString(buff);
Shinya Kitaoka 120a6e
      // break;  // break out of loop
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      data += QString(buff);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    size -= cnt;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (data.size() < size) return -1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if 0
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int readData(int sock, string &data)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
  int cnt = 0;
Toshihiro Shimizu 890ddd
  char buff[1024];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  do
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
    memset (buff,0,sizeof(buff));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
    if (( cnt = recv(sock, buff, sizeof(buff), 0)) < 0 )
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
      int err = WSAGetLastError();
Toshihiro Shimizu 890ddd
      // GESTIRE L'ERRORE SPECIFICO
Toshihiro Shimizu 890ddd
      return -1;
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
    if (( cnt = read (sock, buff, sizeof(buff))) < 0 )
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
      printf("socket read failure %d\n", errno);
Toshihiro Shimizu 890ddd
      perror("network server");
Toshihiro Shimizu 890ddd
      close(sock);
Toshihiro Shimizu 890ddd
      return -1;
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
    else
Toshihiro Shimizu 890ddd
    if (cnt == 0)
Toshihiro Shimizu 890ddd
      break;  // break out of loop
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    data += string(buff);
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
  while (cnt != 0);  // do loop condition
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#define PRIMA
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef PRIMA
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int readData(int sock, string &data) {
Shinya Kitaoka 120a6e
  int cnt = 0;
Shinya Kitaoka 120a6e
  char buff[1024];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  do {
Shinya Kitaoka 120a6e
    memset(buff, 0, sizeof(buff));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
    if ((cnt = recv(sock, buff, sizeof(buff), 0)) < 0) {
Shinya Kitaoka 120a6e
      int err = WSAGetLastError();
Shinya Kitaoka 120a6e
      // GESTIRE L'ERRORE SPECIFICO
Shinya Kitaoka 120a6e
      return -1;
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
    if ((cnt = read(sock, buff, sizeof(buff))) < 0) {
Shinya Kitaoka 120a6e
      printf("socket read failure %d\n", errno);
Shinya Kitaoka 120a6e
      perror("network server");
Shinya Kitaoka 120a6e
      close(sock);
Shinya Kitaoka 120a6e
      return -1;
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
    else if (cnt == 0) {
Shinya Kitaoka 120a6e
      break;  // break out of loop
Shinya Kitaoka 120a6e
    } else if (cnt < sizeof(buff)) {
Shinya Kitaoka 120a6e
      data += string(buff);
Shinya Kitaoka 120a6e
      // break;  // break out of loop
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      data += string(buff);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } while (cnt != 0);  // do loop condition
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int TTcpIpClient::send(int sock, const QString &data, QString &reply) {
Shinya Kitaoka 120a6e
  if (data.size() > 0) {
Shinya Kitaoka 120a6e
    int ret           = send(sock, data);
Shinya Kitaoka 120a6e
    if (ret == 0) ret = readData(sock, reply);
Shinya Kitaoka 120a6e
    return ret;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return OK;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 3bfa54
int TTcpIpClient::send(int sock, const std::string &data, string &reply)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
  int ret = send(sock, data);
Toshihiro Shimizu 890ddd
  if (ret == 0)
Toshihiro Shimizu 890ddd
    ret = readData(sock, reply);
Toshihiro Shimizu 890ddd
  return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/