0

我正在尝试运行一个简单的回显程序,但是它一直在捕获 IOException,我不知道为什么!有人知道我哪里出错了吗?(BAHBAHBAH 是我电脑的名称)。

import java.io.*;
import java.net.*;

public class EchoClient {
public static void main(String[] args) throws IOException {

    Socket echoSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
        echoSocket = new Socket("BAHBAHBAH", 7);
        out = new PrintWriter(echoSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(
                                    echoSocket.getInputStream()));
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: taranis.");
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for "
                           + "the connection to: taranis.");
        System.exit(1);
    }

 BufferedReader stdIn = new BufferedReader(
                               new InputStreamReader(System.in));
 String userInput;

 while ((userInput = stdIn.readLine()) != null) {
 out.println(userInput);
 System.out.println("echo: " + in.readLine());
 }

 out.close();
 in.close();
 stdIn.close();
 echoSocket.close();
 }
 }

堆栈跟踪如下:

 java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at EchoClient.main(EchoClient.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.dynamicjava.symbol.JavaClass$JavaMethod.evaluate(JavaClass.java:362)
at             edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.handleMethodCall(ExpressionEvaluator.java:92     )
at  .rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:84)
at koala.dynamicjava.tree.StaticMethodCall.acceptVisitor(StaticMethodCall.java:121)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:38)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:37)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:106)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:29)
at koala.dynamicjava.tree.ExpressionStatement.acceptVisitor(ExpressionStatement.java:101)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.evaluateSequence(StatementEvaluator.java:66)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.evaluate(Interpreter.java:77)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.interpret(Interpreter.java:47)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:246)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:220)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

无法获得连接到的 I/O:taranis。

4

3 回答 3

3

这句话最有可能存在一个问题:

    echoSocket = new Socket("BAHBAHBAH", 7);

确保您尝试连接的主机名和端口是正确的。我怀疑他们两个都不正确。0-1024 之间有保留端口,不确定您尝试在端口 7 上连接哪个服务。

确保某些服务器正在侦听端口 7 和主机名“BAHBAHBAH”。您可以通过尝试使用以下命令在该主机和端口上执行 telnet 来检查这一点:

telnet BAHBAHBAH 7

如果它没有出现任何错误,则意味着在该主机和端口上有一个服务正在侦听。

于 2013-07-31T02:45:53.203 回答
1

您还需要运行服务器程序并在同一端口上侦听。

Socket serverEchoSocket;
ServerSocket echoService;

try {
   echoService = new ServerSocket(7);
   serverEchoSocket = echoService.accept();
} catch (IOException e) {
   System.out.println(e);
}
于 2013-07-31T02:44:56.260 回答
1

我认为在获得输入流之前必须将套接字置于侦听状态......并且在收到连接时获得输出流

于 2013-07-31T02:55:08.743 回答