Use UDP when using sptps_test in datagram mode.

This commit is contained in:
Guus Sliepen 2013-02-22 15:37:48 +01:00
parent a93c0139c5
commit 4d05e695ab

View file

@ -77,8 +77,8 @@ int main(int argc, char *argv[]) {
memset(&hint, 0, sizeof hint); memset(&hint, 0, sizeof hint);
hint.ai_family = AF_UNSPEC; hint.ai_family = AF_UNSPEC;
hint.ai_socktype = SOCK_STREAM; hint.ai_socktype = datagram ? SOCK_DGRAM : SOCK_STREAM;
hint.ai_protocol = IPPROTO_TCP; hint.ai_protocol = datagram ? IPPROTO_UDP : IPPROTO_TCP;
hint.ai_flags = initiator ? 0 : AI_PASSIVE; hint.ai_flags = initiator ? 0 : AI_PASSIVE;
if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) { if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
@ -86,7 +86,7 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
int sock = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP); int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if(sock < 0) { if(sock < 0) {
fprintf(stderr, "Could not create socket: %s\n", strerror(errno)); fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
return 1; return 1;
@ -106,6 +106,8 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Could not bind socket: %s\n", strerror(errno)); fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
return 1; return 1;
} }
if(!datagram) {
if(listen(sock, 1)) { if(listen(sock, 1)) {
fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno)); fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno));
return 1; return 1;
@ -117,6 +119,23 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Could not accept connection: %s\n", strerror(errno)); fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
return 1; return 1;
} }
} else {
fprintf(stderr, "Listening...\n");
char buf[65536];
struct sockaddr addr;
socklen_t addrlen = sizeof addr;
if(recvfrom(sock, buf, sizeof buf, MSG_PEEK, &addr, &addrlen) <= 0) {
fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
return 1;
}
if(connect(sock, &addr, addrlen)) {
fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
return 1;
}
}
fprintf(stderr, "Connected\n"); fprintf(stderr, "Connected\n");
} }