static int udpserver_create(const char* ip, int port) { int ret; socket_t socket; struct sockaddr_in addr; // new a UDP socket socket = socket_udp(); if(socket_error == socket) return 0; // reuse addr socket_setreuseaddr(socket, 1); // bind if(ip && ip[0]) { ret = socket_addr_ipv4(&addr, ip, (unsigned short)port); if(0 == ret) ret = socket_bind(socket, (struct sockaddr*)&addr, sizeof(addr)); } else { ret = socket_bind_any(socket, (unsigned short)port); } if(0 != ret) { socket_close(socket); return 0; } return socket; }
static socket_t Listen(int port) { int r; socket_t server; server = socket_tcp(); if(socket_error == server) return socket_invalid; // reuse addr r = socket_setreuseaddr(server, 1); // bind r = socket_bind_any(server, (unsigned short)port); // listen r = socket_listen(server, 64); return server; }