它在客戶端代碼的最後一行中出現錯誤,並帶有代碼。 java.io.StreamCorruptedException:無效的流標題:36353137.在這之前,我還沒有對流做過任何事情,所以我不確定可能導致ObjectInputStream的問題。損壞的inputStream
服務器類正常工作,並遵循我爲它設置的行爲,它只是客戶端類錯誤。
我以爲這個問題起初可能是因爲流沒有被刷新,但是刷新並沒有解決問題。
除此之外,由於此錯誤在代碼的早期發生,因此我無法確定要添加哪些內容來修復它。
客戶端類 -
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();
}
}
發佈實際異常和堆棧跟蹤。 – EJP