libmp/examples/server/main.c

53 lines
1.1 KiB
C

#include "../../include/error.h"
#include "../../include/mptp.h"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
int main(int argc, char *argv[]) {
int ret = EXIT_FAILURE;
if (argc != 3) {
printf("usage: %s <host> <port>\n", argv[0]);
return ret;
}
int porti = atoi(argv[2]);
if (porti <= 0 || porti > UINT16_MAX) {
printf("bad port number\n");
return ret;
}
int sock = lm_mptp_server_listen(argv[1], porti);
if (sock < 0) {
printf("failed to start the server: %s\n", lm_strerror());
return ret;
}
lm_mptp_t packet;
struct sockaddr addr;
while (lm_mptp_server_recv(sock, &packet, &addr)) {
switch (MPTP_FLAGS_TYPE(&packet)) {
case MPTP_C2S_PING:
bzero(&packet, sizeof(packet));
lm_mptp_packet_init(&packet, false, MPTP_S2C_PONG, true);
lm_mptp_server_send(sock, &packet, &addr);
break;
default:
bzero(&packet, sizeof(packet));
lm_mptp_packet_init(&packet, false, MPTP_S2C_WHAT, true);
lm_mptp_server_send(sock, &packet, &addr);
break;
}
}
ret = EXIT_SUCCESS;
end:
lm_mptp_close(sock);
return ret;
}