Commit af64f7ad authored by Robert Sprowson's avatar Robert Sprowson
Browse files

Add test case

In support of Internet-5_65.
parent 855d1062
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef __riscos
#include "socklib.h"
#else
#include "unistd.h"
#define socketclose(k) close(k)
#endif
#include "sys/errno.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "arpa/inet.h"
/* Purpose: open a socket, give it a receive timeout, trigger
* a receive for which no response is expected, see
* if the timeout fires
*
* RISC OS: cc -o test timeo.c -Wp -ITCPIPLibs: -lTCPIPLibs:socklib5.o -lTCPIPLibs:inetlib.o -lCLib:stubs.o
* run test
* FreeBSD: cc -o test timeo.c -Wno-pointer-sign
* ./test
*/
static void showtimeout(int s)
{
struct timeval t;
int result, len = sizeof(t);
result = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (void *)&t, &len);
if (result < 0)
{
printf("timeout is unreadable\n");
return;
}
if (t.tv_sec || t.tv_usec)
{
printf("timeout is %lds+%ldus\n", t.tv_sec, t.tv_usec);
}
else
{
printf("timeout is unset\n");
}
}
int main(void)
{
int s = -1, result;
struct timeval timeout;
struct sockaddr_in sa;
char data[64];
clock_t start, end;
errno = 0;
s = socket(PF_INET, SOCK_STREAM, 0);
printf("socket() returned %d (errno = %d)\n", s, errno);
if (s < 0) goto bad;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(80);
inet_aton("91.203.57.172" /* riscosopen.org */, &sa.sin_addr);
result = connect(s, (const struct sockaddr *)&sa, sizeof(sa));
printf("connect() returned %d (errno = %d)\n", result, errno);
if (result != 0) goto bad;
/* Before, try set, after */
showtimeout(s);
timeout.tv_sec = 10;
timeout.tv_usec = 0;
result = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const void *)&timeout, sizeof(struct timeval));
printf("setsockopt(SO_RCVTIMEO) returned %d (errno = %d)\n", result, errno);
if (result != 0) goto bad;
showtimeout(s);
/* Don't request anything, just wait for data to never come */
start = clock();
result = recv(s, data, sizeof(data), 0);
end = clock();
printf("recv() returned %d (errno = %d) "
"after waiting for %lfs\n", result, errno, (double)(end - start) / (double)CLOCKS_PER_SEC);
socketclose(s);
return 0;
bad:
if (s != -1) socketclose(s);
exit(EXIT_FAILURE);
}
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