一:OSI模型
Open System Interconnection 开放系统互联
二:TCP/IP分层模型
TCP代表传输控制协议:Transfer Control Protocol 允许两个程序之前进行可靠的通讯
UDP代表用户报文协议:User Datagram Protocol 允许两个程序之前进行不可靠的通讯
三 客户端 和 服务器
四:套接字
定义:Socket(套接字)是一种抽象层,应用程序通过它来发送和接收数据
五:InetAddress类代表IP地址,没有构造器但是有多个方法,查询API文档。
六:TCP套接字编程
服务器:
如:ServerSocket server = new ServerSocket(8000);
如:Socket socket = server.accept()
如:BufferedReader br = new BufferedReader( new InputStreamReader(socket.getInputStream()));
如: br.close();
pw.close();
socket.close();
1 package inetAddress; 2 3 /** 4 * 服务器 5 */ 6 import java.io.BufferedReader; 7 import java.io.BufferedWriter; 8 import java.io.InputStreamReader; 9 import java.io.OutputStreamWriter; 10 import java.net.ServerSocket; 11 import java.net.Socket; 12 13 public class ServerTest { 14 15 public static void main(String[] args) throws Exception { 16 // 1创建ServerSocket对象指定端口号 17 ServerSocket server = new ServerSocket(30001); 18 server.setSoTimeout(10000); 19 // 2等待客户连接 accept 返回类型Socket 20 Socket socket = server.accept(); 21 // 3从客户端读数据 socket.getInputStream() 22 BufferedReader reader = new BufferedReader(new InputStreamReader( 23 socket.getInputStream())); 24 char[] ch = new char[100]; 25 int len = reader.read(ch); 26 System.out.println("从客户端接收到的消息:"); 27 System.out.println(new String(ch, 0, len)); 28 // 3向客户端写数据 socket.getOutputStream() 29 BufferedWriter write = new BufferedWriter(new OutputStreamWriter( 30 socket.getOutputStream())); 31 write.write("我们已经接收到消息了"); 32 write.flush(); 33 // 4 释放资源 34 reader.close(); 35 socket.close(); 36 write.close(); 37 38 } 39 40 }
客户端:
如:Socket socket = new Socket (“127.0.0.1”, 8000);
如:BufferedReader br = new BufferedReader( new InputStreamReader( socket.getInputStream()));
如: br.close();
pw.close();
socket.close();
1 package inetAddress; 2 3 /** 4 * 客户端 5 */ 6 import java.io.BufferedReader; 7 import java.io.BufferedWriter; 8 import java.io.InputStreamReader; 9 import java.io.OutputStreamWriter; 10 import java.net.Socket; 11 12 public class Cliebt { 13 14 public static void main(String[] args) throws Exception { 15 // 1 创建Socket对象 主机 端口 16 Socket socket = new Socket("127.0.0.1", 30001); 17 // 2向服务器写入数据 .getOutputStream() 18 BufferedWriter write = new BufferedWriter(new OutputStreamWriter( 19 socket.getOutputStream())); 20 write.write("你好服务器"); 21 write.flush(); 22 // 2读出数据 .getInputStream() 23 BufferedReader reader = new BufferedReader(new InputStreamReader( 24 socket.getInputStream())); 25 char[] ch = new char[100]; 26 int len = reader.read(ch); 27 System.out.println("数据"); 28 System.out.println(new String(ch, 0, len)); 29 // 3 释放资源 30 reader.close(); 31 write.close(); 32 socket.close(); 33 } 34 35 }
七:UDP套接字编程
1 package chap18UDP; 2 3 import java.io.IOException; 4 import java.net.DatagramPacket; 5 import java.net.DatagramSocket; 6 7 /** 8 * 发送 9 * 10 * @author acer 11 * 12 */ 13 public class Sender { 14 15 public static void main(String[] args) throws IOException { 16 // * 1创建DatagramSocket对象 17 DatagramSocket scoket = new DatagramSocket(3000); 18 // *2 创建DatagramPacket对象 19 byte[] buffer = "你好".getBytes(); 20 int port = 30001; 21 DatagramPacket packet = new DatagramPacket(buffer, buffer.length, 22 scoket.getInetAddress().getLocalHost(), port); 23 // 3 使用send 24 scoket.send(packet); 25 System.out.println("正在发送消息"); 26 // 释放资源 27 scoket.close(); 28 29 } 30 31 }
1 package chap18UDP; 2 3 import java.net.DatagramPacket; 4 import java.net.DatagramSocket; 5 6 /** 7 * 接收 8 * 9 * @author acer 10 * 11 */ 12 public class Reciver { 13 14 public static void main(String[] args) throws Exception { 15 // 创建字节数组 16 byte[] buffer = new byte[1024]; 17 // 2 创建DatagramPacket对象 18 DatagramPacket packet = new DatagramPacket(buffer, buffer.length); 19 // 3 创建DatagramSocket对象 20 DatagramSocket socket = new DatagramSocket(30001); 21 // 4 使用receive方法 22 socket.receive(packet); 23 System.out.println("正在接收从" + packet.getSocketAddress() + "的信息"); 24 System.out.println(new String(packet.getData())); 25 // 释放资源 26 socket.close(); 27 28 } 29 30 }