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

求帮忙改下代码,我希望实现同步SOCKET,望大家能帮忙改下
public class SocketServer {
public SocketServer(){
  Socket incoming;
  ServerSocket so;
  try{
  so= new ServerSocket(9731);
  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{
  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);
// String xmlc=xmlcontent.substring(0, 40);
// System.out.println(xmlc);
  System.out.println("发送欧非任务已完成&q