gpt4 book ai didi

java - 客户端/服务器应用程序

转载 作者:行者123 更新时间:2023-11-29 04:01:48 24 4
gpt4 key购买 nike

伙计们谁能告诉我我在这里做错了什么?下面是四个文件 Client, main, Server, main.尝试使用 fromServer.readLine() 后,客户端出现错误。
错误:客户端软件错误导致连接中止:接收失败

            package client;
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client
{
private PrintWriter toServer;
private BufferedReader fromServer;
private Socket socket;
public Client( )throws IOException
{
socket = new Socket("127.0.0.1",3000);
}
public void openStreams() throws IOException
{

//
// InputStream is = socket.getInputStream();
// OutputStream os = socket.getOutputStream();
// fromServer = new BufferedReader(new InputStreamReader(is));
// toServer = new PrintWriter(os, true);

toServer = new PrintWriter(socket.getOutputStream(),true);
fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public void closeStreams() throws IOException
{
fromServer.close();
toServer.close();
socket.close();
}
public void run()throws IOException
{
openStreams();
String msg = "";
Scanner scanner = new Scanner(System.in);
toServer.println("Hello from Client.");
// msg = scanner.nextLine();
while (msg != "exit")
{
System.out.println(">");
// msg = scanner.nextLine();
toServer.println("msg");
String tmp = fromServer.readLine();
System.out.println("Server said: " + tmp);
}
closeStreams();
}
}



package server;
import java.net.*;
import java.io.*;

public class Server
{
private ServerSocket serverSocket;
private Socket socket;
private PrintWriter toClient;
private BufferedReader fromClient;
public void run() throws IOException
{
System.out.println("Server is waiting for connections...");
while (true)
{
openStreams();
processClient();
closeStreams();
}

}
public void openStreams() throws IOException
{
serverSocket = new ServerSocket(3000);
socket = serverSocket.accept();
toClient = new PrintWriter(socket.getOutputStream(),true);
fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public void closeStreams() throws IOException
{
fromClient.close();
toClient.close();
socket.close();
serverSocket.close();
}
public void processClient()throws IOException
{
System.out.println("Connection established.");
String msg = fromClient.readLine();
toClient.println("Client said " + msg);
}
}


package client;

import java.io.IOException;


public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

try
{
Client client = new Client();
client.run();
}
catch(IOException e)
{
System.err.println("Error in Client " + e.getMessage());
}
}

}



package server;

import java.io.IOException;


public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Server server = new Server();
try
{
server.run();
}
catch(IOException e)
{
System.err.println("Error in Server " + e.getMessage());
}
}

}

最佳答案

你的服务器端都搞混了。您需要另一个实现 Runnable 的类,将其称为 Connection 或 ConnectedClient;将 Socket 作为构造函数参数;并为特定客户端执行所有服务器端 I/O。目前,您将这一切都与您的服务器类混在一起了。您的服务器类应该做的就是创建一个 ServerSocket,然后进入一个接受套接字并创建连接对象并为它们启动线程的循环。 Server 的 run() 方法中的 Socket 对象只是一个本地对象,关联的输入/输出流或读取器/写入器属于 Connection 类,而不是 Server 类。

这样您就可以同时处理多个客户,而不会互相干扰。

您的下一个问题是 readLine() 在另一端关闭连接时返回 null。每次调用 readLine() 时,您必须测试它,然后再进行任何其他处理。

接下来,任何时候在使用 Socket 时遇到 SocketException 或 IOException 而不是 SocketTimeoutException 时,它就死了 - 关闭它,忘记客户端并退出线程(如果它发生在服务器中)。

关于java - 客户端/服务器应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3003882/

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