「java判断ip是否访问」java判断是否为ip地址

博主:adminadmin 2022-12-31 09:33:07 477

本篇文章给大家谈谈java判断ip是否访问,以及java判断是否为ip地址对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

使用java代码,访问某个ip地址的端口,看是否可以访问

import java.io.BufferedReader;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.InputStreamReader;

import java.net.Socket;

public class Client {

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

Socket clientSocket = new Socket("127.0.0.1", 8008);

clientSocket.close();

}

}

没有异常就是可以连接了。

如何JAVA编程判断局域网中的某个IP地址是否已经被使用?

import java.io.*;import java.net.*;import java.nio.*;import java.nio.channels.*;import java.nio.charset.*;import java.util.*;import java.util.regex.*;public class Ping { // The default daytime port static int DAYTIME_PORT = 13; // The port we'll actually use static int port = DAYTIME_PORT; // Representation of a ping target // static class Target { InetSocketAddress address; SocketChannel channel; Exception failure; long connectStart; long connectFinish = 0; boolean shown = false; Target(String host) { try { address = new InetSocketAddress(InetAddress.getByName(host), port); } catch (IOException x) { failure = x; } } void show() { String result; if (connectFinish != 0) result = Long.toString(connectFinish - connectStart) + "ms"; else if (failure != null) result = failure.toString(); else result = "Timed out"; System.out.println(address + " : " + result); shown = true; } } // Thread for printing targets as they're heard from // static class Printer extends Thread { LinkedList pending = new LinkedList(); Printer() { setName("Printer"); setDaemon(true); } void add(Target t) { synchronized (pending) { pending.add(t); pending.notify(); } } public void run() { try { for (;;) { Target t = null; synchronized (pending) { while (pending.size() == 0) pending.wait(); t = (Target)pending.removeFirst(); } t.show(); } } catch (InterruptedException x) { return; } } } // Thread for connecting to all targets in parallel via a single selector // static class Connector extends Thread { Selector sel; Printer printer; // List of pending targets. We use this list because if we try to // register a channel with the selector while the connector thread is // blocked in the selector then we will block. // LinkedList pending = new LinkedList(); Connector(Printer pr) throws IOException { printer = pr; sel = Selector.open(); setName("Connector"); } // Initiate a connection sequence to the given target and add the // target to the pending-target list // void add(Target t) { SocketChannel sc = null; try { // Open the channel, set it to non-blocking, initiate connect sc = SocketChannel.open(); sc.configureBlocking(false); sc.connect(t.address); // Record the time we started t.channel = sc; t.connectStart = System.currentTimeMillis(); // Add the new channel to the pending list synchronized (pending) { pending.add(t); } // Nudge the selector so that it will process the pending list sel.wakeup(); } catch (IOException x) { if (sc != null) { try { sc.close(); } catch (IOException xx) { } } t.failure = x; printer.add(t); } } // Process any targets in the pending list // void processPendingTargets() throws IOException { synchronized (pending) { while (pending.size() 0) { Target t = (Target)pending.removeFirst(); try { // Register the channel with the selector, indicating // interest in connection completion and attaching the // target object so that we can get the target back // after the key is added to the selector's // selected-key set t.channel.register(sel, SelectionKey.OP_CONNECT, t); } catch (IOException x) { // Something went wrong, so close the channel and // record the failure t.channel.close(); t.failure = x; printer.add(t); } } } } // Process keys that have become selected // void processSelectedKeys() throws IOException { for (Iterator i = sel.selectedKeys().iterator(); i.hasNext();) { // Retrieve the next key and remove it from the set SelectionKey sk = (SelectionKey)i.next(); i.remove(); // Retrieve the target and the channel Target t = (Target)sk.attachment(); SocketChannel sc = (SocketChannel)sk.channel(); // Attempt to complete the connection sequence try { if (sc.finishConnect()) { sk.cancel(); t.connectFinish = System.currentTimeMillis(); sc.close(); printer.add(t); } } catch (IOException x) { sc.close(); t.failure = x; printer.add(t); } } } volatile boolean shutdown = false; // Invoked by the main thread when it's time to shut down // void shutdown() { shutdown = true; sel.wakeup(); } // Connector loop // public void run() { for (;;) { try { int n = sel.select(); if (n 0) processSelectedKeys(); processPendingTargets(); if (shutdown) { sel.close(); return; } } catch (IOException x) { x.printStackTrace(); } } } } public static void main(String[] args) throws InterruptedException, IOException { if (args.length 1) { System.err.println("Usage: java Ping [port] host..."); return; } int firstArg = 0; // If the first argument is a string of digits then we take that // to be the port number to use if (Pattern.matches("[0-9]+", args[0])) { port = Integer.parseInt(args[0]); firstArg = 1; } // Create the threads and start them up Printer printer = new Printer(); printer.start(); Connector connector = new Connector(printer); connector.start(); // Create the targets and add them to the connector LinkedList targets = new LinkedList(); for (int i = firstArg; i args.length; i++) { Target t = new Target(args[i]); targets.add(t); connector.add(t); } // Wait for everything to finish Thread.sleep(2000); connector.shutdown(); connector.join(); // Print status of targets that have not yet been shown for (Iterator i = targets.iterator(); i.hasNext();) { Target t = (Target)i.next(); if (!t.shown) t.show(); } }}

昆明Java培训:如何验证IP地址的有效性

【实例描述】IP地址是网络上每台计算机的标识,在浏览器中输入的网址也是要经过DNS服务器转换为IP地址才能找到服务器的,在很多网络程序中要求输入服务器IP地址或者对方连接的IP地址,IP地址的错误输入将使程序无法运行。

本实例将实现对IP地址的验证功能,实例的运行效果如图4.14所示。

【实现过程】(1)在Eclipse中新建项目CheckIP,并在其中创建一个CheckIP.java文件。

在该类的主方法中创建标准输入流的扫描器对象,接收用户输入的IP地址。

核心代码如下所示:protectedvoiddo_button_actionPerformed(ActionEvente){Stringtext=ipField.getText();//获取用户输入Stringinfo=matches(text);//对输入文本进行IP验证showMessageDialog(null,info);//用对话框输出验证结果}(2)编写IP地址的mathches()方法,该方法利用正则表达式对输入的字符串进行验证,并返回验证结果,关键代码如下所示。

publicStringmatches(Stringtext){if(text!=null!text.isEmpty()){//定义正则表达式Stringregex="^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."+"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."+"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."+"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";//判断IP地址是否与正则表达式匹配if(text.matches(regex)){//返回判断信息returntext+"\n是一个合法的IP地址!";}else{//返回判断信息returntext+"\n不是一个合法的IP地址!";}}//返回判断信息return"请输入要验证的IP地址!";}【代码解析】本实例的关键点在于IP地址格式与数字范围的验证,用户在输入IP地址时,程序可以获取的只有字符串类型,所以本实例利用字符串的灵活性和正则表达式搭配进行IP地址格式与范围的验证。

该方法是String字符串类的方法,用于判断字符串与制定的正则表达式是否匹配。

其声明语法如下:publicbooleanmathches(Stringregex);Java程序设计经典300例XXXVIII【知识扩展】在正则表达式中,“.”代表任何一个字符,因此在正则表达式中如果想使用普通意义的点字符“.”,必须使用转义字符“\”。

关于java判断ip是否访问和java判断是否为ip地址的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。