VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/sockpong.c@ 89890

最後變更 在這個檔案從89890是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 3.8 KB
 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999-2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/*
39 * File: sockpong.c
40 *
41 * Description:
42 * This test runs in conjunction with the sockping test.
43 * The sockping test creates a socket pair and passes one
44 * socket to this test. Then the sockping test writes
45 * "ping" to this test and this test writes "pong" back.
46 * To run this pair of tests, just invoke sockping.
47 *
48 * Tested areas: process creation, socket pairs, file
49 * descriptor inheritance.
50 */
51
52#include "prerror.h"
53#include "prio.h"
54
55#include <stdio.h>
56#include <string.h>
57#include <stdlib.h>
58
59#define NUM_ITERATIONS 10
60
61int main()
62{
63 PRFileDesc *sock;
64 PRStatus status;
65 char buf[1024];
66 PRInt32 nBytes;
67 int idx;
68
69 sock = PR_GetInheritedFD("SOCKET");
70 if (sock == NULL) {
71 fprintf(stderr, "PR_GetInheritedFD failed\n");
72 exit(1);
73 }
74 status = PR_SetFDInheritable(sock, PR_FALSE);
75 if (status == PR_FAILURE) {
76 fprintf(stderr, "PR_SetFDInheritable failed\n");
77 exit(1);
78 }
79
80 for (idx = 0; idx < NUM_ITERATIONS; idx++) {
81 memset(buf, 0, sizeof(buf));
82 nBytes = PR_Read(sock, buf, sizeof(buf));
83 if (nBytes == -1) {
84 fprintf(stderr, "PR_Read failed: (%d, %d)\n",
85 PR_GetError(), PR_GetOSError());
86 exit(1);
87 }
88 printf("pong process: received \"%s\"\n", buf);
89 if (nBytes != 5) {
90 fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n",
91 nBytes);
92 exit(1);
93 }
94 if (strcmp(buf, "ping") != 0) {
95 fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n",
96 buf);
97 exit(1);
98 }
99
100 strcpy(buf, "pong");
101 printf("pong process: sending \"%s\"\n", buf);
102 nBytes = PR_Write(sock, buf, 5);
103 if (nBytes == -1) {
104 fprintf(stderr, "PR_Write failed\n");
105 exit(1);
106 }
107 }
108
109 status = PR_Close(sock);
110 if (status == PR_FAILURE) {
111 fprintf(stderr, "PR_Close failed\n");
112 exit(1);
113 }
114 return 0;
115}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette