gpt4 book ai didi

java - 损坏的输入流

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

它在客户端代码的最后一行出错,带有代码。 java.io.StreamCorruptedException:无效的流 header :36353137。在此之前我没有对流执行任何操作,所以我不确定是什么导致了 ObjectInputStream 的问题。

服务器类工作正常,并遵循我为其设置的行为,只有客户端类出错。

起初我认为问题可能是因为流没有被刷新,但刷新并没有解决问题。

除此之外,由于此错误发生在代码的早期,我不知道该添加什么来修复它。

客户端类 -

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

public class tcpClient {

int nonce;
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
tcpClient(){}
void run()
{
try{

requestSocket = new Socket("localhost", 3223);
System.out.println("Connected to localhost in port 3223");

out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());

服务器类 -

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;
import java.util.Random;
import java.util.Scanner;
import javax.swing.Timer;

public class TCPMasterServer
implements ActionListener
{
ServerSocket server;
Socket client;
Random random;
Calendar rightNow;
Timer timer;
String ipaddress;
PrintWriter out;
BufferedReader ir;

public TCPMasterServer()
{
this.random = new Random();
this.rightNow = Calendar.getInstance();
try
{
this.server = new ServerSocket(3223);
}
catch (Exception e)
{
e.printStackTrace();
}
}

private void listenForConnection()
{
try
{
this.client = this.server.accept();
this.ipaddress = this.client.getInetAddress().getHostAddress();
System.out.println(this.rightNow.getTime() + ": Connected from: " + this.ipaddress);

this.out = new PrintWriter(this.client.getOutputStream(), true);
this.ir = new BufferedReader(new InputStreamReader(this.client.getInputStream()));

communicateWithClient();
}
catch (Exception e)
{
e.printStackTrace();
listenForConnection();
}
}

private void communicateWithClient()
{
this.timer = new Timer(2000, this);
this.timer.setRepeats(false);
this.timer.start();
try
{
int nonce = this.random.nextInt(1000000);
this.out.println(nonce);
System.out.println(this.rightNow.getTime() + ": Send nonce: " + nonce);


String input = this.ir.readLine();
Scanner in = new Scanner(input);
int nonceResponse = in.nextInt();

System.out.print(this.rightNow.getTime() + ": Received number: " + nonceResponse);
if (nonceResponse == nonce + 1)
{
System.out.println("... OK");


this.out.println("SEND_NAME");
System.out.println(this.rightNow.getTime() + ": Request name");

input = this.ir.readLine();
System.out.println(this.rightNow.getTime() + ": Received name: " + input);


this.out.println(input + " ACK");
System.out.println(this.rightNow.getTime() + ": ACK sent");
}
this.client.close();
}
catch (Exception ex)
{
System.out.println(this.rightNow.getTime() + ": Error happened. Giving up");
}
this.timer.stop();
System.out.println();
listenForConnection();
}

public void actionPerformed(ActionEvent evt)
{
try
{
System.out.println("Timer fired.");
this.client.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
TCPMasterServer server = new TCPMasterServer();
server.listenForConnection();
}
}

最佳答案

您在服务器端使用对象流,但在客户端使用读取器和写入器。那行不通的。如果要读取对象,则必须写入对象。

关于java - 损坏的输入流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21356602/

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