1.單向通信實現
傳輸示意圖
客戶端程序

1 import java.io.DataInputStream; 2 import java.io.DataOutputStream; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.net.Socket; 7 8 public class clinet { 9 10 public static void main(String[] args) throws IOException { 11 //創建Socket對象 12 Socket clinet = new Socket("127.0.0.1",9999); 13 //獲取輸出流 14 DataOutputStream dos = new DataOutputStream(clinet.getOutputStream()); 15 dos.writeUTF("hello word!"); 16 //獲取輸入流 17 DataInputStream dis = new DataInputStream(clinet.getInputStream()); 18 System.out.println(dis.readUTF()); 19 //關閉流 20 if(dis != null) { 21 dis.close(); 22 } 23 if(dos != null) { 24 dos.close(); 25 } 26 if(clinet != null) { 27 clinet.close(); 28 } 29 } 30 }
服務器程序

1 import java.io.DataInputStream; 2 import java.io.DataOutputStream; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.net.ServerSocket; 7 import java.net.Socket; 8 9 public class Server { 10 11 public static void main(String[] args) throws IOException { 12 System.out.println("-----------服務器端已啟動----------"); 13 //創建ServerSocket對象 14 ServerSocket server = new ServerSocket(9999); 15 //監聽是否有客戶端請求連接 16 Socket clinet = server.accept(); 17 //獲取輸入流 18 DataInputStream dis = new DataInputStream(clinet.getInputStream()); 19 System.out.println(dis.readUTF()); 20 //獲取輸出流 21 DataOutputStream dos = new DataOutputStream(clinet.getOutputStream()); 22 dos.writeUTF("收到了"); 23 //關閉流,關閉Socket 24 if(dos != null) { 25 dos.close(); 26 } 27 if(dis != null) { 28 dis.close(); 29 } 30 clinet.close(); 31 } 32 33 }
先啟動服務器,再啟動客戶端
服務器:
客戶端:
2.模擬用戶登陸
服務器

1 import java.io.DataOutputStream; 2 import java.io.IOException; 3 import java.io.ObjectInputStream; 4 import java.net.ServerSocket; 5 import java.net.Socket; 6 7 import javax.swing.plaf.synth.SynthSeparatorUI; 8 9 import org.omg.PortableInterceptor.DISCARDING; 10 11 public class Server2 { 12 13 public static void main(String[] args) throws IOException, ClassNotFoundException { 14 System.out.println("------服務器端已啟動----------"); 15 //創建ServerSocket對象 16 ServerSocket server = new ServerSocket(10000); 17 Socket socket = server.accept(); 18 //獲取輸入流(對象流) 19 ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); 20 //對用戶名和密碼進行驗證 21 User user = (User)ois.readObject(); 22 System.out.println(socket.getInetAddress().getHostAddress()+"請求登陸:用戶名"+user.getUsername()+"\t密碼:"+user.getPassword()); 23 String str = ""; 24 if("chb".equals(user.getUsername())&&"123456".equals(user.getPassword())) { 25 str = "登陸成功"; 26 }else { 27 str = "登陸失敗"; 28 } 29 //獲取輸出流(數據流) 30 DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); 31 dos.writeUTF(str); 32 //關閉流 33 if(dos != null) { 34 dos.close(); 35 } 36 if(ois != null) { 37 ois.close(); 38 } 39 socket.close(); 40 } 41 42 }
客戶端

1 import java.io.DataInputStream; 2 import java.io.IOException; 3 import java.io.ObjectOutputStream; 4 import java.net.Socket; 5 import java.util.Scanner; 6 7 public class Clint2 { 8 9 public static void main(String[] args) throws IOException { 10 //(1)創建Socket對象,用於連接服務器 11 Socket clint = new Socket("localhost", 10000); 12 //(2)獲取輸出流(對象流) 13 ObjectOutputStream oos = new ObjectOutputStream(clint.getOutputStream()); 14 //(3)創建User對象 15 User user = getUser(); 16 //(4)User對象發生到服務器 17 oos.writeObject(user); 18 //(5)獲取輸入流(數據流) 19 DataInputStream dis = new DataInputStream(clint.getInputStream()); 20 System.out.println(dis.readUTF()); 21 if(dis != null) { 22 dis.close(); 23 } 24 if(oos != null) { 25 oos.close(); 26 } 27 clint.close(); 28 } 29 public static User getUser() { 30 Scanner input = new Scanner(System.in); 31 System.out.println("請輸入用戶名:"); 32 String username = input.next(); 33 System.out.println("請輸入密碼:"); 34 String password = input.next(); 35 //封裝成User對象 36 return new User(username, password); 37 } 38 }
3.UDP 通信_DatagramSocket 實現_客戶咨詢
1) 不需要利用 IO 流實現數據的傳輸
2) 每個數據發送單元被統一封裝成數據包的方式,發送方將數據包發送到網絡中,數據包在網絡中去尋找他的目的地。
3) DatagramSocket:用於發送或接收數據包
4) DatagramPacket:數據包
1 import java.io.IOException; 2 import java.net.DatagramPacket; 3 import java.net.DatagramSocket; 4 5 public class Test1 { 6 //接收方 7 /** 一收, 8 * 一發 9 * @throws IOException 10 */ 11 public static void main(String[] args) throws IOException { 12 System.out.println("客服人員"); 13 DatagramSocket ds=new DatagramSocket(9999); 14 //准 備接收數據 15 byte [] buf=new byte[1024]; 16 //准 備數據報接收 17 DatagramPacket dp=new DatagramPacket(buf, buf.length); 18 19 //接收 20 ds.receive(dp); 21 22 //查看接收到的數據 23 String str=new String(dp.getData(),0,dp.getLength()); 24 System.out.println("客戶說:"+str); 25 26 27 /**回復數據*/ 28 byte [] buf2="welcome to beijing".getBytes();//發生的內容 長度 地址 端口號 29 DatagramPacket dp2=new DatagramPacket(buf2, buf2.length, dp.getAddress(), dp.getPort()); 30 ds.send(dp2); 31 //關閉 32 ds.close(); 33 34 35 } 36 }
1 import java.io.IOException; 2 import java.net.DatagramPacket; 3 import java.net.DatagramSocket; 4 import java.util.Scanner; 5 6 public class Test2 { 7 // 8 /** 一收, 9 * 一發 10 * @throws IOException 11 */ 12 public static void main(String[] args) throws IOException { 13 Scanner input=new Scanner(System.in); 14 System.out.println("客服人員"); 15 DatagramSocket ds=new DatagramSocket(9999); 16 while(true){ 17 //准 備接收數據 18 byte [] buf=new byte[1024]; 19 //准 備數據報接收 20 DatagramPacket dp=new DatagramPacket(buf, buf.length); 21 22 //接收 23 ds.receive(dp); 24 25 //查看接收到的數據 26 String str=new String(dp.getData(),0,dp.getLength()); 27 System.out.println("客戶說:"+str); 28 29 String s=input.next(); 30 /**回復數據*/ 31 byte [] buf2=s.getBytes(); 32 DatagramPacket dp2=new DatagramPacket(buf2, buf2.length, dp.getAddress(), dp.getPort()); 33 ds.send(dp2); 34 if("bye".equals(s)){ 35 break; 36 } 37 } 38 //關閉 39 40 ds.close(); 41 42 43 } 44 }