gpt4 book ai didi

java - In & Out 无法解析为类型 (Java Socket)

转载 作者:行者123 更新时间:2023-12-01 13:50:19 24 4
gpt4 key购买 nike

我真的不知道问题出在哪里。我正在学习 Java,并且正在关注 Oracle 的网络文档。我只是复制并粘贴了他们的示例并得到了这个错误。有人可以帮忙吗?谢谢:)

这是来自 Oracle 的代码:

import java.net.Socket;
import java.net.ServerSocket;

public class EchoServer {

public static void main(String[] args) throws Exception {

// create socket
int port = 4444;
ServerSocket serverSocket = new ServerSocket(port);
System.err.println("Started server on port " + port);

// repeatedly wait for connections, and process
while (true) {

// a "blocking" call which waits until a connection is requested
Socket clientSocket = serverSocket.accept();
System.err.println("Accepted connection from client");

// open up IO streams
In in = new In (clientSocket);
Out out = new Out(clientSocket);

// waits for data and reads it in until connection dies
// readLine() blocks until the server receives a new line from client
String s;
while ((s = in.readLine()) != null) {
out.println(s);
}

// close IO streams, then socket
System.err.println("Closing connection with client");
out.close();
in.close();
clientSocket.close();
}
}
}

最佳答案

看来你复制错了。

我第一次尝试就能够运行它。

echo: 
hi mom
echo: hi mom
how are you?
echo: how are you?

我拿走了他们所拥有的东西,并对其进行了一些重新混合,以使其更简单(更少的代码,并且它做了同样的事情)。您需要 JDK 7,但您可以将它们放入 IDE 中。首先点击在服务器上运行。然后点击客户端上的运行。然后点击IDE的输出区域,开始在客户端中输入。或者您可以从命令行运行它们。

package com.examples;
import java.net.*;
import java.io.*;

public class EchoServer {
public static void main(String... args) throws IOException {

int port;

if ( args.length != 1 ) {
System.out.println("listening to port 9999");
port = 9999;
} else {
port = Integer.parseInt ( args[ 0 ] );

}

try (
ServerSocket serverSocket =
new ServerSocket(port);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.println(inputLine);
}
}
}
}

上面使用了新的 JDK 7 try 和自动关闭语法。你要这个。

然后这是重新混合的客户端。

package com.examples;

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

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

String host;
int port;

if ( args.length != 2 ) {
System.out.println("binding to port localhost:9999");
host = "localhost";
port = 9999;
} else {
host = args[ 0 ];
port = Integer.parseInt ( args[ 1 ] );
}

try (
Socket echoSocket = new Socket ( host, port );
PrintWriter out =
new PrintWriter ( echoSocket.getOutputStream (), true );
BufferedReader in =
new BufferedReader (
new InputStreamReader ( echoSocket.getInputStream () ) );
BufferedReader stdIn =
new BufferedReader (
new InputStreamReader ( System.in ) )
) {
System.out.println("Type in some text please.");
String userInput;
while ( ( userInput = stdIn.readLine () ) != null ) {
out.println ( userInput );
System.out.println ( "echo: " + in.readLine () );
}
}
}
}

服务器如下:

我们从传递给 main 的参数中获取端口号,或者如果它们没有在命令行上传递,则将它们设置为 9999。

    int port;

if ( args.length != 1 ) {
System.out.println("listening to port 9999");
port = 9999;
} else {
port = Integer.parseInt ( args[ 0 ] );

}

然后在 try 语句括号中我们打开服务器套接字 streak gak

            ServerSocket serverSocket =
new ServerSocket(port);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));

如果上面的内容没有意义,那么您必须了解有关流的更多信息。转到有关输入/输出(读取和写入文件)的教程,然后返回到此。

基本上,它将服务器套接字的输出流包装在 PrintWriter 中,并将输入流包装在 BufferedReader 中。如果您以前没有使用过 Java I/O API,那么它会有点令人畏惧,所以我建议先从 I/O 教程开始。

原文如下:
EchoClient
EchoServer

根据你搞砸的地方,开始阅读 IO-Streams ,然后 IO-Character Streams .

然后在几个小时的背景知识之后回到这里。

关于java - In & Out 无法解析为类型 (Java Socket),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20014213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com