本文將介紹Socket的建構方法與Socket的相關屬性設定與例外處理
#建構Socket
#設定等待逾時時間
##設定伺服器位址
設定客戶端位址
客戶端連線伺服器可能出現的例外
Socket();Socket(InetAddress address, int port)Socket(InetAddress address, int port, InetAddress localAddr, int localPort)Socket(Proxy proxy)Socket(SocketImpl impl)Socket(String host, int port) Socket(String host, int port, InetAddress localAddr, int localPort)
如下程式碼掃描主機上從1到1024之間的端口,判斷這些端口是否已經被伺服器程式監聽。
public class PortScanner { public static void main(String[] args) { String host = "localhost"; new PortScanner().scan(host); } public void scan(String host){ Socket socket = null; for(int i=0;i<1024;i++){ try { socket = new Socket(host, i); System.out.println("There is a server on port "+i); } catch (IOException e) { System.out.println("Can't connect to port "+i); }finally{ if(socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } } }
Socket socket = new Socket(); SocketAddress endPoint = new InetSocketAddress("localhost", 8000);socket.connect(endPoint, 60000);
SocketTimeoutException。
socket. connect(SocketAddress endpoint, int timeout);負責連接伺服器,參數endpoint指定伺服器位址,參數timeout設定逾時時間,以毫秒為單位。如果參數timeout為0,表示永遠不逾時。
Socket(InetAddress address, int port)
Socket(String host, int port)
InetAddress類別表示伺服器的IP位址,InetAddress提供了許多靜態方法:
// 返回本地主机的IP地址 InetAddress.getLocalHost(); // 返回代表10.202.164.65的IP地址 InetAddress.getByName("10.202.164.65"); // 返回域名为'www.csdn.net'的ip地址 InetAddress.getByName("www.csdn.net");
//参数localAddr和localPort用来设置客户端的IP和端口。 Socket(InetAddress address, int port, InetAddress localAddr, int localPort) Socket(String host, int port, InetAddress localAddr, int localPort)
• UnknownHostException:如果無法辨識主機的名字或IP位址,就會拋出這種例外
public class ConnectTester { public static void main(String[] args) { String host = "www.csdn.net"; int port = 12312; new ConnectTester().connect(host, port); } public void connect(String host,int port){ SocketAddress remoteAddress = new InetSocketAddress(host, port); Socket socket = null; String result = null; try{ socket = new Socket(); long start = System.currentTimeMillis(); socket.connect(remoteAddress,1000); long end = System.currentTimeMillis(); result = (end-start)+"ms"; }catch(BindException bindException){ result = "BindException,Socket对象与指定的本地IP地址或端口绑定,异常"; }catch (UnknownHostException unknownHostException) { result = "UnknownHostException,无法识别的主机"; }catch (ConnectException connectException) { result = "ConnectException,连接被拒绝"; }catch (SocketTimeoutException socketTimeoutException) { result = "SocketTimeoutException,连接超时"; }catch(IOException e){ result = "IOException,连接失败"; }finally{ if(socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println(remoteAddress+" : "+result); } }
如果無法辨識主機的名字或IP位址,就會拋出這個例外。例如:host為:’ somehost11’。 Socket的connect方法就會拋出UnknownHostException異常。
• 拋出ConnectException的情況:
在以下兩種情況會拋出ConnectException。
1) 沒有伺服器程序監聽指定的連接埠。例如:host為 ‘localhost’ port為 12321 。如果本機的12321埠沒有被任何進程監聽,則Socket連線方法會拋出ConnectException。
2) 伺服器程序拒絕連線。介紹伺服器程序拒絕客戶的連線請求的情形。如下範例程式碼,一個簡單的服務程式ServerSocket建構方法中的第二個參數表示請求佇列的長度。如果佇列的請求已滿,伺服器就會拒絕其餘的請求。拋出ConnectException
public class SimplServer { public static void main(String[] args) throws Exception{ ServerSocket serverSocket = new ServerSocket(8888, 2); Thread.sleep(3600000); } }
public class SimpleClient { public static void main(String[] args) throws Exception{ String host = "localhost"; int port = 8888; Socket s1 = new Socket(host, port); System.out.println("第一次连接成功"); Socket s2 = new Socket(host, port); System.out.println("第二次连接成功"); Socket s3 = new Socket(host, port); System.out.println("第三次连接成功"); } }
如果客戶端連線逾時,就會拋出這種例外。修改
socket.connect(remoteAddress, 1);由原來的1000毫秒修改為1毫秒,這樣增加了超時的可能性。
將程式碼
socket = new Socket();socket.connect(remoteAddress, 1000);
socket = new Socket(); socket.bind(new InetSocketAddress(InetAddress.getByName("222.34.5.6"), 5678)); socket.connect(remoteAddress, 1000);
#