输入流已损坏

时间:2014-01-25 21:41:19

标签: java

使用代码在客户端代码的最后一行出错。 java.io.StreamCorruptedException:无效的流标题: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();
  }
}

1 个答案:

答案 0 :(得分:2)

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