日期:2014-05-18  浏览次数:20755 次

客户端SOCKET不断发送报文,如何获得每次的信息,并依次处理????
下面是小弟的代码,但是我这个代码运行情况是客户端不断发,服务器端接收,等到客户端不再发送时,再处理收到的报文。
现在我希望能收到一笔报文就立即处理,而不是堆积起来处理。请高手指点下如何改,谢谢!!!
代码如下
public class SocketServer {
public SocketServer(){
  Socket incoming;
  ServerSocket so;
  try{
  so= new ServerSocket(8010);
  System.out.println("等待客户端连接");
   
  while(true){
  try{
  incoming = so.accept( );
  System.out.println("已连接客户端");
  GetInfo gi=new GetInfo(incoming);
  } catch (IOException e){
  so.close();
  e.printStackTrace();
  }
  }
  }catch (IOException e){
  e.printStackTrace();
  }
  }

  public static void main(String[] args){ 

  PropertyConfigurator.configure("E:\\Myeclipse\\workspace\\oufeitest\\src\\oufei\\test\\log4j.properties");
  new SocketServer();
  }
  private static class GetInfo implements Runnable{ //?????
  private Socket incoming;
  private String s=null;
  private BufferedReader b;
  Thread t;
   
  public GetInfo(Socket incoming){
  try{
  this.incoming=incoming;
  b=new BufferedReader(new InputStreamReader(incoming.getInputStream()));
   
  t=new Thread(this);
  t.start();
  }catch(IOException e){
  e.printStackTrace();
  }  
  }
   
  public void run(){
  try{
// while(true){
  Logger logger=Logger.getLogger(SocketServer.class);
  String lines;
  String line="";
  while ((lines = b.readLine()) != null){
  line+=lines;
  }
  System.out.println("line----"+line);
  System.out.println(line.length());
  List<String> list1 = new ArrayList<String>();
  for(int i=0;i<line.length();i+=63){
  list1.add(line.substring(i,i+63));
  }
  Iterator list=list1.iterator();
  while(list.hasNext()){
  String co="";
co=(String) list.next();
  logger.info("后台socket发送给我的内容:"+co);
  String post_url="http://esales1.ofcard.com:8088/onlineorder.do";
  String content=ConcatPackage.getPackage(co);
  //用post方式发送http请求
  String xmlcontent="";
  try {
  xmlcontent=Sender.readContentFromPost(post_url, content);
  } catch (IOException e) {
  // TODO Auto-generated catch block
  System.out.println("不好啦,发送http请求出问题啦,哦也!");
  e.printStackTrace();
  }
  System.out.println("查看取得的xml字符串是否为空:"+xmlcontent);