Commit 2614d54c authored by Andreas Schmidt's avatar Andreas Schmidt

TCP time-apps.

* IPv4 instead of IPv6.
* Reliable send/recv.
parent 6085dafc
#include <assert.h>
#ifdef XLAP
......@@ -12,6 +11,7 @@
# include <sys/socket.h>
# include <netdb.h>
# include <unistd.h>
# include <netinet/in.h>
typedef int socket_t;
......@@ -45,17 +45,18 @@ static inline socket_t _open_sender(struct arguments *args)
static inline socket_t _open_receiver(struct arguments *args)
{
int ssock = socket(AF_INET6, SOCK_STREAM, 0);
int ssock = socket(AF_INET, SOCK_STREAM, 0);
assert(ssock >= 0);
int yes = 1;
setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
struct sockaddr_in6 address = {
.sin6_family = AF_INET6,
.sin6_port = htons(args->port),
.sin6_addr = in6addr_any,
struct sockaddr_in address = {
.sin_family = AF_INET,
.sin_port = htons(args->port),
};
memset(address.sin_zero, '\0', sizeof(address.sin_zero));
address.sin_addr.s_addr = htonl(INADDR_ANY);
assert(0 == bind(ssock, (const struct sockaddr *) &address, sizeof(address)));
assert(0 == listen(ssock, SOMAXCONN));
......@@ -70,12 +71,28 @@ static inline socket_t _open_receiver(struct arguments *args)
static inline void _send(socket_t conn, const char *buf, size_t size)
{
write(conn, buf, size);
int written = 0;
int n = 0;
while (written < size) {
n = send(conn, buf, size, 0);
written += n;
buf += n;
}
}
static inline ssize_t _recv(socket_t conn, char *buf, size_t size)
{
return read(conn, buf, size);
ssize_t received = 0;
int n = 0;
while (received < size) {
n = recv(conn, buf, size - received, 0);
if (n == 0) {
break;
}
buf += n;
received += n;
}
return received;
}
static inline void _close(socket_t conn)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment