最好方案:使用haproxy 或者nginx转发。自己写程序性能和监控难保证,推荐使用开源软件替代。

源地址为:http://baishaobin2003.blog.163.com/blog/static/57381812201332355422107/

Socket在使用过程中往往会出现这样的问题
在生产的机器有一个服务,但是测试环境不能连接生产
中间有一台公共机可以连接两台机器,
这种情况下需要用到一个Socket桥
测试机器发送数据到公共机上,然后从公共机上发送数据到生产机器,由生产机返回数据到公共机,在有公共机转发回来测试机上来

=================================================================================
       AGClientBase.java
=================================================================================
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class AGClientBase {
 
 Socket socket = null;

 InputStream is = null;

 OutputStream os = null;

 String tCode = null;

 public String testClient(String data) {
  try {
   // 建立连接
   socket = new Socket(getValueByKey.readValue("IpAddrss"), Integer
     .valueOf(getValueByKey.readValue("Port")).intValue());
   // 发送数据
   os = socket.getOutputStream();
   os.write(data.getBytes());
   // 接收数据
   is = socket.getInputStream();
   byte[] b = new byte[4000];
   is.read(b);

   return new String(b).trim();
  } catch (Exception e) {
   System.out.println("连接异常,请检查配置文件或对方Socket连接");
   return null;
  } finally {
   try {
    // 关闭流和连接
    is.close();
    os.close();
    socket.close();
   } catch (Exception e2) {
   }
  }
 }

}
======================================================================================================
       LinkServer.java
======================================================================================================
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream;
import java.io.InputStreamReader; 
import java.io.OutputStream;
import java.io.PrintWriter; 
import java.net.ServerSocket; 
import java.net.Socket; 
 
public class LinkServer { 
    public static void main(String[] args) throws IOException { 
        ServerSocket server = new ServerSocket(10000); 
         
        while (true) { 
            Socket socket = server.accept();
            invoke(socket); 
        } 
    } 
     
    private static void invoke(final Socket socket) throws IOException { 
        new Thread(new Runnable() { 
            public void run() { 
             AGClientBase ag = new AGClientBase();
                BufferedReader in = null; 
                PrintWriter out = null; 
                try { 
                  InputStream inputStream = socket.getInputStream();
                     OutputStream outputStream = socket.getOutputStream();
                     byte[] arry = new byte[4000];
                     while(true){
                      inputStream.read(arry);
                      outputStream.write(ag.testClient(new String(arry)).getBytes());
                      outputStream.flush();
                      socket.close(); 
                    } 
                } catch(IOException ex) { 
                   // ex.printStackTrace(); 
                } finally { 
                    try { 
                        in.close(); 
                    } catch (Exception e) {} 
                    try { 
                        out.close(); 
                    } catch (Exception e) {} 
                    try { 
                        socket.close(); 
                    } catch (Exception e) {} 
                } 
            } 
        }).start(); 
    } 
} 
======================================================================================================
       LinkService.java
======================================================================================================
public interface LinkService {
 public String reMsg();
 public String reMsg(String repmsg);
}

======================================================================================================
                                                                                 getValueByKey
======================================================================================================

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
 * @author wyyw
 * 读取配置文件
 * 根据配置文件中的内容获取要连接的地址
 *
 * */
public class getValueByKey {
 
  private static String filename= "linkconfig.propertes";
  /**
   * @author wyyw
   * @param key 要查询的内容key
   * @return 根据key查询出来的value
   *
   * */
     public static String readValue( String key){
      
         Properties props = new Properties();
         FileInputStream in=null;
         try{
         in = new FileInputStream(filename);
         props.load(in);
         String value = props.getProperty(key);
         return value;
         }catch(Exception e){
             System.out.println("读取配置文件失败,请检查配置文件 linkconfig.propertes 格式如下");
          System.out.println("IpAddrss=*.*.*.*");
          System.out.println("Port=*");
             return null;
         }finally{
            try {
             in.close();
            } catch (IOException e) {
            System.out.println("读取配置文件失败,请检查配置文件 linkconfig.propertes 格式如下");
           System.out.println("IpAddrss=*.*.*.*");
           System.out.println("Port=*");
            }
         }
     }
}
======================================================================================================
       linkconfig.propertes 和项目SRC文件夹同级
======================================================================================================
IpAddrss=127.0.0.1

Port=6500

 

posted on 2013-08-05 10:12  一天不进步,就是退步  阅读(349)  评论(0编辑  收藏  举报