当前位置 : 主页 > 编程语言 > c++ >

javaSocket尝试链接端口

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt import java.io.IOException;import java.net.InetSocketAddress;import java.net.Socket;import java.net.SocketAddress;import java.net.UnknownHostException;import java.util.Scanner;public class Myself {public static void main(Strin
gistfile1.txt
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Myself {

	public static void main(String[] args) throws Exception{
		Scanner scan = new Scanner(System.in);
		String ip = scan.nextLine();
		scan.close();
		run(ip);
		
	}
	public static void run(String ip)throws Exception{
		try{
			Socket socket = new Socket(ip,56354);
			SocketAddress socketAddress = new InetSocketAddress(ip,56354);
			socket.connect(socketAddress,100);
			socket.setSoTimeout(1000);
			socket.close();
			System.out.println("ok");
		}catch(UnknownHostException e){
			System.out.println("no");
		}catch(IOException e){
			System.out.println("ok");
		}
	
	}	
}
网友评论