首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >腐蚀inputStream

腐蚀inputStream
EN

Stack Overflow用户
提问于 2014-01-25 21:41:18
回答 1查看 1K关注 0票数 2

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

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

我想问题在一开始可能是因为溪流没有被冲过,但是冲水并没有解决问题。

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

客户类-

代码语言:javascript
复制
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());

服务器类-

代码语言:javascript
复制
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();
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-25 22:46:04

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21356602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档