Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用套接字交换对象_Java_Sockets_Serialization - Fatal编程技术网

Java 用套接字交换对象

Java 用套接字交换对象,java,sockets,serialization,Java,Sockets,Serialization,我正在尝试使用套接字在Java中编写客户端和服务器之间的简单交互 服务器 客户 你能看出什么不对劲吗?问题是,如果我尝试交换多个对象,我会得到 java.io.StreamCorruptedException:无效类型代码:00 编辑-解决 最后,我设法解决了这个问题:正如Pshemo所怀疑的,问题不在这段代码中,而是在另一个类中,在这个类中,有另一个对receive方法的调用,这显然导致了奇怪的行为。现在我必须重新考虑一下结构,但至少套接字通信看起来不错。在服务器类中,您试图从客户端获取输入和

我正在尝试使用套接字在Java中编写客户端和服务器之间的简单交互

服务器 客户 你能看出什么不对劲吗?问题是,如果我尝试交换多个对象,我会得到

java.io.StreamCorruptedException:无效类型代码:00

编辑-解决
最后,我设法解决了这个问题:正如Pshemo所怀疑的,问题不在这段代码中,而是在另一个类中,在这个类中,有另一个对receive方法的调用,这显然导致了奇怪的行为。现在我必须重新考虑一下结构,但至少套接字通信看起来不错。

在服务器类中,您试图从客户端获取输入和输出,但在客户端类中,您将tour client socket设置为套接字而不是客户端。您试图通过类而不是套接字获取信息,所以我猜这是您的问题。如果我错了,请纠正我

public class MyServer{

private static final int SPORT = 4444;



/**
* Constructor
*/
public MyServer() {
try
{
  ServerSocket server = new ServerSocket(SPORT);
}
catch (IOException e)
{
  e.printStackTrace();
}

listenAndReply();
}


public void listenAndReply(){

System.out.println("Server running.");
System.out.println("Waiting for connection...");
try
{
  socket = server.accept();

  System.out.println("Connection accepted. \n");

  InputStream is = new InputStream(socket.getInputStream()); //changed from client.getInputStream() because in your client class you have setup your client socket to be named socket instead of client.
  OutputStream os = new OutputStream(socket.getOutputStream()); //changed from client.getOutputStream() since your client class has a differently named client socket, your server can’t get information from it by calling client.get

  while( !socket.isClosed()) {

    Object o = is.readObject();

    // ... process object ...

  }
}
catch (IOException | ClassNotFoundException e)
{
  e.printStackTrace();
}

}

操作系统重置()的目的是什么在
sendCommand
?它应该重置流,使其看起来像一个新打开的流,正如我在另一个问题中读到的。不管怎样,忽略它不会改变结果。我不能重现你的错误。你能创建并发布(简短但完整的代码再现你的问题)吗?实际上这只是一个更大项目的一小部分。我以为我只是在对象流上犯了一些愚蠢的错误,所以我试着删掉所有其他内容,只发布了与对象交换相关的代码。是的,我意识到创建SSCCE有很多好处:(1)它可以帮助你缩小问题范围(2)使回答更容易(3)如果未来的读者面临类似的问题,它可以帮助他们更容易地测试。因此,我建议无论如何都要创建它,因为第一点(我在创建SSCCE时经常发现代码中存在问题,所以确实值得花大量时间来创建它)。。。但在客户端类中,您将tour客户端套接字设置为套接字,而不是客户端的mean?谢谢在客户端类中,您将客户端套接字设置为socket socket=new socket,但在服务器类中,您的输入和输出流来自client.get。。看见您试图从您的客户端中没有实例化的套接字名称中提取是的,只是输入错误。。。无论如何,我仍然不明白这一点:在服务器类中,客户机被声明为套接字对象,而在客户机类中,我尝试通过名为“Socket”的套接字对象连接到服务器。我是否遗漏了您的答案?确切地说,您的服务器正在查找client.getinput和output,而它应该查找socket.getinput/output,因为这是设置clientsocket的方式,您的clientsocket不是服务器类的成员,所以应该如何查找它?套接字的名称是“client”,这是我用来获取流的名称。无论如何,我试过了,正如你所说,但我显然得到了“套接字无法解析”。
public class MyClient implements ChatClient
{
  private static final int SPORT    = 4444;
  private static final String SHOST = "127.0.0.1";
  Socket socket;
  ObjectInputStream is = null;
  ObjectOutputStream os = null;


  public MyClient() {
    try
    {
      socket = new Socket(SHOST, SPORT);
      os = new ObjectOutputStream(socket.getOutputStream());
      is = new ObjectInputStream(socket.getInputStream());
      System.out.println("Client socket created.");
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }


  @Override
  public Command receive()
  {
    try
    {
      Object o = is.readObject();

      if(o instanceof Command) {
        System.out.println("Received command: " + o.getClass().getSimpleName());
        return (Command) o;
      }
    }
    catch (IOException | ClassNotFoundException e)
    {
      e.printStackTrace();
    }

    return null;
  }


public void sendCommand(Command c) {
    System.out.println("Client sending command " + c.getClass().getSimpleName());

    try
    {
      os.reset();
      os.writeObject(c);
      os.flush();
      System.out.println("Command sent. \n");
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }

}
public class MyServer{

private static final int SPORT = 4444;



/**
* Constructor
*/
public MyServer() {
try
{
  ServerSocket server = new ServerSocket(SPORT);
}
catch (IOException e)
{
  e.printStackTrace();
}

listenAndReply();
}


public void listenAndReply(){

System.out.println("Server running.");
System.out.println("Waiting for connection...");
try
{
  socket = server.accept();

  System.out.println("Connection accepted. \n");

  InputStream is = new InputStream(socket.getInputStream()); //changed from client.getInputStream() because in your client class you have setup your client socket to be named socket instead of client.
  OutputStream os = new OutputStream(socket.getOutputStream()); //changed from client.getOutputStream() since your client class has a differently named client socket, your server can’t get information from it by calling client.get

  while( !socket.isClosed()) {

    Object o = is.readObject();

    // ... process object ...

  }
}
catch (IOException | ClassNotFoundException e)
{
  e.printStackTrace();
}