一、Java支持基於流的通訊和基於包的通訊java
基於流的通訊使用TCP協議(傳輸控制協議)進行數據傳輸,傳輸是無損可靠的服務器
基於包的通訊使用UDP協議(用戶數據報協議)進行數據傳輸,不能保證傳輸沒有丟失socket
二、服務端和客戶端套接字--基於流的通訊ide
客戶端代碼
複製代碼
package com.wangluo;this
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;.net
public class Client {code
public static void main(String[] args) { try { //1.建立客戶端Socket,指定服務器端地址和端口 Socket socket = new Socket("localhost",8888); //2.獲取輸出流,向服務器端發送信息 OutputStream os = socket.getOutputStream();//字節輸出流 PrintWriter pw = new PrintWriter(os);//將輸出流包裝爲打印流 pw.write("用戶名:admin;密碼:123"); pw.write("用戶名:admin2;密碼:456"); pw.flush(); socket.shutdownOutput();//關閉輸出流,對於 TCP 套接字,任何之前寫入的數據都將被髮送 //3.獲取輸入流,並讀取服務器的響應信息 InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String info = null; while((info = br.readLine()) != null) { System.out.println("我是客戶端,服務器端說:"+info); } //4.關閉資源 br.close(); is.close(); pw.close(); os.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
}
複製代碼
若是服務端的端口服務沒有起來,運行客戶端的程序會拋異常server
java.net.ConnectException: Connection refused: connect
服務端代碼--服務多個客戶
複製代碼
package com.wangluo;對象
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;接口
public class Server {
public static void main(String[] args) { try { //1.建立一個服務器端Socket,即ServerSocket,指定綁定的端口 ServerSocket serverSocket = new ServerSocket(8888); //2.調用accept()方法開始監聽,等待客戶端 的鏈接 System.out.println("***服務器即將啓動,等待客戶端的鏈接***"); while (true) { //監聽端口 Socket socket = serverSocket.accept(); new MyThread(socket).start(); } } catch (IOException e) { e.printStackTrace(); } } static class MyThread extends Thread{ private Socket socket; public MyThread(Socket socket){ this.socket=socket; } @Override public void run() { try { server(socket); } catch (IOException e) { e.printStackTrace(); } } private void server(Socket socket) throws IOException { //3.獲取輸入流,並讀取客戶端信息 InputStream is = socket.getInputStream();//字節輸入流 InputStreamReader isr = new InputStreamReader(is);//將字節輸入流轉換爲字符流 BufferedReader br = new BufferedReader(isr);//爲輸入流添加緩衝 String info = null; while((info=br.readLine())!=null) {//循環讀取客戶端的信息 System.out.println("我是服務器,客戶端說:"+info); } OutputStream os = socket.getOutputStream();//字節輸出流 PrintWriter pw = new PrintWriter(os);//將輸出流包裝爲打印流 pw.write("我是服務端"); pw.flush(); socket.shutdownInput();//關閉輸入流 //4.關閉資源 br.close(); isr.close(); is.close(); socket.close(); } }
}
複製代碼
若是服務端監聽的端口已經被使用了,在啓動服務端的服務時候會拋異常
java.net.BindException
輸出結果:(先啓動服務端,再啓動客戶端)
服務端輸出:我是服務器,客戶端說:用戶名:admin;密碼:123用戶名:admin2;密碼:456
客戶端輸出:我是客戶端,服務器端說:我是服務端
三、套接字發送和接受對象
對象必須實現Serializable接口
複製代碼
package com.wangluo;
import java.io.Serializable;
public class Test implements Serializable{
private static long serialVersionUID=1L; private int num; private String name; public Test(int num, String name){ this.num=num; this.name=name; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Test{" + "num=" + num + ", name='" + name + '\'' + '}'; }
}
複製代碼
客戶端代碼
複製代碼
package com.wangluo;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) { try { //1.建立客戶端Socket,指定服務器端地址和端口 Socket socket = new Socket("localhost",8888); //2.獲取輸出流,向服務器端發送信息 OutputStream os = socket.getOutputStream();//字節輸出流
// PrintWriter pw = new PrintWriter(os);//將輸出流包裝爲打印流
// pw.write("用戶名:admin;密碼:123");
// pw.write("用戶名:admin2;密碼:456");
// pw.flush();
ObjectOutputStream objectOutputStream=new ObjectOutputStream(os);
objectOutputStream.writeObject(new Test(123,"deng"));
objectOutputStream.flush();
socket.shutdownOutput();//關閉輸出流
//3.獲取輸入流,並讀取服務器的響應信息
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String info = null;
while((info = br.readLine()) != null) {
System.out.println("我是客戶端,服務器端說:"+info);
}
//4.關閉資源
br.close();
is.close();
// pw.close();
objectOutputStream.close();
os.close();
socket.close();
} catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
}
複製代碼
服務端代碼
複製代碼
package com.wangluo;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) { try { //1.建立一個服務器端Socket,即ServerSocket,指定綁定的端口 ServerSocket serverSocket = new ServerSocket(8888); //2.調用accept()方法開始監聽,等待客戶端 的鏈接 System.out.println("***服務器即將啓動,等待客戶端的鏈接***"); while (true) { //監聽端口 Socket socket = serverSocket.accept(); new MyThread(socket).start(); } } catch (IOException e) { e.printStackTrace(); } } static class MyThread extends Thread{ private Socket socket; public MyThread(Socket socket){ this.socket=socket; } @Override public void run() { try { server(socket); } catch (Exception e) { e.printStackTrace(); } } private void server(Socket socket) throws Exception { //3.獲取輸入流,並讀取客戶端信息 InputStream is = socket.getInputStream();//字節輸入流
// InputStreamReader isr = new InputStreamReader(is);//將字節輸入流轉換爲字符流
// BufferedReader br = new BufferedReader(isr);//爲輸入流添加緩衝
// String info = null;
// while((info=br.readLine())!=null) {//循環讀取客戶端的信息
// System.out.println("我是服務器,客戶端說:"+info);
// }
ObjectInputStream objectInputStream=new ObjectInputStream(is); Object test=objectInputStream.readObject(); System.out.println("我是服務器,客戶端說:"+test); OutputStream os = socket.getOutputStream();//字節輸出流 PrintWriter pw = new PrintWriter(os);//將輸出流包裝爲打印流 pw.write("服務端應答"); pw.flush(); socket.shutdownInput();//關閉輸入流 //4.關閉資源
// br.close();// isr.close();objectInputStream.close();is.close();socket.close();