------------------------------------網絡編程(Socket)(TCP )
1.Java.net 包提供若幹支持基於套接字的客戶端/服務器通信的類。
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模型,如上圖;
4.Socket類
1)Socket對象在客戶端和服務器之間建立連接。構造方法創建套接字,並將套接字連接至給定的主機和端口。以下是與此Socket對象關聯的構造方法和一些常用方法。
(1)構造方法
第一種構造方法以主機名和端口號作為參數來創建一個Socket對象。創建Socket對象時可能拋出異常UnknowHostException或IOException異常,必須捕捉它們。
Socket s=new Socket(Hhost,port);
兩一種構造方法以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(); }
------------------------------------網絡編程(Socket)(TCP )