Android-socket传输:客户端-udp方式改为tcp遇到的有关问题,求指教

Android-socket传输:客户端-udp方式改为tcp遇到的问题,求指教,在线等
先贴出udp发送和接收数据的主要代码:
接收:

public void run() {
byte[] buffer = new byte[10240];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
Log.e("UDPClientInputThread", "I'm in UDP input thread run() now");
try {
socket.setTrafficClass(0x04 | 0x10);//设置服务类型
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while (isStart) {
// 读取信息,如果没信息将会阻塞线程

// 每收到一条消息,就调用接口的方法Message(String msg)
socket.receive(packet);
receiveTime = System.nanoTime();
if (packet.getLength() > 8) {
sendTime = getLong(buffer, 0);
timesInPack = getLong(buffer, 8);
serverPacks = getLong(buffer, 16);
delayTimeMs = receiveTime - sendTime;
//delayArray[delayArrayIndex++] = delayTimeMs;
++recvPackTimes;
//Log.e("收到消息", "UDP len = " + packet.getLength() + ", delay = " + delayTime + " ms");
} else {
Log.e("收到消息", "UDP len = " + packet.getLength() + " 长度不对");
delayTimeMs = -1;
//++delayArrayIndex;
++recvPackTimes;
}/**/
/*Log.e("UDPClientInputThread", "I'm in UDPClientInputThread 3:"
+timesInPack+", "+serverPacks+", "+recvPackTimes+", ");*/
messageListener.Message(delayTimeMs, timesInPack, serverPacks, recvPackTimes);
}
if (socket != null) { // 循环结束后,关闭输出流和socket
socket.close();
socket = null;
}
} catch (IOException e) {
e.printStackTrace();
socket.close();
socket = null;
packet = null;
}
}
}

发送:

public void run() {
DatagramPacket packet;
try {
Log.e("UDPClientOutputThread", "I'm UDPClientOutputThread 4");
while (isStart) {
byte[] qbuf = sendQueue.take();
try {
Thread.sleep(periodMs);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Log.e("UDPClientOutputThread", "test of queue:"+qbuf.length);
sendTime = System.nanoTime();
putLong(qbuf, 0, sendTime);
putLong(qbuf, 8, ++testTimes);
putLong(qbuf, 8 + 8 + 8, deviceId);
//Log.e("UDPClientOutputThread", "test time:" + testTimes);
packet = new DatagramPacket(qbuf, qbuf.length, InetAddress.getByName(ip), remotePort);
socket.send(packet);
}
Log.e("UDPClientOutputThread", "I'm UDPClientOutputThread 5");
if (socket != null) { // 循环结束后,关闭输出流和socket
socket.close();
socket = null;
}
} catch (IOException e) {
e.printStackTrace();
socket.close();
socket = null;
packet = null;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


我自己改为tcp的代码(才接触这方面,还请各位多多指教,帮忙找错):
接收:

public void run() {
//String sbuf = null;
//DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
try {
socket.setTrafficClass(0x04 | 0x10);
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while (isStart) {
// 读取信息,如果没信息将会阻塞线程
byte[] buffer = new byte[10240];
int n = dis.read(buffer);
Log.e("ClientInputThread", "I'm in input thread run() now");
// 每收到一条消息,就调用接口的方法Message(String msg)
//socket.receive(packet);
receiveTime = System.nanoTime();
if (buffer.length > 8) {
sendTime = getLong(buffer, 0);
timesInPack = getLong(buffer, 8);
serverPacks = getLong(buffer, 16);
delayTimeMs = receiveTime - sendTime;
//delayArray[delayArrayIndex++] = delayTimeMs;
++recvPackTimes;
//Log.e("收到消息", "UDP len = " + packet.getLength() + ", delay = " + delayTime + " ms");
} else {
Log.e("收到消息", "UDP len = " + buffer.length + " 长度不对");
delayTimeMs = -1;
//++delayArrayIndex;
++recvPackTimes;
}
messageListener.Message(delayTimeMs, timesInPack, serverPacks, recvPackTimes);
}
if (socket != null) { // 循环结束后,关闭输出流和socket
socket.close();
socket = null;
}
} catch (IOException e) {
e.printStackTrace();
try {
socket.close();
socket = null;
dis.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}

发送:

public void run() {
DatagramPacket packet;

byte buffer[]=new byte[10240];
int temp=0;
try {
Log.e("ClientOutputThread", "I'm ClientOutputThread 4");
while (isStart) {
byte[] qbuf = sendQueue.take();
try {
Thread.sleep(periodMs);
} catch (InterruptedException e) {
e.printStackTrace();
}
sendTime = System.nanoTime();
putLong(qbuf, 0, sendTime);
putLong(qbuf, 8, ++testTimes);
putLong(qbuf, 8 + 8 + 8, deviceId);
//packet = new DatagramPacket(qbuf, qbuf.length, InetAddress.getByName(ip), remotePort);
//socket.send(packet);
//dos.write(packet);
InputStream inputstream=socket.getInputStream();
OutputStream outputstream=socket.getOutputStream();
while((temp=inputstream.read(buffer)) != -1){
outputstream.write(buffer,0,temp);
}
outputstream.flush();
synchronized (this) {
wait();// 发送完消息后,线程进入等待状态
Log.e("ClientOutputThread", "no wait");
}
}
Log.e("ClientOutputThread", "I'm ClientOutputThread 5");
dos.close();// 循环结束后,关闭输出流和socket
if (socket != null)
socket.close();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

祈祷高手帮忙,指教一下小白。谢谢!!(我自己改了程序之后tcp客户端就会停止运行)
------解决思路----------------------
temp=inputstream.read(buffer) ,这个read是阻塞的,没有数据就一直卡在那