0

适应 Apache WeatherTelnet代码,我正在开发一个MUD 客户端,但正在努力处理并发输入/输出。当然,不可能同时读取和写入。

对于此应用程序,out应优先于in. 如果什么都没有被写入,java.lang.System.out那么只有在那时,才应该从java.lang.System.in被读取的输入。我只是不知道如何做到这一点,因为inout交织在一起。

运行和内存转储:

thufir@dur:~$ 
thufir@dur:~$ 
thufir@dur:~$ java -jar NetBeansProjects/WeatherTelnet/dist/WeatherTelnet.jar 
------------------------------------------------------------------------------
*               Welcome to THE WEATHER UNDERGROUND telnet service!            *
------------------------------------------------------------------------------
*                                                                            *
*   National Weather Service information provided by Alden Electronics, Inc. *
*    and updated each minute as reports come in over our data feed.          *
*                                                                            *
*   **Note: If you cannot get past this opening screen, you must use a       *
*   different version of the "telnet" program--some of the ones for IBM      *
*   compatible PC's have a bug that prevents proper connection.              *
*                                                                            *
*           comments: jmasters@wunderground.com                              *
------------------------------------------------------------------------------

Press Return to continue:
fgf
d
e
f





2013-08-30 14:13:49
Full thread dump Java HotSpot(TM) Client VM (23.25-b01 mixed mode):

"Thread-2" daemon prio=10 tid=0xa0595400 nid=0x1c60 runnable [0x9fcdf000]
   java.lang.Thread.State: RUNNABLE
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:150)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:273)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    - locked <0xa0af5740> (a java.io.BufferedInputStream)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    - locked <0xa0afa1e0> (a org.apache.commons.net.telnet.TelnetInputStream)
    at org.apache.commons.net.telnet.TelnetInputStream.__read(TelnetInputStream.java:141)
    at org.apache.commons.net.telnet.TelnetInputStream.run(TelnetInputStream.java:611)
    at java.lang.Thread.run(Thread.java:724)

"DestroyJavaVM" prio=10 tid=0xb6b06400 nid=0x1c56 waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Thread-1" prio=10 tid=0x9fd20400 nid=0x1c5f in Object.wait() [0x9fe79000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0xa0afca50> (a [I)
    at java.lang.Object.wait(Object.java:503)
    at org.apache.commons.net.telnet.TelnetInputStream.read(TelnetInputStream.java:392)
    - locked <0xa0afca50> (a [I)
    at org.apache.commons.net.telnet.TelnetInputStream.read(TelnetInputStream.java:534)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    - locked <0xa0afebc8> (a java.io.BufferedInputStream)
    at weathertelnet.WeatherTelnet.consoleOutput(WeatherTelnet.java:65)
    at weathertelnet.WeatherTelnet$2.run(WeatherTelnet.java:33)
    at java.lang.Thread.run(Thread.java:724)

"Thread-0" prio=10 tid=0x9fd1f000 nid=0x1c5e runnable [0xa045c000]
   java.lang.Thread.State: RUNNABLE
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(FileInputStream.java:242)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    - locked <0xa0a16d88> (a java.io.BufferedInputStream)
    at weathertelnet.WeatherTelnet.consoleInput(WeatherTelnet.java:47)
    at weathertelnet.WeatherTelnet$1.run(WeatherTelnet.java:22)
    at java.lang.Thread.run(Thread.java:724)

"Service Thread" daemon prio=10 tid=0xb6bc3400 nid=0x1c5c runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread0" daemon prio=10 tid=0xb6bc1400 nid=0x1c5b waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0xb6bbfc00 nid=0x1c5a waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=10 tid=0xb6b83c00 nid=0x1c59 in Object.wait() [0xa04fe000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0xa0a05698> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
    - locked <0xa0a05698> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:189)

"Reference Handler" daemon prio=10 tid=0xb6b82000 nid=0x1c58 in Object.wait() [0xa065d000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0xa0a05270> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:503)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)
    - locked <0xa0a05270> (a java.lang.ref.Reference$Lock)

"VM Thread" prio=10 tid=0xb6b7c400 nid=0x1c57 runnable 

"VM Periodic Task Thread" prio=10 tid=0xb6bcd800 nid=0x1c5d waiting on condition 

JNI global references: 171

Heap
 def new generation   total 4928K, used 1299K [0xa0a00000, 0xa0f50000, 0xa5f50000)
  eden space 4416K,  29% used [0xa0a00000, 0xa0b44d90, 0xa0e50000)
  from space 512K,   0% used [0xa0e50000, 0xa0e50000, 0xa0ed0000)
  to   space 512K,   0% used [0xa0ed0000, 0xa0ed0000, 0xa0f50000)
 tenured generation   total 10944K, used 0K [0xa5f50000, 0xa6a00000, 0xb0a00000)
   the space 10944K,   0% used [0xa5f50000, 0xa5f50000, 0xa5f50200, 0xa6a00000)
 compacting perm gen  total 12288K, used 2060K [0xb0a00000, 0xb1600000, 0xb4a00000)
   the space 12288K,  16% used [0xb0a00000, 0xb0c030d0, 0xb0c03200, 0xb1600000)
No shared spaces configured.




^Cthufir@dur:~$ 
thufir@dur:~$ 
thufir@dur:~$ 
thufir@dur:~$ 

代码:

package weathertelnet;

import static java.lang.System.in;
import static java.lang.System.out;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.SocketException;
import org.apache.commons.net.telnet.TelnetClient;

public final class WeatherTelnet implements Runnable {

    @Override
    public void run() {


        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    consoleInput();
                } catch (IOException ex) {
                }
            }
        }).start();

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    consoleOutput();
                } catch (SocketException ex) {
                } catch (IOException ex) {
                }
            }
        }).start();
    }

    public void consoleInput() throws IOException {
        do {
            char ch = (char) in.read();
            StringBuilder s = new StringBuilder();
            s.append(ch);
            while (255 > ch && ch >= 0) {
                ch = (char) in.read();
                s.append(ch);
            }
            out.println("\tyour text\t"+ s);
        } while (true);        
    }

    public void consoleOutput() throws SocketException, IOException {
        TelnetClient tc;
        tc = new TelnetClient();
        tc.connect("rainmaker.wunderground.com", 3000);

        InputStream inputStream = tc.getInputStream();

        char ch = (char) inputStream.read();

        while (255 > ch && ch >= 0) {
            out.print(ch);
            ch = (char) inputStream.read();
        }
    }

    public WeatherTelnet() {
        run();
    }

    public static void main(String[] args) {
        new WeatherTelnet();
    }
}

*代码在 ASL 下,请:

4

1 回答 1

0

处理此问题的正确方法是将其交给实用程序类,然后examples.util.IOUtil从 Apache 导入:

package weathertelnet;

import examples.util.IOUtil;
import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Properties;
import org.apache.commons.net.telnet.TelnetClient;

public class WeatherTelnet {

    private TelnetClient telnetConnection = new TelnetClient();

    public WeatherTelnet() throws SocketException, IOException {
        System.out.println("weather telnet..");
        Properties props = PropertiesReader.getProps();
        InetAddress host = InetAddress.getByName(props.getProperty("host"));
        int port = Integer.parseInt(props.getProperty("port"));
        telnetConnection.connect(host, port);
        System.out.println("connected");
        IOUtil.readWrite(telnetConnection.getInputStream(), telnetConnection.getOutputStream(), System.in, System.out);
    }

    public static void main(String[] args) throws SocketException, IOException {
        System.out.println("main...");
        new WeatherTelnet();
    }
}
于 2013-08-30T23:34:20.820 回答