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"
#include "unistd.h"
#define socketclose(k) close(k)
#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");
if (t.tv_sec || t.tv_usec)
printf("timeout is %lds+%ldus\n", t.tv_sec, t.tv_usec);
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("" /* */, &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 */
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;
/* 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);
return 0;
if (s != -1) socketclose(s);
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