两个java流程实例之间的差异

时间:2014-08-15 11:22:13

标签: java ant firewall

我的Windows计算机上有一个java TCP服务器TCPEchoServer,而远程Linux计算机上有一个客户端TCPClient

当我从命令行启动TCPEchoServer时,一切正常。但是当我从Ant启动它时,客户端无法与我的服务器建立连接。

当我关闭Windows防火墙时,在这两种情况下(命令行和Ant)一切正常。我没有通过ANT_OPTS或命令行指定任何代理设置。

当我在本地启动TCPClient时,它也会在两种情况下正确连接。

在Java配置面板中,我有“使用浏览器设置”选项。

我想知道如何检测从命令行和Ant启动的java进程实例之间的差异。

我打印出了env和java System属性。一切似乎都是一样的。我还可以打印出什么来看看差异?

这是java代码和Ant脚本。

服务器:

import java.net.*; 
import java.io.*; 
import java.util.*; 

public class TCPEchoServer extends Thread { 

    public static void main(String[] args) throws Exception { 


        for(String prop : System.getProperties().stringPropertyNames()) {
            System.out.println(prop + "=" + System.getProperties().getProperty(prop));
        }
        Map<String, String> env = System.getenv();
        for (String name : env.keySet()) {
            System.out.format("%s=%s%n", name, env.get(name));
        }

        ServerSocket serverSocket = null; 
        int port = -1;
        if(args.length < 1) {
            System.out.println("java TCPEchoServer port");
            System.exit(1);
        }
        port = new Integer(args[0]).intValue();

        serverSocket = new ServerSocket(port); 
        while(true) {
            final Socket socket = serverSocket.accept();
            new Thread() {
                public void run() {
                    try{
                        DataOutputStream out = new DataOutputStream(socket.getOutputStream()); 
                        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
                        String inputLine; 
                        while ((inputLine = in.readLine()) != null) { 
                            System.out.println("received:" + inputLine);
                            out.writeBytes(inputLine.toUpperCase() + "\n");
                        }
                    }
                    catch(Exception e) {
                        System.err.println("Exception:" + e.getMessage());
                    }
                }
            }.start(); 
        }

    }

} 

客户

import java.net.*; 
import java.io.*; 
import java.util.*; 

public class TCPClient { 

    public static void detectProxy(String host, int port) {
        System.out.println("detecting proxies");
        List proxyList = null;
        try {
            proxyList = ProxySelector.getDefault().select(new URI("socket://" + host + ":" + port));
        } 
        catch (URISyntaxException e) {
            System.err.println(e.getMessage());
        }
        if (proxyList != null) {
            for (Object p : proxyList) {
                java.net.Proxy proxy = (java.net.Proxy)p;
                InetSocketAddress addr = (InetSocketAddress) proxy.address();
                System.out.println(proxy.type() + ((addr == null)? "No Proxy": "host:" + addr.getHostName() + ", port" + addr.getPort()));
            }
        }
    }

    public static void main(String[] args) throws Exception { 

        ServerSocket serverSocket = null; 
        int port = -1;
        if(args.length < 2) {
            System.out.println("java TCPClient host port");
            System.exit(1);
        }
        port = new Integer(args[1]).intValue();
        detectProxy(args[0], port);

        Socket socket = new Socket(args[0], port);
        DataOutputStream out = new DataOutputStream(socket.getOutputStream()); 
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
        out.writeBytes("Hi there!\n");
        System.out.println("got back:" + in.readLine());
        socket.close();
    }

} 

蚂蚁脚本

<project name="server" default="run">
    <target name="run" >
        <java fork="true" spawn="false" classname="TCPEchoServer" >
            <arg value="4444" />
            <classpath>
                <pathelement location="." />
            </classpath>
        </java>
    </target>
</project>

1 个答案:

答案 0 :(得分:0)

最后,解决方案是为所有java平台二进制文件启用防火墙

enter image description here