Example #1
0
int
main(int argc, char **argv)
{
	int		n, flags;
	char	buff[NREAD+1];		/* +1 for null at end */

	if (argc == 2)
		listenfd = Tcp_listen(NULL, argv[1], NULL);
	else if (argc == 3)
		listenfd = Tcp_listen(argv[1], argv[2], NULL);
	else
		err_quit("usage: tcprecv02 [ <host> ] <port#>");

	connfd = Xti_accept(listenfd, NULL, NULL);

	Signal(SIGPOLL, sig_poll);
	Ioctl(connfd, I_SETSIG, S_RDNORM);

	for ( ; ; ) {
		flags = 0;
		if ( (n = t_rcv(connfd, buff, NREAD, &flags)) < 0) {
			if (t_errno == TLOOK) {
				if ( (n = T_look(connfd)) == T_ORDREL) {
					printf("received T_ORDREL\n");
					exit(0);
				} else
					err_quit("unexpected event after t_rcv: %d", n);
			}
			err_xti("t_rcv error");
		}
		buff[n] = 0;	/* null terminate */
		printf("read %d bytes: %s, flags = %s\n",
			   n, buff, Xti_flags_str(flags));
	}
}
Example #2
0
File: serv01.c Project: rkks/refer
int
main(int argc, char **argv)
{
	int				listenfd, connfd;
	pid_t			childpid;
	void			sig_chld(int), sig_int(int), web_child(int);
	socklen_t		addrlen;
	struct netbuf	cliaddr;

	if (argc == 2)
		listenfd = Tcp_listen(NULL, argv[1], &addrlen);
	else if (argc == 3)
		listenfd = Tcp_listen(argv[1], argv[2], &addrlen);
	else
		err_quit("usage: serv01 [ <host> ] <port#>");
	cliaddr.buf = Malloc(addrlen);
	cliaddr.maxlen = addrlen;

	Signal(SIGCHLD, sig_chld);
	Signal(SIGINT, sig_int);

	for ( ; ; ) {
		connfd = Xti_accept(listenfd, &cliaddr, 1);
		printf("connection from %s\n", Xti_ntop(&cliaddr));

		if ( (childpid = Fork()) == 0) {	/* child process */
			Close(listenfd);	/* close listening socket */
			web_child(connfd);	/* process the request */
			exit(0);
		}
		Close(connfd);			/* parent closes connected socket */
	}
}
Example #3
0
int
main(int argc, char **argv)
{
	int				listenfd, connfd, n, flags;
	char			buff[MAXLINE];
	struct pollfd	fds[1];

	if (argc == 2)
		listenfd = Tcp_listen(NULL, argv[1], NULL);
	else if (argc == 3)
		listenfd = Tcp_listen(argv[1], argv[2], NULL);
	else
		err_quit("usage: daytimetcpsrv01 [ <host> ] <service or port>");
	
	connfd = Xti_accept(listenfd, NULL, 0);

	fds[0].fd = connfd;
	fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI;
	for ( ; ; ) {
		n = poll(fds, 1, INFTIM);
		printf("poll returned %d, revents = 0x%x\n", n, fds[0].revents);

		n = T_rcv(connfd, buff, sizeof(buff), &flags);
		printf("received %d bytes, flags = %d\n", n, flags);
	}
}
Example #4
0
int
main(int argc, char **argv)
{
	int		n, flags;
	char	buff[NREAD+1];		/* +1 for null at end */
	struct pollfd	pollfd[1];

	if (argc == 2)
		listenfd = Tcp_listen(NULL, argv[1], NULL);
	else if (argc == 3)
		listenfd = Tcp_listen(argv[1], argv[2], NULL);
	else
		err_quit("usage: tcprecv05 [ <host> ] <port#>");

	connfd = Xti_accept(listenfd, NULL, NULL);
	sleep(5);

	pollfd[0].fd = connfd;
	pollfd[0].events = POLLIN;

	for ( ; ; ) {
		Poll(pollfd, 1, INFTIM);

		printf("revents = %x\n", pollfd[0].revents);
		if (pollfd[0].revents & POLLIN) {
			flags = 0;
			if ( (n = t_rcv(connfd, buff, NREAD, &flags)) < 0) {
				if (t_errno == TLOOK) {
					if ( (n = T_look(connfd)) == T_ORDREL) {
						printf("received T_ORDREL\n");
						exit(0);
					} else
						err_quit("unexpected event after t_rcv: %d", n);
				}
				err_xti("t_rcv error");
			}
			buff[n] = 0;	/* null terminate */
			printf("read %d bytes: %s, flags = %s\n",
				   n, buff, Xti_flags_str(flags));
		}
	}
}