Book2-No.2 Socket用法详解
1、Socket构造方法
2、扫描某台主机开放了哪些端口号 正在使用:如果返回一个socket对象,则该端口有服务正在使用,否则返回IOException异常
?
package com.metarnet.NetworkProgram.Part02;import java.io.IOException;import java.net.InetAddress;import java.net.InetSocketAddress;import java.net.Socket;import java.net.SocketAddress;import java.net.UnknownHostException;public class PortScanner {public static void main(String[] args) {String host = "127.0.0.1";if(args.length > 0) {host = args[0];}new PortScanner().scan(host);//Socket socket = new Socket();//try {//InetAddress ip = InetAddress.getLocalHost();//try {//Socket s = new Socket(ip, 8080);//ip = InetAddress.getByName("127.0.0.1");//ip = InetAddress.getByName("http://www.hhstu.edu.cn");//} catch (IOException e) {//// TODO Auto-generated catch block//e.printStackTrace();//}//} catch (UnknownHostException e1) {//// TODO Auto-generated catch block//e1.printStackTrace();//}//SocketAddress endpoint = new InetSocketAddress("127.0.0.1", 8000);//try {//socket.connect(endpoint, 60000);//} catch (IOException e) {//// TODO Auto-generated catch block//e.printStackTrace();//}}private void scan(String host) {InetAddress ip = null;try {ip = InetAddress.getByName("www.hhstu.edu.cn");} catch (UnknownHostException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}Socket socket = null;for(int i=1; i<=1024; ++i) {try {socket = new Socket(host, i);System.out.println("connect to port " + i);} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {System.out.println("Cann't connect to port " + i);} finally {if(socket != null) {try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}socket = null;}}}}}
?
?3、设置建立等待的超时时间
?
try {Socket s = new Socket(); //建立一个不带参数的socketSocketAddress address = new InetSocketAddress("192.168.9.45", 8080); //设置远程IP为192.168.9.45,端口号为8080s.connect(address, 60000);// 用s建立连接,设置超时时间为60000,设置为0,永远不超时} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}
?
?4、InetAddress:代表远程IP,提供静态方法处理
?
try {InetAddress addr = InetAddress.getByName("www.baidu.com");System.out.println(addr.getHostAddress());addr = InetAddress.getLocalHost();System.out.println(addr.getHostAddress());addr = InetAddress.getByName("127.0.0.1");System.out.println(addr.getHostAddress());} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();}
?
?5、设置客户端地址
?
try {InetAddress inetAddr = InetAddress.getByName("192.168.9.24");InetAddress localAddr =InetAddress.getByName("192.168.9.238");Socket s = new Socket(inetAddr, 8000, localAddr, 2345);} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}
?
?6、socket连接异常
?
package com.metarnet.NetworkProgram.Part02;import java.io.IOException;import java.net.BindException;import java.net.ConnectException;import java.net.InetSocketAddress;import java.net.Socket;import java.net.SocketAddress;import java.net.SocketTimeoutException;import java.net.UnknownHostException;public class ConnectTest {public static void main(String[] args) {String host = "localhost";int port = 25;if(args.length > 1) {host = args[0];port = Integer.parseInt(args[1]);}new ConnectTest().connect(host, port);}private void connect(String host, int port) {SocketAddress address = new InetSocketAddress(host, port);Socket socket = null;try {long begin = System.currentTimeMillis();socket = new Socket();socket.connect(address, 1000);long end = System.currentTimeMillis();System.out.println((end-begin) + "ms");} catch(BindException e) {System.out.println("local address and port can't bind!");} catch(UnknownHostException e) {System.out.println("Unknown host");} catch(ConnectException e) {System.out.println("Connect resulted");} catch(SocketTimeoutException e) {System.out.println("socket timeout");} catch (IOException e) {System.out.println("faulite");} finally {if(socket != null) {try {socket.close();socket = null;} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}
?
?7、判断一个socket是否处于连接状态
?
boolean isconnected = socket.isConnected() && !socket.isClosed();
?8、
?