linux—将java应用程序绑定到接口

txu3uszq  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(302)

我有根用户权限访问带有2个接口的gnu/linux服务器。我用jericho和htmlunit来抓取一些网页。我想指定哪个接口应该用于http连接。有没有一种方法可以在命令行或java中实现这一点?

iyzzxitl

iyzzxitl1#

在java中,必须将套接字绑定到要使用的nic的本地ip地址。如果使用0.0.0.0,它将绑定到所有。它只使用你想要的。

olmpazwi

olmpazwi2#

有两种方法。
在java中,关键点是在调用httpconnection#open()方法之前指定要使用的源地址:
inetaddress favorateaddr=inetaddress(字节[]{1,2,3,4});//1.2.3.4是要使用的nic的ip地址
yourhttpconnobj.setlocaladdress(favorateAddr);
//然后打开http连接yourtpconnobj.open();
在linux中,由第3层utils决定谁将选择nic的路由。因此,您需要使用目标地址自定义路由表,以强制连接从哪个nic开始。它非常简单,只是一个路由命令。查看此链接。

mpbci0fu

mpbci0fu3#

我想指定哪个接口应该用于http连接。
每个网络接口都有自己唯一的ip地址。通过连接到接口ip地址上的端口,可以绑定到接口。
我没有插座。我正在给它一个网址。
是否使用插座(通过 Socket / InetAddress )或http连接(通过 URL ),使用相同的方法-绑定到ip地址+端口。
确定哪个接口有哪个地址
使用linux命令:

ip a
 ip link show
 /sbin/ifconfig -a
 lshw -class network

使用java( java.net.NetworkInterface 类别):

import java.io.*;
 import java.net.*;
 import java.util.*;
 import static java.lang.System.out;

 public class ListNIFs {
     public static void main(String args[]) throws SocketException {
         Enumeration<NetworkInterface> nets = 
             NetworkInterface.getNetworkInterfaces();

         for (NetworkInterface netIf : Collections.list(nets)) {
             out.printf("Display name: %s\n", netIf.getDisplayName());
             out.printf("Name: %s\n", netIf.getName());
             Enumeration<InetAddress> inetAddresses = netIf.getInetAddresses();
             for (InetAddress add : Collections.list(inetAddresses)) {
                 out.printf("Address: %s\n", add.getName().getAddress().getHostAddress());
             }
             displaySubInterfaces(netIf);
             out.printf("\n");
         }
     }

     static void displaySubInterfaces(NetworkInterface netIf) throws SocketException {
         Enumeration<NetworkInterface> subIfs = netIf.getSubInterfaces();            
         for (NetworkInterface subIf : Collections.list(subIfs)) {
             out.printf("\tSub Interface Display name: %s\n", subIf.getDisplayName());
             out.printf("\tSub Interface Name: %s\n", subIf.getName());
             Enumeration<InetAddress> inetAddresses = subIf.getInetAddresses();
             for (InetAddress add : Collections.list(inetAddresses)) {
                 out.printf("\t Sub Interface Address: %s\n", add.getName().getAddress().getHostAddress());
             }
         }
      }
 }

html单位:指定ip地址和端口
如果你在调用 HTMLUnitWebClient ,例如:

com.gargoylesoftware.htmlunit.WebClient webClient = new WebClient();
com.gargoylesoftware.htmlunit.html.HtmlPage page = 
    webClient.getPage("http://mycompany.org/mypage");

然后可以执行以下任一操作:

com.gargoylesoftware.htmlunit.html.HtmlPage page = 
    webClient.getPage("http://255.255.255.0:80/mypage");

java.net.URL url = new java.net.URL("http://255.255.255.0:80/mypage");
page = webClient.getPage(url);

com.gargoylesoftware.htmlunit.WebRequest req = 
    new com.gargoylesoftware.htmlunit.WebRequest(url, com.gargoylesoftware.htmlunit.HttpMethod.GET);
page = webClient.getPage(req);

你也可以使用
HTMLUnit HttpWebConnection :

com.gargoylesoftware.htmlunit.HttpWebConnection conn = 
    new com.gargoylesoftware.htmlunit.HttpWebConnection(webClient);
java.net.URL url = new java.net.URL("http://255.255.255.0/mypage");
com.gargoylesoftware.htmlunit.WebRequest req = 
    new com.gargoylesoftware.htmlunit.WebRequest(url, com.gargoylesoftware.htmlunit.HttpMethod.POST);
com.gargoylesoftware.htmlunit.WebRequest resp = conn.getResponse(req);

核心java:为http连接指定url的选项
创建一个连接到资源(文件、网页等)的url。您可以使用主机字符串(自动dns查找)或ip地址指定url。您可以选择添加端口:

// does not encode URL for you (manually include %20, etc)
    java.net.URL url = 
        new URL("http://255.255.255.20:80/docs/books/tutorial/index.html?name=networking#DOWNLOADING");
    java.net.URL url2 = new URL("http", "255.255.255.20", 80, 
                                "/docs/books/tutorial/index.html?name=networking#DOWNLOADING");
    java.net.URL url3 = new java.net.URL("https://maps.google.com/maps?q=sydney");
    java.net.URL url4 = new java.net.URL("https", "maps.google.com", 80, "/maps?q=sydney");

    // does encoding of URL string for you 
    java.net.URI uri5 = new URI("http", "example.com", "/hello world/", "");
    java.net.URL url5 = uri2.toURL();

    // Can then pass URL to HTMLUnit, or invoke yourself 
    // (see Java Network Tutorial for details):

    java.net.URLConnection conn = url.openConnection();
    java.net.HttpConnection httpConn = (HttpConnection)conn;
    // preconnection: call setAllowUserInteraction/setDoInput/setDoOutput/    
    //                     setIfModifiedSince/setUseCaches/setRequestProperty
    httpConn.connect();  // or getInputStream/getOutputStream/getContent/getHeaderField
    // postconnection: call getContentEncoding/getContentLength/getContentType
    //                      getDate/getExpiration/getLastModifed

核心java:指定inetaddress的选项(用于套接字连接)
使用ip地址

// 4 bytes:
    byte[] ip4Address = {101,119,11,106};  
    java.net.InetAddress addressObj = java.net.InetAddress.getByAddress(ip4Address); 

    // or use ip6Address, 16 bytes (2001:0db8:85a3:0000:0000:8a2e:0370:7334):
    // Any hex value above 0x7f must be cast to byte, because byte is signed (-128 to 127)
    byte[] ip6Address = {0x20,0x01,0x0d,(byte)0xb8,(byte)0x85,(byte)0xa3,0x00,0x00,0x00,0x00,(byte)0x8a,0x2e,0x03,0x70,0x73,0x34};

使用url字符串(dns查找)

java.net.InetAddress addressObj = java.net.InetAddress.getByName("www.google.com");

相关问题