gpt4 book ai didi

java - 客户端-服务器套接字

转载 作者:行者123 更新时间:2023-11-30 10:10:40 24 4
gpt4 key购买 nike

我试图在 java 中实现一个客户端服务器,我在其中读取客户端中的输入并在服务器中执行大写并返回到客户端并打印大写。我使用 ObjectOutputStream 和 ObjectInputStream 进行读取和写入,但是当我在客户端中键入消息时,我的程序会显示以下错误:

Digite uma msg casa java.io.EOFException at java.io.DataInputStream.readUnsignedShort(Unknown Source) at java.io.ObjectInputStream$BlockDataInputStream.readUnsignedShort(Unknown Source) at java.io.ObjectInputStream$BlockDataInputStream.readUTF(Unknown Source) at java.io.ObjectInputStream.readUTF(Unknown Source) at Client.java.Client.main(Client.java:32)

我的客户端是这个:

package Client.java;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {

public static void main(String[] args) {

//estabelece conexao
// trocar msg com o server

try {
//cria conexao entre client e servidor
Socket socket = new Socket("localhost",5558);

//cria input and output
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());

Scanner scan = new Scanner(System.in);
System.out.println("Digite uma msg");
String word = scan.nextLine();
output.writeUTF(word);
output.flush();
word = input.readUTF();
System.out.println("Upercase"+ word);

input.close();
output.close();
socket.close();

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

这是我的服务器端:

package Server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.ServerSocket;

public class Servidor {

private ServerSocket servidor;

private void criarServerSocket(int porta) throws IOException {
servidor = new ServerSocket(porta);
}

private Socket esperaConexao() throws IOException
{
Socket socket = servidor.accept();
return socket;
}

private void fechaSocket(Socket s) throws IOException {
s.close();
}

private void trataConexao(Socket socket) throws IOException {
//*recebe e envia msg */

try {
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());

String msg = input.readUTF();
output.writeObject(msg.toUpperCase());
output.flush();

input.close();
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}

fechaSocket(socket);
}

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

Servidor server = new Servidor();
System.out.println("Wait for a connection");
server.criarServerSocket(5558);
System.out.println("Conectado ");
Socket socket = server.esperaConexao();
server.trataConexao(socket);
}
}

是不是因为我的服务器正在关闭流?以及如何修复它并仍然关闭流?感谢您的帮助。

最佳答案

你应该在Servidor中使用

output.writeUTF(msg.toUpperCase());

而不是

output.writeObject(msg.toUpperCase());

关于java - 客户端-服务器套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52738306/

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