zoukankan      html  css  js  c++  java
  • Socket桥(转载)

    最好方案:使用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
  • 相关阅读:
    JAVA错误:Unable to find config file. Creating new servlet engine config file: /WEBINF/serverconfig.wsdd
    java axis发布web service(二) 发布web service
    JAVA错误:AXIS Web Service Problem: No compiler found in your classpath! (you may need to add ‘tools.jar’)
    JAVA错误:java.lang.UnsupportedClassVersionError: Bad version number in .class file
    JavaScript中的div和filter错误
    拉格朗日乘数法
    Timeout Detection & Recovery (TDR)
    游戏程序员关心的Autodesk Maya 2013相关操作
    Eclipse开发Android程序如何在手机上运行
    我的第一篇随笔
  • 原文地址:https://www.cnblogs.com/davidwang456/p/3237629.html
Copyright © 2011-2022 走看看