gpt4 book ai didi

Java Simple EchoServer 无法工作

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:46:07 25 4
gpt4 key购买 nike

我通读了很多,发现很多例子都在做我想做的事情。但是我根本找不到我的代码中的问题。可能我只是需要一双全新的眼睛来审视我的代码。 因此,这里有被标记为重复线程的风险。我有一个简单的 Java 代码。它打开一个端口。将套接字连接到它。获取输入流和输出流。将一些文本放入输出流,输入流尝试读取文本。当执行 readLine 的方法时,它不会返回代码。它只是继续运行,永远不会回到 main 方法。

import java.net.*;
import java.io.*;
import java.io.ObjectInputStream.GetField;

public class echoserver {

public static void main(String[] args) {
// TODO Auto-generated method stub
String hostName = "127.0.0.1";
// InetAddress.getLocalHost()
int portNumber = 5000;
ServerSocket ss = null;
Socket echoSocket = null;


try {
ss = new ServerSocket(portNumber);
echoSocket = new Socket(hostName, portNumber);
// echoSocket = ss.accept();
System.out.println("open");
System.out.println(echoSocket.isBound());

PrintWriter writer = new PrintWriter(echoSocket.getOutputStream());
for (int i = 0; i < 100; i++) {
writer.print("test String");
}

writer.flush();
// writer.close();

System.out.println("inputstream read");
DataInputStream is = new DataInputStream(echoSocket.getInputStream());
String fromStream = is.readLine();
System.out.println(fromStream);

System.out.println("bufferreader read");

BufferedReader reader = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
String fromReader = reader.readLine();

System.out.println(fromReader);

} catch (UnknownHostException ex1) {
// TODO Auto-generated catch block
System.out.println("EX1");
ex1.printStackTrace();
}

catch (IOException ex2) {
// TODO: handle exception
System.out.println("EX2");
ex2.printStackTrace();
}

finally {
try {
echoSocket.close();
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



}

}

编辑:更新了下面的代码...此代码中的唯一问题是 Server.Run 中的 while 循环永远不会结束。我寻找了一些其他属性(我记得类似 isTextAvailable 的东西)但找不到。代码背后的想法是将其转换为聊天客户端。不用说这是一场斗争!

编辑 2:我发现了问题所在。我从来没有从编写器端关闭套接字,所以监听器一直在听!感谢大家的帮助!

clientsocket.close();

加了一行就成功了!

import java.net.*;
import java.io.*;
import java.io.ObjectInputStream.GetField;
import java.util.*;

public class echoserver {



static echoserver echo;

public static class Client implements Runnable {

Socket clientsocket;
String hostName = "127.0.0.1";
int portNumber = 5000;
static int onesleep = 0;

public void run(){


System.out.println("Client Run " + new Date());

try {
clientsocket = new Socket(hostName,portNumber);
PrintWriter writer = new PrintWriter(clientsocket.getOutputStream());

for (int i = 0; i < 10; i++) {
writer.println("test String " + i );
}

writer.flush();
clientsocket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}
}

public class Server implements Runnable {

public void run(){

System.out.println("Server Run" + new Date());
int portNumber = 5000;
ServerSocket ss = null;
Socket serversocket = null;
InputStreamReader streamReader;

try {
ss = new ServerSocket(portNumber);
serversocket = ss.accept();

System.out.println("bufferreader read " + new Date());

streamReader = new InputStreamReader(serversocket.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String fromReader;

System.out.println(reader.ready());
System.out.println(reader.readLine());



while ((fromReader = reader.readLine()) != null) {
System.out.println(fromReader);

}

System.out.println("After While in Server Run");


} catch (IOException ex_server) {
// TODO Auto-generated catch block
System.out.println("Server Run Error " + new Date());
ex_server.printStackTrace();

}
finally {
try {
serversocket.close();
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
System.out.println("open" + new Date());
System.out.println(serversocket.isBound());


}
}


public void go(){

Server server = new Server();
Thread serverThread = new Thread(server);
serverThread.start();

Client client = new Client();
Thread clientThread = new Thread(client);
clientThread.start();

}

public static void main(String[] args) {
// TODO Auto-generated method stub

echo = new echoserver();
echo.go();





}

}

最佳答案

我之前准备了这篇文章的一个版本,但根据你在另一个答案中的最后评论,你似乎已经明白了。无论如何我都会发布这个,以防它有任何帮助。

广泛的答案是,您的类,正如您目前拥有的那样,有效地代表了同一线程/进程中的客户端和服务器端部分。如您所见,您可以将数据写入出站(或客户端)套接字,但服务器端组件永远没有机会监听传入连接。

因此,当您尝试从入站(或服务器端)套接字的输入流中读取数据时,没有任何内容存在,因为没有收到任何内容。 readline() 方法最终会阻塞,直到数据可用,这就是为什么您的程序似乎在那个时候保持不变。此外,如 haifzhan 所说,使用 new Socket(...) 创建新套接字不会建立连接,您所拥有的只是一个套接字,流中没有任何内容。

ServerSocket#accept方法你需要使用什么来监听连接。此方法将为您创建套接字,您可以尝试从中读取其流。正如 haifzhan 所说,该方法会阻塞,直到建立连接,这最终是它无法在单线程环境中正常运行的原因。

要在同一个应用程序中执行此操作,您只需将组件分开并在单独的线程中运行它们。尝试如下操作:

public class EchoClient {

public static void main(String[] args) throws InterruptedException {
new Thread(new EchoServer()).start(); // start up the server thread

String hostName = "localhost";
int portNumber = 5000;

try {
Socket outboundSocket = new Socket(hostName, portNumber);

System.out.println("Echo client is about to send data to the server...");

PrintWriter writer = new PrintWriter(outboundSocket.getOutputStream());
for (int i = 0; i < 100; i++) {
writer.print("test String");
}

System.out.println("Data has been sent");

writer.flush();
outboundSocket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

还有作为单独线程运行的服务器组件:

public class EchoServer implements Runnable {

public void run(){
try {
ServerSocket ss = new ServerSocket(5000);
System.out.println("Waiting for connection...");
Socket inboundSocket = ss.accept();

System.out.println("inputstream read");
DataInputStream is = new DataInputStream(inboundSocket.getInputStream());

BufferedReader reader = new BufferedReader(new InputStreamReader(is));

String fromStream = reader.readLine();
System.out.println(fromStream);

System.out.println("bufferreader read");
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

关于Java Simple EchoServer 无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24293502/

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