日期:2014-05-20  浏览次数:20813 次

各位大侠 帮帮我啊 困扰我一天了啊~~~~
我在学socket 通信,出现了问题不知道怎么解决啊:
代码:server :
//发送
public boolean send(byte[] in_data){
if(in_data!=null){
try {
bos.write(in_data,0,in_data.length);
//bos.flush();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//Logger._o(e.getMessage());
}
}
return false;
}

/**********************/
//接受
public boolean receive(){
try {
int failureCount=0;
while(isOne.frame_end != 1){
if(true){
//isOne.IsOneFrame((byte)bis.read());
System.out.print((byte)bis.read()+" ");
}
else{
failureCount++;
Thread.sleep(200);
}
if(failureCount <= MAX_READ_FAILURE_COUT)
continue;
else
return false;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//Logger._o(e.getMessage());
}
return false;
}
 

client:
package sdu.ie.lcms;
//客户端
//客户端的(多线程)
import java.io.*;
import java.net.*;
public class SocketClientTrue {

private BufferedInputStream bis=null;
private BufferedOutputStream bos=null;

public byte receiveCommand[]=new byte[20];
public byte[] sendResult=new byte[]{0x11,0x22,0x33,0x55};

 public SocketClientTrue()
 {
  //去连接服务器
  try {
  Socket s=new Socket("202.194.26.112",8090);
  //System.out.println("连接成功!");
   
  bis=new BufferedInputStream(s.getInputStream());
  bos=new BufferedOutputStream(s.getOutputStream());
   
  //接受来自上位机的消息
  int len=0;
  System.out.println("下位机接受到的:");
  while(bis.available()>0)
  { 
receiveCommand[len] = (byte)bis.read();
  }
  for (int m = 0;m < receiveCommand.length; m++){
System.out.print(receiveCommand[m]+" ");
  }
  System.out.println(); 
   
  //向上位机发送处理结果
  System.out.println("开始向上位机发送处理结果。。。");
  bos.write(sendResult,0,sendResult.length);
   
  } catch (UnknownHostException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub 
  SocketClientTrue scm=new SocketClientTrue();
 }
}; 
错误出现在 server的 :System.out.print((byte)bis.read()+" ");
错误信息:Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sdu.ie.lcms.SocketServer.receive(SocketServer.java:65)
at sdu.ie.lcms.SocketServer.main(SocketServer.java:101)
client 与server已经建立好了连接。。。
怎么解决啊、。。???是不是server发送完了以后 就断开连接了?

------解决方案--------------------
代码太多,太乱啦,