java - ObjectInputStream/ObjectOutputStream 工作不正常

标签 java sockets serialization objectinputstream objectoutputstream

我有类:客户端、服务器和后台正在使用 Player 类。 我真的不明白为什么我的带有 ObjectInputStream/ObjectOutputStream 的 Client 类不能正常工作。

我做错了什么?我的错误在哪里?

package Shooter2Dv27082013;
public class Player implements Serializable{
....
public int x=10;
public int y=10;
.... }

package Shooter2Dv27082013;
public class Background extends JPanel implements ActionListener, Serializable {
public int countCollisions=0;
private int time = 20;                       // 0.02s
Timer mainTimer = new Timer(time, this);
....
Player p = new Player(); ... }

现在是客户端类:

package Shooter2Dv27082013;


import javax.swing.*;
import java.net.*;
import java.io.*;

public class Client {
    public static void main(String[] ar) {
        JFrame frame = new JFrame("D2 Shooter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1000, 520);
        Background bg = new Background();
        frame.add(bg);
        frame.setResizable(false);
        frame.setVisible(true);

        int serverPort = 6666; 
        String address = "127.0.0.1"; /

        Player p = new Player();
        try {
            InetAddress ipAddress = InetAddress.getByName(address); 
            System.out.println("Any of you heard of a socket with IP address " + address + " and port " + serverPort + "?");
            Socket socket = new Socket(ipAddress, serverPort); 
            System.out.println("Yes! I just got hold of the program.");

            InputStream sin = socket.getInputStream();
            OutputStream sout = socket.getOutputStream();

            ObjectInputStream ois = new ObjectInputStream(sin);
            ObjectOutputStream oos = new ObjectOutputStream(sout);

            System.out.println("Streams are created. Let's try send these objects");
            System.out.println();


            System.out.println("P.x : "+bg.p.x);
            while (true) {
                oos.writeObject(bg.p);
                oos.flush();
                oos.close();
                System.out.println("Player X: " + bg.p.x + " Player Y: " + bg.p.y);
                p = (Player) ois.readObject();
                ois.close();
                System.out.println("New X: " + p.x + "New Y: "+p.y);
                System.out.println("Looks like the server is pleased with us. Go ahead and enter more lines.");
                System.out.println();
            }

        } catch (Exception x) {
            x.printStackTrace();
        }
    }
}

它不会将 Object 发送到 Server 类,但它也没有说明错误。

服务器类:

package Shooter2Dv27082013;

import java.net.*;
import java.io.*;
public class Server {
    public static void main(String[] ar)    {
        int port = 6666; 
        try {
            ServerSocket ss = new ServerSocket(port); 
            System.out.println("Waiting for a client...");

            Socket socket = ss.accept(); 
            System.out.println("Got a client :) ... Finally, someone saw me through all the cover!");
            System.out.println();

            InputStream sin = socket.getInputStream();
            OutputStream sout = socket.getOutputStream();

            ObjectInputStream ois = new ObjectInputStream(sin);
            ObjectOutputStream oos = new ObjectOutputStream(sout);

            Player p = new Player();

            while(true) {
                p = (Player) ois.readObject();
                System.out.println("The client just sent me this x: "+p.x+" y: "+p.y);
                p.x=555; p.y=600;
                System.out.println("I change it and now I'm sending it back...");
                oos.writeObject(p);
                oos.flush();
                oos.close();
                System.out.println("Waiting for the next line...");
                System.out.println();
            }
        } catch(Exception x) { x.printStackTrace(); }
    }
}

最佳答案

您需要在两端构造 ObjectInputStream 之前构造 ObjectOutputStream。目前你有一个僵局。

需要将闭包移到循环之外。

关于java - ObjectInputStream/ObjectOutputStream 工作不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18461894/

相关文章:

java - 变量类型(通用或非通用)更改属性行为

java - 如何在不使用数组的情况下向 ArrayList 添加多个项目

c++ - 使用 TCP 套接字(WIN32 API)创建的两个窗口之间的通信

java - 尝试运行 Java NIO SSL 的简单示例来加载 https ://www. amazon.com 的内容,但收到 400 Bad Request

java - 实现序列化时需要确定的关键点有哪些

java - 我需要搜索多个关键字

java - 为 Java 应用程序实现记录器

php - 如何监听UDP并使用PHP

c# - 序列化/反序列化 "NHibernate Session",延迟初始化错误 ("StateServer mode"集群)

java - jackson 将 map 序列化到列表