1.Java.net 包提供若干支持基於套接字的客戶端/服務器通訊的類。java
2.java.net包中常有的類有Socket、ServerSocket、DatagramPacket、InetAddress、URL、URLConnection和URLEncoder等編程
3.爲了監聽客戶端的鏈接請求,可使用ServerSocket類。Socket類實現用於網絡上進程間通訊的套接字.DatagramSocket類使用UDP協議實現客戶端和服務器套接字。DatagramPacket類使用DatagramSocket類的對象封裝設置和收到的收據情報。InetAddress類表示Internet地址。在建立數據報報文和Socket對象時,可使用InetAddress類。服務器
,Socket通訊模式圖網絡
3.2個斷點在TCP協議的Socket編程中,常常一個做爲客戶端,一個做爲服務器端。也就是client-server模型,如上圖;socket
4.Socket類this
1)Socket對象在客戶端和服務器之間創建鏈接。構造方法建立套接字,並將套接字鏈接至給定的主機和端口。如下是與此Socket對象關聯的構造方法和一些經常使用方法。spa
(1)構造方法.net
第一種構造方法以主機名和端口號做爲參數來建立一個Socket對象。建立Socket對象時可能拋出異常UnknowHostException或IOException異常,必須捕捉它們。線程
Socket s=new Socket(Hhost,port);code
兩一種構造方法以InetAddress對象和端口做爲參數來建立一個Socket對象。建立Socket對象時可能拋出異常UnknowHostException或IOException異常,必須捕捉它們。
Socket s=new Socket(address,port);
(2)經常使用方法
InetAddress getInetAddress();//返回與Socket對象關聯的InetAddress
int getPort();//返回此Socket 對象所鏈接的遠程端口
int getLocalPort();//返回此Socket對象所鏈接的本地端口
InputStream getInputStream();//返回與此套接關聯的InputStream
OutputStream getOutputStream();//返回於此套接關聯的OutputStream
void close();//關閉該Socket
5.ServerSocket類
ServerSocket對象等待客戶創建鏈接,鏈接創建之後進行通訊。
1)構造方法
第一種構造方法接收端口號做爲參數建立ServerSocket對象。建立此對象時,可能拋出IOExption異常,必須捕捉和處理。
ServerSocket ss=new ServerSocket(port);
另外一種構造方法 接收端口號和最大隊列長度做爲參數。隊列長度表示系統在拒絕鏈接前能夠擁有的最大客戶端數。
ServerSocket ss=new ServerSocket(port,maxqu);
Socket類中列出的方法也適用於ServerSocket類。此外,ServerSocket類具備的方法有accept(),此方法用於等待客戶端觸發通訊,這樣Socket對象就可用於進一步的數據傳送。
6.實現單用戶登陸
Socket 網絡編程通常能夠劃分以下4個步驟進行:
(1)創建鏈接
(2)打開Socket關聯的輸入/輸出流
(3)從數據流中寫入信息和讀取信息
(4)關閉全部的數據流和Socket
.
/** * 客戶端 * @param args * @throws IOException * @throws UnknownHostException */ public static void main(String[] args) throws UnknownHostException, IOException { //創建客戶端Socket鏈接,指定服務器的位置爲本機當即端口8800 Socket socket=new Socket("localhost",8800); OutputStream os=socket.getOutputStream(); InputStream is = socket.getInputStream(); String str="用戶名:tom,用戶密碼:123456"; os.write(str.getBytes()); socket.shutdownOutput(); //接收服務器端的響應,即從輸入流讀取信息 String reply=null; BufferedReader br=new BufferedReader(new InputStreamReader(is)); while ((reply=br.readLine())!=null){ System.out.println("我是客戶端,服務器的響應爲:"+reply); } br.close(); is.close(); os.close(); }
/** * @param args * 服務端 * @throws IOException */ public static void main(String[] args) throws IOException { //創建一個服務器Socket(ServerSocket),指定端口8800bin開始監聽 ServerSocket serverSocket=new ServerSocket(8800); //使用accept()方法獲得Socket對象 Socket socket = serverSocket.accept(); InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String info=null; while ((info=br.readLine())!=null) { System.out.println("我是服務器,客戶的信息是:"+info); } String reply="歡迎你,登陸成功!"; os.write(reply.getBytes()); os.close(); br.close(); is.close();
2)把對象傳遞,建一個Student類裏面有三個屬性和set/get方法
/** * 客戶端 * @param args * @throws IOException * @throws UnknownHostException */ public static void main(String[] args) throws UnknownHostException, IOException { Socket socket=new Socket("localhost",8800); OutputStream os = socket.getOutputStream(); InputStream is = socket.getInputStream(); //序列化對象 ObjectOutputStream oos=new ObjectOutputStream(os); Student student=new Student(); student.setID(0); student.setName("小明"); student.setSEX("男"); oos.writeObject(student); socket.shutdownOutput(); //接收服務器端的響應,即從輸入流讀取信息 BufferedReader br=new BufferedReader(new InputStreamReader(is)); String info=null; while ((info=br.readLine())!=null) { System.out.println("我是客戶端,服務器的信息是:"+info); } br.close(); oos.close(); is.close(); os.close(); }
/** * @param args * 服務端 * @throws IOException * @throws ClassNotFoundException */ public static void main(String[] args) throws IOException, ClassNotFoundException { //創建一個服務器Socket(ServerSocket),指定端口8800bin開始監聽 ServerSocket serverSocket=new ServerSocket(8800); //使用accept()方法獲得Socket對象 Socket socket = serverSocket.accept(); InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); ObjectInputStream ois=new ObjectInputStream(is); Student student =(Student) ois.readObject(); if (student!=null) { System.out.println("我是服務器端,客戶端的信息是"+student.getID()+student .getName()+student.getSEX()); } String str="歡迎使用!"; os.write(str.getBytes()); os.close(); ois.close(); is.close(); } }
3)實現多個客戶端用戶登陸
/** * 客戶端 * @param args * @throws IOException * @throws UnknownHostException */ public static void main(String[] args) throws UnknownHostException, IOException { Socket socket=new Socket("localhost",8800); OutputStream os = socket.getOutputStream(); InputStream is = socket.getInputStream(); //序列化對象g ObjectOutputStream oos=new ObjectOutputStream(os); Student student2=new Student(); student2.setID(2); student2.setName("小明"); student2.setSEX("男"); oos.writeObject(student2); socket.shutdownOutput(); //接收服務器端的響應,即從輸入流讀取信息 BufferedReader br=new BufferedReader(new InputStreamReader(is)); String info=null; while ((info=br.readLine())!=null) { System.out.println("我是客戶端,服務器的信息是:"+info); } br.close(); oos.close(); is.close(); os.close(); }
//創建多個服務器Socket(ServerSocket),指定端口8800bin開始監聽 ServerSocket serverSocket=new ServerSocket(8800);
public class LoginThread extends Thread{ Socket socket=null; //每啓動一個線程,鏈接響應的Socket public LoginThread(Socket socket){ this.socket=socket; } //啓動線程,即響應客戶請求 public void run(){ try { InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); ObjectInputStream ois=new ObjectInputStream(is); Student student; try { student = (Student) ois.readObject(); if (student!=null) { System.out.println("我是服務器端,客戶端的信息是"+student.getID()+student .getName()+student.getSEX()); } String str="歡迎使用!"; os.write(str.getBytes()); os.close(); ois.close(); is.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } }
/** * 客戶端 * @param args * @throws IOException * @throws UnknownHostException */ public static void main(String[] args) throws UnknownHostException, IOException { Socket socket=new Socket("localhost",8800); OutputStream os = socket.getOutputStream(); InputStream is = socket.getInputStream(); //序列化對象 ObjectOutputStream oos=new ObjectOutputStream(os); Student student=new Student(); student.setID(0); student.setName("小明"); student.setSEX("男"); oos.writeObject(student); socket.shutdownOutput(); //接收服務器端的響應,即從輸入流讀取信息 BufferedReader br=new BufferedReader(new InputStreamReader(is)); String info=null; while ((info=br.readLine())!=null) { System.out.println("我是客戶端,服務器的信息是:"+info); } br.close(); oos.close(); is.close(); os.close(); }
Socket socket=null; while (true) { socket=serverSocket.accept(); LoginThread loginThread=new LoginThread(socket); loginThread.start(); }