Example #1
0
/*
 * Open a TCP socket and connect to ip:port.
 */
int tcp_connect(unsigned int ip, unsigned short port)
{
  int fd;
  int error = 0;

#ifdef WIN32
  if (wsa_init() < 0)
  {
    return -1;
  }
#endif

  if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1)
  {
    struct sockaddr_in sa =
    { .sin_family = AF_INET, .sin_port = htons(port), .sin_addr.s_addr = ip };

    if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1)
    {
      gprintf(_("connected to %s:%d\n"), inet_ntoa(sa.sin_addr), port);

#ifdef WIN32
      // Set the socket I/O mode; iMode = 0 for blocking; iMode != 0 for non-blocking
      int iMode = 1;
      if(ioctlsocket(fd, FIONBIO, (u_long FAR*) &iMode) == SOCKET_ERROR)
      {
        psockerror("ioctlsocket");
        error = 1;
      }
#endif
    }
    else
    {
      psockerror("connect");
      error = 1;
    }
  }
  else
  {
    psockerror("socket");
    error = 1;
  }

  if(error && fd >= 0)
  {
    close(fd);
    fd = -1;
  }

#ifdef WIN32
  wsa_count(error);
#endif

  return fd;
}
Example #2
0
/*
 * Send a packet.
 */
int tcp_send(int fd, const unsigned char* buf, int length)
{
  int ret = send(fd, (const void*)buf, length, MSG_DONTWAIT | MSG_NOSIGNAL);
  if(ret < 0)
  {
    psockerror("send");
  }
  return ret;
}
Example #3
0
/*
 * Receive data from a tcp socket.
 */
int tcp_recv(int fd, unsigned char* buf, int len)
{
  int ret = recv(fd, (void*)buf, len, 0);
  if(ret < 0)
  {
    psockerror("recv");
  }
  return ret;
}
Example #4
0
void sockfail(char *msg)
{
	fflush(stdout);
	psockerror(msg);
	exit(2);
}