* #include <sys/types.h>
* #include <sys/socket.h>
* #include <ccan/oserver/oserver.h>
+ * #include <ccan/opt/opt.h>
* #include <err.h>
* #include <stdlib.h>
*
- * int main(void)
+ * int main(int argc, char *argv[])
* {
* int fd, sockfd;
+ * unsigned int port = OSERVER_PORT;
*
- * sockfd = oserver_setup();
+ * opt_register_noarg("--help|--usage|-h", opt_usage_and_exit,
+ * "\nA oserver program", "Usage information");
+ * opt_register_arg("--port", opt_set_uintval, opt_show_uintval,
+ * &port, "Server port number");
+ *
+ * opt_parse(&argc, argv, opt_log_stderr_exit);
+ * if (argc != 1)
+ * opt_log_stderr_exit("Unknown extra arguments");
+ *
+ * sockfd = oserver_setup(port);
* if (sockfd < 0)
* err(1, "Failed to set up server socket");
*
printf("ccan/foreach\n");
printf("ccan/noerr\n");
printf("ccan/failtest\n");
+ printf("ccan/opt\n");
return 0;
}
#include <ccan/oserver/oserver.h>
#include <ccan/read_write_all/read_write_all.h>
+#include <ccan/opt/opt.h>
#include <ccan/noerr/noerr.h>
#include <sys/types.h>
#include <sys/socket.h>
return true;
}
-int oserver_setup(void)
+int oserver_setup(unsigned short port)
{
int sockfd;
int one = 1;
warn("Setting socket reuse");
u.in.sin_family = AF_INET;
- u.in.sin_port = htons(OSERVER_PORT);
+ u.in.sin_port = htons(port);
u.in.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd, &u.addr, sizeof(u.in)) == -1) {
close_noerr(sockfd);
/**
* oserver_setup - get a listening filedescriptor for an oserver
- *
- * Opens a socket and binds it to OSERVER_PORT, then sets it up to listen
+ * @port: port to use (usually OSERVER_PORT)
+ * Opens a socket and binds it to @port, then sets it up to listen
* for connections and returns it.
*
* Example:
* int serverfd;
*
- * serverfd = oserver_setup();
+ * serverfd = oserver_setup(OSERVER_PORT);
* if (serverfd < 0)
* err(1, "Failed to set up server");
*/
-int oserver_setup(void);
+int oserver_setup(unsigned short port);
/**
* oserver_serve - serve an oserver client via a file descriptor