.\" Copyright (c
) 1986 Regents of the University of California
.
.\" All rights reserved
. The Berkeley software License Agreement
.\" specifies the terms
and conditions
for redistribution
.
.\" @
(#)strchkread.c 6.2 (Berkeley) %G%
* This program uses select() to check that someone is trying to connect
* before calling accept().
struct sockaddr_in server
;
sock
= socket(AF_INET
, SOCK_STREAM
, 0);
perror("opening stream socket");
/* Name socket using wildcards */
server
.sin_family
= AF_INET
;
server
.sin_addr
.s_addr
= INADDR_ANY
;
if (bind(sock
, &server
, sizeof(server
))) {
perror("binding stream socket");
/* Find out assigned port number and print it out */
if (getsockname(sock
, &server
, &length
)) {
perror("getting socket name");
printf("Socket has port #%d\en", ntohs(server
.sin_port
));
/* Start accepting connections */
if (select(sock
+ 1, &ready
, 0, 0, &to
) < 0) {
if (FD_ISSET(sock
, &ready
)) {
msgsock
= accept(sock
, (struct sockaddr
*)0, (int *)0);
if ((rval
= read(msgsock
, buf
, 1024)) < 0)
perror("reading stream message");
printf("Ending connection\en");
printf("Do something else\en");