src/network.c

changeset 34
c4d4b8a8f902
parent 22
41bbfd4d17a3
child 45
e14a1d9aa91d
equal deleted inserted replaced
33:866025982aa9 34:c4d4b8a8f902
27 * 27 *
28 */ 28 */
29 29
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <string.h> 31 #include <string.h>
32 #include <fcntl.h>
32 #include "network.h" 33 #include "network.h"
33 34
34 #define new_socket() socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 35 #define new_socket() socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
35 36
36 int net_create(Server *server, char* port) { 37 int net_create(Server *server, char* port) {
131 } 132 }
132 133
133 void net_recieve_data(int socket, void *data, size_t len) { 134 void net_recieve_data(int socket, void *data, size_t len) {
134 recv(socket, data, len, 0); 135 recv(socket, data, len, 0);
135 } 136 }
137
138 int net_setnonblocking(int socket, _Bool nonblocking) {
139 int opts = fcntl(socket, F_GETFL);
140 if (opts < 0) {
141 return 1;
142 }
143
144 if (nonblocking) {
145 opts |= O_NONBLOCK;
146 } else {
147 opts &= ~O_NONBLOCK;
148 }
149 if (fcntl(socket, F_SETFL, opts) < 0) {
150 return 1;
151 }
152
153 return 0;
154 }

mercurial