gpt4 book ai didi

java - read() 和 readObject() 的问题

转载 作者:行者123 更新时间:2023-12-01 06:13:12 25 4
gpt4 key购买 nike

我尝试开发一款通过蓝牙进行 1vs1 的纸牌游戏。我已经连接了设备,现在有一个问题:我想通过蓝牙发送对象。如果我只制作对象,它就可以工作,如果我只制作字符串,它就可以工作。但如果我尝试同时制作两者,就会遇到问题。

/**
* This thread runs during a connection with a remote device.
* It handles all incoming and outgoing transmissions.
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;

private final InputStream mmInStream;
private final OutputStream mmOutStream;

// for Objects
private final ObjectInputStream mObjectInStream;
private final ObjectOutputStream mObjectOutStream;

public ConnectedThread(BluetoothSocket socket) {
if (D) Log.d(TAG, "create ConnectedThread");
mmSocket = socket;

InputStream tmpIn = null;
OutputStream tmpOut = null;

ObjectInputStream tmpObjIn = null;
ObjectOutputStream tmpObjOut = null;

// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();

tmpObjOut = new ObjectOutputStream(socket.getOutputStream());
tmpObjOut.flush();
tmpObjIn = new ObjectInputStream(socket.getInputStream());

} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}


mmInStream = tmpIn;
mmOutStream = tmpOut;

mObjectOutStream = tmpObjOut;
mObjectInStream = tmpObjIn;
}

public void run() {
if (D) Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;


// Keep listening to the InputStream while connected
while (true) {

try {

// Read from the InputStream
bytes = mmInStream.read(buffer);

// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();

} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
// Start the service over to restart listening mode
BluetoothService.this.start();
break;
}

try {

// Send the obtained Object to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_READ_OBJECT, -1, -1, mObjectInStream.readObject())
.sendToTarget();

} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
// Start the service over to restart listening mode
BluetoothService.this.start();
break;
} catch (ClassNotFoundException cn) {
Log.e(TAG, "Class not found", cn);
}
}

}


/**
* Write to the connected OutStream.
*
* @param buffer The bytes to write
*/

public void writeString(byte[] buffer) {
try {

mmOutStream.write(buffer);

// Share the sent message back to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}

/**
* Write an Object (Serializable) to the connected OutStream.
*
* @param object The object to write
*/
public void writeObject(Object object) {
try {

mObjectOutStream.writeObject(object);

// Share the sent message back to the UI Activity

// TODO hier unterscheiden zwischen Player und UnoKarte?
mHandler.obtainMessage(Constants.MESSAGE_WRITE_OBJECT, -1, -1, object)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}


public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}

错误:

06-21 14:18:44.580  10941-11034/? E/BluetoothService﹕ disconnected
java.io.StreamCorruptedException: Wrong format: 0
at java.io.ObjectInputStream.corruptStream(ObjectInputStream.java:830)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:943)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2262)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2217)
at com.example.thm_wip1.uno.BluetoothService$ConnectedThread.run(BluetoothService.java:550)

第550行是:mHandler.obtainMessage(Constants.MESSAGE_READ_OBJECT, -1, -1, mObjectInStream.readObject())

<小时/>

While(true) 中,我有两个 try-catch,第一个我尝试读取字符串,第二个尝试读取我的 Object.如何在运行方法中区分字符串和对象?我是套接字新手,inputStreamoutputStream..

如果您需要更多详细信息,我将编辑我的问题。

最佳答案

当您在设备的内部一致性检查中出现 header 读取不一致时,会发生 StreamCorruptedException,在您的情况下,一致性检查失败,因为您尝试使用多个输出流和输入流,第一个在 tmpout 和 tmpin 中定义,然后您尝试再次从不同的输出流和输入流创建 ObjectOutputStreams 和 ObjectinputStreams。这些会导致 header 不一致。

应该解决此问题的正确代码如下

 try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();

tmpObjOut = new ObjectOutputStream(tmpOut);
tmpObjOut.flush();
tmpObjIn = new ObjectInputStream(tmpIn);

} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}

关于java - read() 和 readObject() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30964524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com