j2me中通过socket传输数据,发送的数据服务器端接受不到
yzbsd 2007-12-11 10:49:57 参照j2me中的networkdemo,发送代码如下:
import java.lang.Thread;
import java.io.OutputStream;
import java.io.IOException;
/**
*
* @author Administrator
*/
public class Sender extends Thread{
private OutputStream os;
private String message;
private boolean _bCtrl = true;
private byte[] _btSend;
/** Creates a new instance of Sender */
public Sender(OutputStream os) {
this.os = os;
start();
}
public synchronized void send(String msg) {
message = msg;
_bCtrl = false;
notify();
}
public synchronized void send(byte[] bsend) {
this._btSend = bsend;
_bCtrl = true;
notify();
}
public synchronized void run()
{
while (true)
{
// If no client to deal, wait until one connects
if (message == null && this._btSend == null)
{
try
{
wait();
}
catch (InterruptedException e)
{}
}
if (message == null && this._btSend == null)
{
break;
}
try
{
if(this._bCtrl)
{
if(this._btSend != null)
{
os.write(this._btSend);
this._btSend = null;
}
}
else
{
os.write(message.getBytes());
os.flush();
}
//os.write("\r\n".getBytes());
}
catch (IOException ioe)
{
System.out.println("发送错误" + ioe.getMessage());
}
message = null;
}
}
public synchronized void stop() {
message = null;
notify();
}
}
测试结果:在wtk中,发送数据在服务器端能够接受到,偶尔也有收不到的情况;但是在真机上,服务器一直收不到数据,但是手机客户端能收到。请各位指点迷津