sockpong.c revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
0N/A/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
0N/A/* ***** BEGIN LICENSE BLOCK *****
0N/A * Version: MPL 1.1/GPL 2.0/LGPL 2.1
0N/A *
0N/A * The contents of this file are subject to the Mozilla Public License Version
2362N/A * 1.1 (the "License"); you may not use this file except in compliance with
0N/A * the License. You may obtain a copy of the License at
2362N/A * http://www.mozilla.org/MPL/
0N/A *
0N/A * Software distributed under the License is distributed on an "AS IS" basis,
0N/A * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
0N/A * for the specific language governing rights and limitations under the
0N/A * License.
0N/A *
0N/A * The Original Code is the Netscape Portable Runtime (NSPR).
0N/A *
0N/A * The Initial Developer of the Original Code is
0N/A * Netscape Communications Corporation.
0N/A * Portions created by the Initial Developer are Copyright (C) 1999-2000
2362N/A * the Initial Developer. All Rights Reserved.
2362N/A *
2362N/A * Contributor(s):
0N/A *
0N/A * Alternatively, the contents of this file may be used under the terms of
0N/A * either the GNU General Public License Version 2 or later (the "GPL"), or
0N/A * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
0N/A * in which case the provisions of the GPL or the LGPL are applicable instead
0N/A * of those above. If you wish to allow use of your version of this file only
0N/A * under the terms of either the GPL or the LGPL, and not to allow others to
0N/A * use your version of this file under the terms of the MPL, indicate your
0N/A * decision by deleting the provisions above and replace them with the notice
0N/A * and other provisions required by the GPL or the LGPL. If you do not delete
3984N/A * the provisions above, a recipient may use your version of this file under
0N/A * the terms of any one of the MPL, the GPL or the LGPL.
0N/A *
0N/A * ***** END LICENSE BLOCK ***** */
0N/A
0N/A/*
0N/A * File: sockpong.c
0N/A *
0N/A * Description:
0N/A * This test runs in conjunction with the sockping test.
0N/A * The sockping test creates a socket pair and passes one
0N/A * socket to this test. Then the sockping test writes
0N/A * "ping" to this test and this test writes "pong" back.
0N/A * To run this pair of tests, just invoke sockping.
0N/A *
0N/A * Tested areas: process creation, socket pairs, file
0N/A * descriptor inheritance.
0N/A */
0N/A
0N/A#include "prerror.h"
0N/A#include "prio.h"
0N/A
0N/A#include <stdio.h>
3203N/A#include <string.h>
0N/A#include <stdlib.h>
0N/A
0N/A#define NUM_ITERATIONS 10
0N/A
3203N/Aint main()
0N/A{
0N/A PRFileDesc *sock;
0N/A PRStatus status;
0N/A char buf[1024];
0N/A PRInt32 nBytes;
0N/A int idx;
0N/A
0N/A sock = PR_GetInheritedFD("SOCKET");
0N/A if (sock == NULL) {
0N/A fprintf(stderr, "PR_GetInheritedFD failed\n");
0N/A exit(1);
0N/A }
0N/A status = PR_SetFDInheritable(sock, PR_FALSE);
0N/A if (status == PR_FAILURE) {
0N/A fprintf(stderr, "PR_SetFDInheritable failed\n");
0N/A exit(1);
0N/A }
0N/A
0N/A for (idx = 0; idx < NUM_ITERATIONS; idx++) {
0N/A memset(buf, 0, sizeof(buf));
0N/A nBytes = PR_Read(sock, buf, sizeof(buf));
0N/A if (nBytes == -1) {
0N/A fprintf(stderr, "PR_Read failed: (%d, %d)\n",
0N/A PR_GetError(), PR_GetOSError());
0N/A exit(1);
0N/A }
0N/A printf("pong process: received \"%s\"\n", buf);
0N/A if (nBytes != 5) {
0N/A fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n",
0N/A nBytes);
0N/A exit(1);
0N/A }
0N/A if (strcmp(buf, "ping") != 0) {
0N/A fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n",
0N/A buf);
0N/A exit(1);
0N/A }
0N/A
0N/A strcpy(buf, "pong");
0N/A printf("pong process: sending \"%s\"\n", buf);
0N/A nBytes = PR_Write(sock, buf, 5);
0N/A if (nBytes == -1) {
0N/A fprintf(stderr, "PR_Write failed\n");
0N/A exit(1);
0N/A }
0N/A }
0N/A
0N/A status = PR_Close(sock);
0N/A if (status == PR_FAILURE) {
0N/A fprintf(stderr, "PR_Close failed\n");
0N/A exit(1);
0N/A }
0N/A return 0;
0N/A}
0N/A