|
3 | 3 | #include <stdlib.h> |
4 | 4 | #include <string.h> |
5 | 5 | #include <netdb.h> |
6 | | - #include <fcntl.h> |
7 | 6 | #include <unistd.h> |
8 | 7 | #include <sys/time.h> |
9 | 8 | #include <sys/socket.h> |
|
18 | 17 | // NETWORK FUNCTIONS // |
19 | 18 | /////////////////////// |
20 | 19 |
|
21 | | - int network_connect(char *hostname, int port, int type) { int loop0, nd = -1; |
22 | | - struct sockaddr_in sockaddr; struct hostent *hostent; |
| 20 | + int network_connect(char *hostname, int port, int type) { int result = 0; |
| 21 | + struct sockaddr_in sockaddr; int loop0, nd = -1; |
23 | 22 |
|
24 | | - // Find an available network descriptor, aborting it none is found. |
| 23 | + // Allocate the network descriptor. |
25 | 24 | for(loop0=0;loop0<10;loop0++) { if (sock[loop0] <= 0) { nd = loop0; } } |
26 | | - if (nd == -1) { return -1; } |
| 25 | + if (nd < 0) { printf("[ERROR] Unable to allocate network descriptor. (%d)\n", nd); return -1; } |
27 | 26 |
|
28 | | - // Set the host information. |
| 27 | + // Set the sockaddr information. |
29 | 28 | sockaddr.sin_family = AF_INET; |
30 | | - sockaddr.sin_port = htons(port); |
31 | | - hostent = gethostbyname(hostname); |
32 | | - sockaddr.sin_addr = *(struct in_addr *)hostent->h_addr; |
| 29 | + sockaddr.sin_port = htons(port); |
| 30 | + sockaddr.sin_addr = *(struct in_addr *)gethostbyname(hostname)->h_addr; |
33 | 31 |
|
34 | | - // Create and connect the socket. |
| 32 | + // Create the socket. |
35 | 33 | if (type == SOCKET_TCP) { sock[nd] = socket(AF_INET, SOCK_STREAM, 0); } |
36 | 34 | if (type == SOCKET_UDP) { sock[nd] = socket(AF_INET, SOCK_DGRAM, 0); } |
| 35 | + if (sock[nd] < 0) { printf("[ERROR] Create socket failed. (%d)\n", result); return -2; } |
37 | 36 |
|
38 | | - // Connect the socket to the remote host. |
39 | | - connect(sock[nd], (struct sockaddr *)&sockaddr, sizeof(struct sockaddr)); |
| 37 | + // Connect the socket. |
| 38 | + result = connect(sock[nd], (struct sockaddr *)&sockaddr, sizeof(struct sockaddr)); |
| 39 | + if (result < 0) { printf("[ERROR] Connect socket failed. (%d)\n", result); return -3; } |
40 | 40 |
|
41 | 41 | // End function. |
42 | 42 | return nd; |
43 | 43 |
|
44 | 44 | } |
45 | 45 |
|
46 | | - int network_listen(int port, int type) { int loop0, nd = -1; |
47 | | - struct sockaddr_in sockaddr; |
| 46 | + int network_listen(int port, int type) { int result = 0; |
| 47 | + struct sockaddr_in sockaddr; int loop0, nd = -1; |
48 | 48 |
|
49 | | - // Find an available network descriptor, aborting if none is found. |
| 49 | + // Find an available network descriptor. |
50 | 50 | for(loop0=0;loop0<10;loop0++) { if (sock[loop0] <= 0) { nd = loop0; } } |
51 | | - if (nd == -1) { return -1; } |
| 51 | + if (nd < 0) { printf("[ERROR] Unable to allocate network descriptor. (%d)\n", nd); return -1; } |
52 | 52 |
|
53 | 53 | // Set the sockaddr information. |
54 | 54 | sockaddr.sin_family = AF_INET; |
55 | | - sockaddr.sin_port = htons(port); |
| 55 | + sockaddr.sin_port = htons(port); |
56 | 56 | sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); |
57 | 57 |
|
58 | 58 | // Create the socket. |
59 | 59 | if (type == SOCKET_TCP) { sock[nd] = socket(AF_INET, SOCK_STREAM, 0); } |
60 | 60 | if (type == SOCKET_UDP) { sock[nd] = socket(AF_INET, SOCK_DGRAM, 0); } |
| 61 | + if (sock[nd] < 0) { printf("[ERROR] Create socket failed. (%d)\n", result); return -2; } |
61 | 62 |
|
62 | | - // Bind to the socket. |
63 | | - bind(sock[nd], (struct sockaddr *)&sockaddr, sizeof(struct sockaddr)); |
64 | | - |
65 | | - // Listen for data on the socket. |
66 | | - listen(sock[nd], 10); |
| 63 | + // Bind the socket. |
| 64 | + result = bind(sock[nd], (struct sockaddr *)&sockaddr, sizeof(struct sockaddr)); |
| 65 | + if (result < 0) { printf("[ERROR] Bind socket failed. (%d)\n", result); return -3; } |
67 | 66 |
|
68 | 67 | // End function. |
69 | 68 | return nd; |
70 | 69 |
|
71 | 70 | } |
72 | 71 |
|
73 | | - int network_send(int nd, void *buffer, int size) { |
| 72 | + int network_send(int nd, void *buffer, int size) { int result = 0; |
| 73 | + |
| 74 | + // Send the data. |
| 75 | + result = send(sock[nd], buffer, size, 0); |
| 76 | + if (result < 0) { printf("[ERROR] Send data failed. (%d)\n", result); return -1; } |
| 77 | + |
| 78 | + // End function. |
| 79 | + return result; |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + int network_sendall(int nd, void *buffer, int size) { int result = 0; |
74 | 84 |
|
75 | | - // Send data to the network. |
76 | | - return send(sock[nd], buffer, size, 0); |
| 85 | + // Send all the data. |
| 86 | + result = send(sock[nd], buffer, size, MSG_WAITALL); |
| 87 | + if (result < 0) { printf("[ERROR] Send all data failed. (%d)\n", result); return -1; } |
| 88 | + |
| 89 | + // End function. |
| 90 | + return result; |
77 | 91 |
|
78 | 92 | } |
79 | 93 |
|
|
100 | 114 |
|
101 | 115 | } |
102 | 116 |
|
103 | | - int network_recv(int nd, void *buffer, int size) { |
| 117 | + int network_recv(int nd, void *buffer, int size) { int result = 0; |
104 | 118 |
|
105 | 119 | // Clear the buffer. |
106 | 120 | memset(buffer, 0, size); |
107 | 121 |
|
108 | | - // Receive some data from the network. |
109 | | - if (FD_ISSET(sock[nd], &rfds)) { return recv(sock[nd], buffer, size, 0); } |
| 122 | + // Check to see if data is available. |
| 123 | + if (!FD_ISSET(sock[nd], &rfds)) { return 0; } |
| 124 | + |
| 125 | + // Read the data. |
| 126 | + result = recv(sock[nd], buffer, size, 0); |
| 127 | + if (result < 0) { printf("[ERROR] Read data failure. (%d)\n", result); return -1; } |
110 | 128 |
|
111 | 129 | // End function. |
112 | | - return 0; |
| 130 | + return result; |
113 | 131 |
|
114 | 132 | } |
115 | 133 |
|
116 | | - int network_recvfrom(int nd, void *buffer, int size) { |
| 134 | + int network_recvall(int nd, void *buffer, int size) { int result = 0; |
117 | 135 |
|
118 | 136 | // Clear the buffer. |
119 | 137 | memset(buffer, 0, size); |
120 | 138 |
|
121 | | - // Receive data from the network. |
122 | | - if (FD_ISSET(sock[nd], &rfds)) { return recvfrom(sock[nd], buffer, size, 0, NULL, NULL); } |
| 139 | + // Check to see if data is available. |
| 140 | + if (!FD_ISSET(sock[nd], &rfds)) { return 0; } |
| 141 | + |
| 142 | + // Read all the data. |
| 143 | + result = recv(sock[nd], buffer, size, MSG_WAITALL); |
| 144 | + if (result < 0) { printf("[ERROR] Read all data failure. (%d)\n", result); return -1; } |
123 | 145 |
|
124 | 146 | // End function. |
125 | | - return 0; |
| 147 | + return result; |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + int network_recvfrom(int nd, void *buffer, int size) { int result = 0; |
| 152 | + |
| 153 | + // Clear the buffer. |
| 154 | + memset(buffer, 0, size); |
| 155 | + |
| 156 | + // Check to see if data is available. |
| 157 | + if (!FD_ISSET(sock[nd], &rfds)) { return 0; } |
| 158 | + |
| 159 | + // Read the data. |
| 160 | + result = recvfrom(sock[nd], buffer, size, 0, NULL, NULL); |
| 161 | + if (result < 0) { printf("[ERROR] Read data failure. (%d)\n", result); return -1; } |
| 162 | + |
| 163 | + // End function. |
| 164 | + return result; |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | + int network_recvallfrom(int nd, void *buffer, int size) { int result = 0; |
| 169 | + |
| 170 | + // Clear the buffer. |
| 171 | + memset(buffer, 0, size); |
| 172 | + |
| 173 | + // Check to see if data is available. |
| 174 | + if (!FD_ISSET(sock[nd], &rfds)) { return 0; } |
| 175 | + |
| 176 | + // Read all the data. |
| 177 | + result = recvfrom(sock[nd], buffer, size, MSG_WAITALL, NULL, NULL); |
| 178 | + if (result < 0) { printf("[ERROR] Read all data failure. (%d)\n", result); return -1; } |
| 179 | + |
| 180 | + // End function. |
| 181 | + return result; |
126 | 182 |
|
127 | 183 | } |
128 | 184 |
|
129 | 185 | int network_disconnect(int nd) { int result = 0; |
130 | 186 |
|
131 | 187 | // Close the socket. |
132 | 188 | result = close(sock[nd]); |
| 189 | + if (result < 0) { printf("[ERROR] Close socket failed. (%d)\n", result); return -1; } |
133 | 190 |
|
134 | 191 | // Clear the descriptor. |
135 | 192 | sock[nd] = -1; |
|
0 commit comments