1 /* HSOCKET.C (c) Copyright Robert Styma, 2006 */
2 /* Socket read/write routines */
3
4 #include "hstdinc.h"
5
6 #define _HSOCKET_C_
7 #define _HUTIL_DLL_
8
9 #include "hercules.h"
10
11 /************************************************************************
12
13 NAME: read_socket - read a passed number of bytes from a socket.
14
15 PURPOSE: This routine is used in place of read to read a passed number
16 of bytes from a socket. A read on a socket will return less
17 than the number of bytes requested if there are less currenly
18 available. Thus we read in a loop till we get all we want.
19
20 PARAMETERS:
21 1. fd - int (INPUT)
22 This is the file descriptor for the socket to be read.
23
24 2. ptr - pointer to void (OUTPUT)
25 This is a pointer to where the data is to be put.
26
27 3. nbytes - int (INPUT)
28 This is the number of bytes to read.
29
30 FUNCTIONS :
31
32 1. Read in a loop till an error occurs or the data is read.
33
34 OUTPUTS:
35 data into the buffer
36
37 *************************************************************************/
38
read_socket(int fd,void * _ptr,int nbytes)39 DLL_EXPORT int read_socket(int fd, void *_ptr, int nbytes)
40 {
41 int nleft, nread;
42 char *ptr;
43
44 nleft = nbytes;
45 ptr=_ptr;
46 while (nleft > 0)
47 {
48
49 #ifdef _MSVC_
50 nread = recv(fd, ptr, nleft, 0);
51 if ((nread == SOCKET_ERROR) || (nread < 0))
52 {
53 nread = -1;
54 break; /* error, return < 0 */
55 }
56 if (nread == 0)
57 break;
58 #else
59 nread = read(fd, ptr, nleft);
60 if (nread < 0)
61 return(nread); /* error, return < 0 */
62 else
63 if (nread == 0) /* eof */
64 break;
65 #endif
66
67 nleft -= nread;
68 ptr += nread;
69
70 } /* end of do while */
71
72 /* if (nleft != 0)
73 logmsg (_("BOB123 read_socket: Read of %d bytes requested, %d bytes actually read\n"),
74 nbytes, nbytes - nleft);*/
75
76 return (nbytes - nleft);
77
78 } /* end of read_socket */
79
80
81 /************************************************************************
82
83 NAME: write_socket - write a passed number of bytes into a socket.
84
85 PURPOSE: This routine is used in place of write to write a passed number
86 of bytes into a socket. A write on a socket may take less
87 than the number of bytes requested if there is currently insufficient
88 buffer space available. Thus we write in a loop till we get all we want.
89
90 PARAMETERS:
91 1. fd - int (OUTPUT)
92 This is the file descriptor for the socket to be written.
93
94 2. ptr - pointer to void (INPUT)
95 This is a pointer to where the data is to be gotten from.
96
97 3. nbytes - int (INPUT)
98 This is the number of bytes to write.
99
100 FUNCTIONS :
101 1. Write in a loop till an error occurs or the data is written.
102
103 OUTPUTS:
104 Data to the socket.
105
106 *************************************************************************/
107
108 /*
109 * Write "n" bytes to a descriptor.
110 * Use in place of write() when fd is a stream socket.
111 */
112
write_socket(int fd,const void * _ptr,int nbytes)113 DLL_EXPORT int write_socket(int fd, const void *_ptr, int nbytes)
114 {
115 int nleft, nwritten;
116 const char *ptr;
117
118 nleft = nbytes;
119 ptr=_ptr;
120 while (nleft > 0)
121 {
122
123 #ifdef _MSVC_
124 nwritten = send(fd, ptr, nleft, 0);
125 if (nwritten <= 0)
126 {
127 return(nwritten); /* error */
128 }
129 #else
130 nwritten = write(fd, ptr, nleft);
131 if (nwritten <= 0)
132 return(nwritten); /* error */
133 #endif
134
135
136 nleft -= nwritten;
137 ptr += nwritten;
138 } /* end of do while */
139
140 return(nbytes - nleft);
141
142 } /* end of write_socket */
143