⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij0167.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 4 页
字号:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
The function 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>fseek(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
moves around in the file; here it is used to move to the top of the file. 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>fgets(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
reads a line from the file 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>list</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
into the buffer 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>lbuf</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
not exceeding the buffer size 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BSIZE</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This is inside a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>while
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">loop
so that each line in the file is read. Next, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>strchr(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is used to locate the newline character so that it can be stripped off. Finally, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>strcmp(&#160;)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
used to compare the name you&#8217;ve passed into the function to the current
line int the file. 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>strcmp(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
returns zero if it finds a match. In this case the function exits and a one is
returned to indicate that yes, the name was already in the list. (Note that the
function returns as soon as it discovers the match, so it doesn&#8217;t waste
time looking at the rest of the list.) If you get all the way through the list
without a match, the function returns zero.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the file is opened using 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>fopen(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The first argument is the file name and the second is the way to open the file; 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>a+</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
means &#8220;Append, and open (or create if the file does not exist) for update
at the end of the file.&#8221; The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>fopen(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
function returns a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FILE</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
pointer which, if it&#8217;s zero, means that the open was unsuccessful. This
is dealt with by printing an error message with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>perror(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and terminating the program with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>exit(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Assuming
that the file was opened successfully, the program enters an infinite loop. The
function call 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>gets(buf)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
gets a line from standard input (which will be connected to the Java program,
remember) and places it in the buffer 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>buf</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This is simply passed to the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>alreadyInList(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
function, and if it&#8217;s already in the list, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>printf(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
sends that message to standard output (where the Java program is listening). 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>fflush(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is a way to flush the output buffer.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
the name is not already in the list, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>fseek(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is used to move to the end of the list and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>fprintf(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
&#8220;prints&#8221; the name to the end of the list. Then 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>printf(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is used to indicate that the name was added to the list (again flushing
standard output) and the infinite loop goes back to waiting for a new name.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Remember
that you usually cannot compile this program on your computer and load it onto
the Web server machine, since that machine might use a different processor and
operating system. For example, my Web server runs on an Intel processor but it
uses Linux, so I must download the source code and compile using remote
commands (via telnet) with the C compiler that comes with the Linux distribution.
</FONT><P></DIV>
<A NAME="Heading523"></A><H4 ALIGN=LEFT>
The
Java program
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
program will first start the C program above and make the necessary connections
to talk to it. Then it will create a datagram socket that will be used to
listen for datagram packets from the applet.
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: NameCollector.java</font>
<font color="#009900">// Extracts email names from datagrams and stores</font>
<font color="#009900">// them inside a file, using Java 1.02.</font>
<font color="#0000ff">import</font> java.net.*;
<font color="#0000ff">import</font> java.io.*;
<font color="#0000ff">import</font> java.util.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> NameCollector {
  <font color="#0000ff">final</font> <font color="#0000ff">static</font> <font color="#0000ff">int</font> COLLECTOR_PORT = 8080;
  <font color="#0000ff">final</font> <font color="#0000ff">static</font> <font color="#0000ff">int</font> BUFFER_SIZE = 1000;
  <font color="#0000ff">byte</font>[] buf = <font color="#0000ff">new</font> <font color="#0000ff">byte</font>[BUFFER_SIZE];
  DatagramPacket dp = 
    <font color="#0000ff">new</font> DatagramPacket(buf, buf.length);
  <font color="#009900">// Can listen &amp; send on the same socket:</font>
  DatagramSocket socket;
  Process listmgr;
  PrintStream nameList;
  DataInputStream addResult;
  <font color="#0000ff">public</font> NameCollector() {
    <font color="#0000ff">try</font> {
      listmgr =
        Runtime.getRuntime().exec("listmgr.exe");
      nameList = <font color="#0000ff">new</font> PrintStream(
        <font color="#0000ff">new</font> BufferedOutputStream(
          listmgr.getOutputStream()));
      addResult = <font color="#0000ff">new</font> DataInputStream(
        <font color="#0000ff">new</font> BufferedInputStream(
          listmgr.getInputStream()));

    } <font color="#0000ff">catch</font>(IOException e) {
      System.err.println(
        "Cannot start listmgr.exe");
      System.exit(1);
    }
    <font color="#0000ff">try</font> {
      socket =
        <font color="#0000ff">new</font> DatagramSocket(COLLECTOR_PORT);
      System.out.println(
        "NameCollector Server started");
      <font color="#0000ff">while</font>(<font color="#0000ff">true</font>) {
        <font color="#009900">// Block until a datagram appears:</font>
        socket.receive(dp);
        String rcvd = <font color="#0000ff">new</font> String(dp.getData(),
            0, 0, dp.getLength());
        <font color="#009900">// Send to listmgr.exe standard input:</font>
        nameList.println(rcvd.trim());
        nameList.flush();
        <font color="#0000ff">byte</font>[] resultBuf = <font color="#0000ff">new</font> <font color="#0000ff">byte</font>[BUFFER_SIZE];
        <font color="#0000ff">int</font> byteCount = 
          addResult.read(resultBuf);
        <font color="#0000ff">if</font>(byteCount != -1) {
          String result = 
            <font color="#0000ff">new</font> String(resultBuf, 0).trim();
          <font color="#009900">// Extract the address and port from </font>
          <font color="#009900">// the received datagram to find out </font>
          <font color="#009900">// where to send the reply:</font>
          InetAddress senderAddress =
            dp.getAddress();
          <font color="#0000ff">int</font> senderPort = dp.getPort();
          <font color="#0000ff">byte</font>[] echoBuf = <font color="#0000ff">new</font> <font color="#0000ff">byte</font>[BUFFER_SIZE];
          result.getBytes(
            0, byteCount, echoBuf, 0);
          DatagramPacket echo =
            <font color="#0000ff">new</font> DatagramPacket(
              echoBuf, echoBuf.length,
              senderAddress, senderPort);
          socket.send(echo);
        }
        <font color="#0000ff">else</font>
          System.out.println(
            "Unexpected lack of result from " +
            "listmgr.exe");
      }
    } <font color="#0000ff">catch</font>(SocketException e) {
      System.err.println("Can't open socket");
      System.exit(1);
    } <font color="#0000ff">catch</font>(IOException e) {
      System.err.println("Communication error");
      e.printStackTrace();
    }
  }
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    <font color="#0000ff">new</font> NameCollector();
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first definitions in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>NameCollector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
should look familiar: the port is chosen, a datagram packet is created, and
there&#8217;s a handle to a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DatagramSocket</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The next three definitions concern the connection to the C program: a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Process</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is what comes back when the C program is fired up by the Java program,
and that 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Process</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object produces the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>OutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects representing, respectively, the standard output and standard input of
the C program. These must of course be &#8220;wrapped&#8221; as is usual with
Java IO, so we end up with a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">All
the work for this program happens inside the constructor. To start up the C
program, the current 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Runtime</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is procured. This is used to call <A NAME="Index2719"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>exec(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which returns the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Process</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object. You can see that there are simple calls to produce the streams from the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Process</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -