我还有另一个问题。

这是我的客户的一部分:

Socket socket = new Socket("127.0.0.1", 3000);
            OutputStream out = socket.getOutputStream();

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutput oo = null;
            try {
              oo = new ObjectOutputStream(bos);
              oo.writeObject(mp3dataStrings);
              byte[] serializedMP3 = bos.toByteArray();
              out.write(serializedMP3);
                out.flush();
            } finally {
              oo.close();
              bos.close();
            }

这是我服务器的一部分:
ServerSocket clientConnect = new ServerSocket(port);
        System.out.println("SimpleServer running on port" + port);
        Socket clientSock = clientConnect.accept();
        InputStream is = clientSock.getInputStream();

        byte[] buffer = new byte[1024];

        for (int i = 0; i < buffer.length; i++) {
          int b = is.read();
          buffer[i] = (byte) b;
          if (b == -1 | b == 0) break;
        }
        ObjectInputStream stream = new ObjectInputStream(new ByteArrayInputStream(buffer));
        String[][] songs = (String[][]) stream.readObject();
        stream.close();

当我发送对象(一个String [] [])时,我得到了异常无效的流头:ACED0000。

我找不到这意味着什么,我必须做什么。

打招呼
亚历克斯

最佳答案

您已经使它变得比所需的复杂得多。

Socket socket = new Socket("127.0.0.1", 3000);
try {
  ObjectOutputStream oo = new ObjectOutputStream(socket.getOutputStream());
  oo.writeObject(mp3dataStrings);
  oo.close();
} finally {
  socket.close();
}


ServerSocket clientConnect = new ServerSocket(port);
System.out.println("SimpleServer running on port" + port);

Socket clientSock = clientConnect.accept();
try {
  ObjectInputStream stream = new ObjectInputStream(clientSock.getInputStream());
  String[][] songs = (String[][]) stream.readObject();
} finally {
  clientSock.close();
}

关于java - 无效的流头-Java中的套接字传输,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14125631/

10-11 06:39