套接字使用TCP創建兩臺計算機之間的通訊機制,客戶端程序建立一個套接字嘗試鏈接服務器的套接字。當創建鏈接時,服務器建立Socket對象,隨後,客戶端和服務器能夠經過Socket對象的寫入和讀取來進行通訊。Java.net.Socket類表明一個套接字,而且Java.net.ServerSocket類爲服務器提供了一種來監聽客戶端bingyu2他們創建鏈接的機制java
鏈接創建後,經過I/O流通訊,每個Socket都有一個輸入輸出流,客戶端的輸出流鏈接到服務器端的輸入流,客戶端的輸入流鏈接到服務器端的輸出流;TCP是雙向的傳輸控制協議,數據能夠經過兩個數據流在同一時間發送。編程
java.net.Socket類是服務器端和客戶端用來溝通的套接字數組
public Socket(String host, int port) //建立流套接字並鏈接到指定主機上的指定端口號 public Socket(InetAddress host, int port)//建立流套接字並鏈接到指定 IP 地址的指定端口號 public Socket()//經過系統默認類型的 SocketImpl 建立未鏈接套接字 //遠程傳遞四個參數
當Socket構造方法返回,會嘗試鏈接到指定服務器和端口,由於客戶端和服務器端Socket對象,能夠調用socket方法:服務器
public InetAddress getInetAddress()//返回套接字鏈接的地址 public InputStream getInputStream() //返回此套接字的輸入流 public OutputStream getOutputStream()//返回此套接字的輸出流 public SocketAddress getRemoteSocketAddress()//返回此套接字鏈接遠程的端點的地址
public ServerSocket()//建立非綁定服務器套接字 public ServerSocket(int port) //建立綁定到特定端口的服務器套接字 ublic ServerSocket(int port, int backlog)// 利用指定的 backlog監聽建立服務器套接字並將其綁定到指定的本地端口號
調用ServerSocket構造方法-應用程序已經成功綁定指定端口,偵聽客戶端請求socket
public Socket accept()//偵聽並接受到此套接字的鏈接 public void bind(SocketAddress host, int backlog)// ServerSocket 綁定到特定地址(IP+端口) public int getLocalPort()//返回此套接字在其上偵聽的端口
String getHostName() //獲取IP地址主機名 static InetAddress getByAddress(byte[] addr)//在給定原始 IP 地址的狀況下,返回 InetAddress 對象 static InetAddress getByName(String host)//在給定主機名的狀況下肯定主機的 IP 地址
package com.day13; import com.sun.security.ntlm.Server; import java.io.*; import java.net.Socket; /** * @author SFJ * @date 2019/11/19 * @time 21:29 **/ public class Test1 { public static void main(String[] args) {//args數組傳遞參數 String serverName = args[0];//第一個參數 int port = Integer.parseInt(args[1]);//轉成整型數據 System.out.println("ServerName:"+serverName+" Port:"+port); try { Socket client = new Socket(serverName,port); System.out.println("RemoteHostAddress:"+client.getRemoteSocketAddress()); OutputStream outputStreamToServer = client.getOutputStream();//返回此套接字輸出流 DataOutputStream outputStream = new DataOutputStream(outputStreamToServer); outputStream.writeUTF("hello comes from"+client.getLocalSocketAddress()); InputStream inputStreamFromServer = client.getInputStream(); DataInputStream inputStream = new DataInputStream(inputStreamFromServer); System.out.println("Server response:"+inputStream.readUTF()); client.close(); } catch (IOException e) { e.printStackTrace(); } } }
package com.day13; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; /** * @author SFJ * @date 2019/11/19 * @time 21:55 **/ public class Test2 { public static void main(String[] args) { int port = Integer.parseInt(args[0]); try { Thread thread = new Server(port); thread.run(); } catch (IOException e) { e.printStackTrace(); } } } class Server extends Thread{ private ServerSocket serverSocket; public Server(int port) throws IOException { serverSocket = new ServerSocket(port); serverSocket.setSoTimeout(22222); } public void run() { for (;;) { System.out.println("Waiting for connection-Port:"+serverSocket.getLocalPort()); try { Socket server = serverSocket.accept(); System.out.println("RemoteSocketAddress: "+server.getRemoteSocketAddress()); DataInputStream inputStream = new DataInputStream(server.getInputStream()); System.out.println(inputStream.readUTF()); DataOutputStream outputStream = new DataOutputStream(server.getOutputStream()); outputStream.writeUTF("Thanks for connecting me "+server.getLocalSocketAddress()+"\nbye"); server.close(); } catch (IOException e) { e.printStackTrace(); System.out.println("timed out"); } } } }