diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Bachelor/Verteilte Systeme/Praktikum1/util.cpp | |
| download | Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2 | |
Diffstat (limited to 'Bachelor/Verteilte Systeme/Praktikum1/util.cpp')
| -rw-r--r-- | Bachelor/Verteilte Systeme/Praktikum1/util.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/Bachelor/Verteilte Systeme/Praktikum1/util.cpp b/Bachelor/Verteilte Systeme/Praktikum1/util.cpp new file mode 100644 index 0000000..88c8f66 --- /dev/null +++ b/Bachelor/Verteilte Systeme/Praktikum1/util.cpp @@ -0,0 +1,86 @@ +/************************************************************************
+ *
+ * Utility functions to use with sockets
+ *
+ ************************************************************************
+
+/************************************************************************
+ *
+ * Read "n" bytes from a descriptor.
+ * Use in place of read() when fd is a stream socket.
+ */
+#include <iostream>
+using namespace std;
+
+int readn(int fd, char* ptr, int nbytes)
+{
+ int nleft, nread;
+
+ nleft = nbytes;
+ while (nleft > 0) {
+ nread = read(fd, ptr, nleft);
+ if (nread < 0)
+ return(nread); /* error, return < 0 */
+ else if (nread == 0)
+ break; /* EOF */
+
+ nleft -= nread;
+ ptr += nread;
+ }
+ return(nbytes - nleft); /* return >= 0 */
+}
+/************************************************************************
+
+/************************************************************************
+ *
+ * Read a line from a descriptor. Read the line one byte at a time,
+ * looking for the newline. We store the newline in the buffer,
+ * then follow it with a null (the same as fgets(3)).
+ * We return the number of characters up to, but not including,
+ * the null (the same as strlen(3)).
+ */
+int readline(int fd,char* ptr, int maxlen)
+{
+ int n, rc;
+ char c;
+
+ for (n = 1; n < maxlen; n++) {
+ if ( (rc = read(fd, &c, 1)) == 1) {
+ *ptr++ = c;
+ if (c == '\n')
+ break;
+ } else if (rc == 0) {
+ if (n == 1)
+ return(0); /* EOF, no data read */
+ else
+ break; /* EOF, some data was read */
+ } else
+ return(-1); /* error */
+ }
+
+ *ptr = 0;
+ return(n);
+}
+/************************************************************************
+
+/************************************************************************
+ *
+ * Write "n" bytes to a descriptor.
+ * Use in place of write() when fd is a stream socket.
+ */
+int writen(int fd, char* ptr, int nbytes)
+{
+ int nleft, nwritten;
+
+ nleft = nbytes;
+ while (nleft > 0) {
+ nwritten = write(fd, ptr, nleft);
+ if (nwritten <= 0)
+ return(nwritten); /* error */
+
+ nleft -= nwritten;
+ ptr += nwritten;
+ }
+ return(nbytes - nleft);
+}
+/************************************************************************/
|
