home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 September / PCO_0998.ISO / filesbbs / frei / palmsrc.arj / PALMSRC.ZIP / sockets.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-31  |  4.6 KB  |  193 lines

  1. /*
  2.  *  Copyright (C) 1997, 1998 Olivetti & Oracle Research Laboratory
  3.  *
  4.  *  Copyright (C) 1998 International Computer Science Institute (ICSI)
  5.  *
  6.  *  This is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This software is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this software; if not, write to the Free Software
  18.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
  19.  *  USA.
  20.  */
  21.  
  22. /*
  23.  * sockets.c - functions to deal with sockets.
  24.  */
  25.  
  26. #include "palmVNC.h"
  27. #include <sys_socket.h>
  28.  
  29. static unsigned long recvBytes = 0;
  30. static unsigned long sentBytes = 0;
  31. static unsigned long start;
  32. static unsigned long sentInterval, recvInterval;
  33.  
  34. Bool errorMessageFromReadExact = True;
  35.  
  36.  
  37. /******************************************************************
  38.  * PrintSentRecvStat
  39.  *
  40.  * prints information about send/receive spped on the console
  41.  *****************************************************************/
  42. void PrintSentRecvStat ()
  43. {
  44.   if (sentBytes || recvBytes) {
  45.     printf ("Network performance statistic:\n");
  46.     printf ("\tsent: %ld bytes\n\trecv: %ld Kbytes\n", sentBytes, recvBytes/1000);
  47.   }
  48.   else
  49.     return;
  50.  
  51.   if (sentInterval != 0)
  52.     printf ("\tsend speed %ld bytes/sec\n",
  53.     sentBytes * sysTicksPerSecond / sentInterval);
  54.  
  55.   if (recvInterval != 0)
  56.     printf ("\treceive speed %ld bytes/sec\n",
  57.     recvBytes * sysTicksPerSecond / recvInterval);
  58.  
  59.   recvBytes = sentBytes = 0;
  60.   start = sentInterval = recvInterval = 0;
  61. }
  62.  
  63.  
  64. /******************************************************************
  65.  * ReadExact
  66.  * 
  67.  * Read an exact number of bytes, and don't return until you've got them.
  68.  *****************************************************************/
  69.  
  70. Bool ReadExact (int sock, unsigned char *buf, int n)
  71. {
  72.   int i = 0;
  73.   int j;
  74.  
  75.   start = TimGetTicks ();
  76.  
  77.   while (i < n) {
  78.     j = read (sock, buf + i, (n - i));
  79.     if (j <= 0) {
  80.       if (j < 0) {
  81.     printf ("read failed\n");
  82.       } else {
  83.     if (errorMessageFromReadExact) {
  84.       printf ("read nothing\n");
  85.     }
  86.       }
  87.       return False;
  88.     }
  89.     i += j;
  90.   }
  91.  
  92.   recvInterval += TimGetTicks () - start;
  93.   recvBytes += n;
  94.  
  95.   return True;
  96. }
  97.  
  98.  
  99. /******************************************************************
  100.  * WriteExact
  101.  * 
  102.  * Write an exact number of bytes, and don't return until you've sent them.
  103.  *****************************************************************/
  104.  
  105. Bool WriteExact (int sock, unsigned char *buf, int n)
  106. {
  107.   int i = 0;
  108.   int j;
  109.  
  110.   start = TimGetTicks ();
  111.  
  112.   while (i < n) {
  113.     j = write(sock, buf + i, (n - i));
  114.     if (j <= 0) {
  115.       if (j < 0) {
  116.     printf ("write failed\n");
  117.       } else {
  118.         printf ("write nothing\n");
  119.       }
  120.       return False;
  121.     }
  122.     i += j;
  123.   }
  124.  
  125.   sentInterval += TimGetTicks () - start;
  126.   sentBytes += n;
  127.  
  128.   return True;
  129. }
  130.  
  131.  
  132. /******************************************************************
  133.  * ConnectToTcpAddr
  134.  * 
  135.  * Connects to the given TCP port.
  136.  *****************************************************************/
  137.  
  138. int ConnectToTcpAddr (unsigned long host, int port)
  139. {
  140.     int sock;
  141.     struct sockaddr_in addr;
  142.     int one = 1;
  143.  
  144.     addr.sin_family = AF_INET;
  145.     addr.sin_port = htons(port);
  146.     addr.sin_addr.s_addr = host;
  147.  
  148.     sock = socket(AF_INET, SOCK_STREAM, 0);
  149.     if (sock < 0) {
  150.     printf ("ConnectToTcpAddr: cannot open socket\n");
  151.     return -1;
  152.     }
  153.  
  154.     if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  155.     printf ("ConnectToTcpAddr: cannot connect\n");
  156.     close(sock);
  157.     return -1;
  158.     }
  159.  
  160.     if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
  161.            (char *)&one, sizeof(one)) < 0) {
  162.     printf (": ConnectToTcpAddr: cannot set socket options\n");
  163.     close(sock);
  164.     return -1;
  165.     }
  166.  
  167.     return sock;
  168. }
  169.  
  170.  
  171.  
  172. /******************************************************************
  173.  * StringToIPAddr
  174.  * 
  175.  * Converts a host string to an IP address.
  176. *****************************************************************/
  177. int StringToIPAddr (char *str, unsigned long *addr)
  178. {
  179.     struct hostent *hp = NULL;
  180.     // without this def. gethostbyname() corrupts memory!!!!!!!
  181.     NetHostInfoBufType AppHostInfo;
  182.  
  183.     if ((hp = gethostbyname (str)) == NULL) {
  184.       if ((long)(*addr = inet_addr (str)) == -1)
  185.         return 0;
  186.     }
  187.     else
  188.       bcopy ((char *)(hp->h_addr), (char *)addr, hp->h_length);
  189.  
  190.     return 1;
  191. }
  192.  
  193.