1 UPD协议发送和接收
1.1 基础版
package d13; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /* * 创建stock对象 * 打包数据 * 发送数据 * 释放数据 * */ public class UDP_Send { public static void main(String[] args) throws Exception { //01 创建对象 DatagramSocket ds = new DatagramSocket(); //02 打包数据 DatagramPacket(byte[] buf, int length, InetAddress address, int port) byte [] contentStr = "Hello Word !!".getBytes(); int lengthInt = contentStr.length; InetAddress ip = InetAddress.getByName("192.168.2.117"); int portInt = 10086; DatagramPacket dp = new DatagramPacket(contentStr,lengthInt,ip,portInt); //03 发送数据 ds.send(dp); //04 释放资源 ds.close(); } }
package d13; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /* * 创建stock对象 * 创建数据包对象 * 接收数据 * 解析数据 * 释放数据 * */ public class UDP_Receive { public static void main(String[] args) throws Exception { //01 创建对象 int portInt = 10086; DatagramSocket ds = new DatagramSocket(portInt); //02 打包数据 DatagramPacket(byte[] buf, int length) byte[] buf = new byte[1024]; int lengthInt = 1024; DatagramPacket dp = new DatagramPacket(buf,lengthInt); //03 发送数据 ds.receive(dp); //04 解析数据 String ip = dp.getAddress().getHostAddress(); byte[] contentByte = dp.getData(); int len = dp.getLength(); String strContent = new String(contentByte,0,len); System.out.println(ip+": "+strContent); //05 释放资源 ds.close(); } }
1.2 改进版,键盘录入
package d13; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /* * 创建stock对象 * 打包数据 * 发送数据 * 释放数据 * */ public class UDP_Send { public static void main(String[] args) throws Exception { //01 创建对象 DatagramSocket ds = new DatagramSocket(); //02 打包数据 DatagramPacket(byte[] buf, int length, InetAddress address, int port) BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; while ((line = br.readLine()) != null) { if ("886".equals(line)) { break; } byte [] contentStr =line.getBytes(); int lengthInt = contentStr.length; InetAddress ip = InetAddress.getByName("192.168.2.117"); int portInt = 10086; DatagramPacket dp = new DatagramPacket(contentStr,lengthInt,ip,portInt); //03 发送数据 ds.send(dp); } //04 释放资源 ds.close(); } }
2 TCP协议发送和接收数据
2.1 基础版
package d13; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; /*TCP协议发送数据: * A:创建发送端的Stock * B:获取输出流,写数据 * C:释放资源 * */ public class TCP_Client { public static void main(String[] args) throws Exception { // A:创建发送端的Stock Socket s = new Socket("192.168.2.117",10087); //B:获取输出流,写数据 OutputStream os = s.getOutputStream(); os.write("Hello Word !!".getBytes()); //释放资源 s.close(); } }
package d13; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; /* *创建服务端的Socket对象 *监听客户端连接,返回一个Socket对象 *获取输入流 并显示在控制台 *释放资源 * */ public class TCP_Server { public static void main(String[] args) throws Exception { //创建服务端的Socket对象 ServerSocket s = new ServerSocket(10087); //监听客户端连接,返回一个Socket对象 Socket ss = s.accept(); //获取输入流 并显示在控制台 InputStream is = ss.getInputStream(); byte [] bys = new byte[1024]; int len = is.read(bys); String str = new String(bys,0,len); String ip = ss.getInetAddress().getHostAddress(); System.out.println(ip+": "+str); //释放资源 ss.close(); } }
2.2 服务器端收到消息后反馈给客户端
package d13; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; /*TCP协议发送数据: * A:创建发送端的Stock * B:获取输出流,写数据 * C:释放资源 * */ public class TCP_Client { public static void main(String[] args) throws Exception { // A:创建发送端的Stock Socket s = new Socket("192.168.2.117",10087); //B:获取输出流,写数据 OutputStream os = s.getOutputStream(); os.write("Hello Word !!".getBytes()); //C 接收服务器端发送来的数据 InputStream is = s.getInputStream(); byte [] by = new byte[1024]; int len = is.read(by); String ms=new String(by,0,len); System.out.println(ms); //释放资源 s.close(); } }
package d13; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; /* *创建服务端的Socket对象 *监听客户端连接,返回一个Socket对象 *获取输入流 并显示在控制台 *释放资源 * */ public class TCP_Server { public static void main(String[] args) throws Exception { //创建服务端的Socket对象 ServerSocket s = new ServerSocket(10087); //监听客户端连接,返回一个Socket对象 Socket ss = s.accept(); //获取输入流 并显示在控制台 InputStream is = ss.getInputStream(); byte [] bys = new byte[1024]; int len = is.read(bys); String str = new String(bys,0,len); String ip = ss.getInetAddress().getHostAddress(); System.out.println(ip+": "+str); //创建输出流,将消息反馈给客户端 OutputStream os = ss.getOutputStream(); os.write("信息已收到!!".getBytes()); //释放资源 ss.close(); } }
2.3 键盘录入+高效流
package d13; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.UnknownHostException; /*TCP协议发送数据: * A:创建发送端的Stock * B:获取输出流,写数据 * C:释放资源 * */ public class TCP_Client { public static void main(String[] args) throws Exception { // A:创建发送端的Stock Socket s = new Socket("192.168.2.117",10087); //B:获取输出流,写数据 OutputStream os = s.getOutputStream(); //键盘录入数据 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //把通道内的流给包装一下 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String line =null; while ((line=br.readLine())!=null) { if ("886".equals(line)) { break; } bw.write(line); bw.newLine(); bw.flush(); } //释放资源 s.close(); } }
package d13; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; /* *创建服务端的Socket对象 *监听客户端连接,返回一个Socket对象 *获取输入流 并显示在控制台 *释放资源 * */ public class TCP_Server { public static void main(String[] args) throws Exception { //创建服务端的Socket对象 ServerSocket ss = new ServerSocket(10087); //监听客户端连接,返回一个Socket对象 Socket s = ss.accept(); //获取输入流 并显示在控制台 BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); String line = null; while ((line=br.readLine())!=null) { //String ip = ss.getInetAddress().getHostAddress(); System.out.println(line); } //释放资源 s.close(); } }
2.4 键盘录入+输出到文本文件
package d13; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; /* * 客户端键盘录入,服务器输出到文本文件*/ public class TCP_Client2 { public static void main(String[] args) throws IOException { //创建客户端Socket对象 Socket s = new Socket("192.168.2.117",23466); //键盘录入 BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); //封装通道内的数据 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String line =null; while ((line=br.readLine())!=null) { if ("over".equals(line)) { break; } bw.write(line); bw.newLine(); bw.flush(); } s.close(); } }
package d13; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; /* * 客户端键盘录入,服务器输出到文本文件*/ public class TCP_Server2 { public static void main(String[] args) throws IOException { //创建服务器Socket对象 ServerSocket ss = new ServerSocket(23466); //监听客户端创建 Socket s = ss.accept(); //从通道中来到文件中去 BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); //封装文本文件 BufferedWriter bw = new BufferedWriter (new FileWriter("b.txt")); String line = null; while ((line=br.readLine())!=null) { bw.write(line); bw.newLine(); bw.flush(); } bw.close(); } }
2.5 文本文件录入+控制台输出
package d13; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.UnknownHostException; /* * 读取文本文件,输出到控制台*/ public class TCP_Client3 { public static void main(String[] args) throws IOException { //创建Socket对象 Socket s = new Socket("192.168.2.117",10087); //封装文本文件 BufferedReader br = new BufferedReader(new FileReader("b.txt")); //封装通道内的流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String line = null; while ((line = br.readLine())!=null) { bw.write(line); bw.newLine(); bw.flush(); } br.close(); s.close(); } }
package d13; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class TCP_Server3 { public static void main(String[] args) throws IOException { //创建服务器对象 ServerSocket ss = new ServerSocket(10087); //监听客户端 Socket s = ss.accept(); //从通道中来 BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); //到控制台 String line = null; while ((line=br.readLine())!=null) { System.out.println(line); } s.close(); } }
2.6 文本文件录入+文本文件输出
package d13; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.UnknownHostException; public class TCP_Client4 { public static void main(String[] args) throws IOException { // 创建Socket对象 Socket s = new Socket("192.168.2.117", 10088); // 创建读取流 BufferedReader br = new BufferedReader(new FileReader("b.txt")); // 创建文件输出流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } //Socket通知服务器,已经结束了 bw.write("over"); s.shutdownOutput(); // 接收反馈 BufferedReader brclient = new BufferedReader(new InputStreamReader(s.getInputStream())); String msg = brclient.readLine(); System.out.println(msg); // 释放资源 br.close(); s.close(); } }
package d13; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; public class TCP_Server4 { public static void main(String[] args) throws IOException { //创建服务器对象 ServerSocket ss = new ServerSocket(10088); //创建监听 Socket s = ss.accept(); //从通道中来 BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); //到文件中 BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt")); String line = null; while ((line=br.readLine())!=null) { bw.write(line); bw.newLine(); bw.flush(); } //发出成功消息 BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); bwServer.write("文件上传成功"); bwServer.newLine(); bwServer.flush(); //bwServer.close(); //释放资源 bw.close(); s.close(); } }
2.7 图片文件录入+图片文件输出
package d13; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; //上传图片,并保存 public class TCP_Client5 { public static void main(String[] args) throws IOException { //创建Socket对象 Socket s = new Socket("192.168.2.117", 10090); //读取图片图片 BufferedInputStream bi = new BufferedInputStream(new FileInputStream("a.png")); byte [] by = new byte[1024]; //向通道中写数据 BufferedOutputStream bo = new BufferedOutputStream(s.getOutputStream()); int len =0; while ((len=bi.read(by))>0) { bo.write(by,0,len); bo.flush(); } //通知服务器传输结束 s.shutdownOutput(); //结束服务器传来的反馈 InputStream biServer = s.getInputStream(); len= biServer.read(by); String msg =new String(by,0,len); System.out.println(msg); s.close(); } }
package d13; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class TCP_Server5 { public static void main(String[] args) throws IOException { // 创建服务端Socket ServerSocket ss = new ServerSocket(10090); // 监听客户端 Socket s = ss.accept(); // 从通道中来 BufferedInputStream bi = new BufferedInputStream(s.getInputStream()); // 到文件中去 byte [] by = new byte[1024]; BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream("b.png")); int len =0; while ((len=bi.read(by))!=-1) { bo.write(by,0,len); } //给客户端个反馈 OutputStream bs = s.getOutputStream(); bs.write("图片上传成功!".getBytes()); bo.close(); s.close(); } }
3 一个服务器+多个客户端(多线程)
package d13; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class TCP_Server5 { public static void main(String[] args) throws IOException { // 创建服务端Socket ServerSocket ss = new ServerSocket(10090); while (true) { // 监听客户端 Socket s = ss.accept(); new Thread(new UserThread(s)).start(); } } }
package d13; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.net.Socket; public class UserThread implements Runnable { private Socket s; public UserThread(Socket s) { this.s=s; } @Override public void run() { // TODO Auto-generated method stub try { // 从通道中来 BufferedInputStream bi = new BufferedInputStream(s.getInputStream()); // 到文件中去 byte [] by = new byte[1024]; String newName =System.currentTimeMillis()+".png"; BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(newName)); int len =0; while ((len=bi.read(by))!=-1) { bo.write(by,0,len); } //给客户端个反馈 OutputStream bs = s.getOutputStream(); bs.write("图片上传成功!".getBytes()); bo.close(); s.close(); } catch (Exception e) { // TODO: handle exception } } }
package d13; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; //上传图片,并保存 public class TCP_Client5 { public static void main(String[] args) throws IOException { //创建Socket对象 Socket s = new Socket("192.168.2.117", 10090); //读取图片图片 BufferedInputStream bi = new BufferedInputStream(new FileInputStream("a.png")); byte [] by = new byte[1024]; //向通道中写数据 BufferedOutputStream bo = new BufferedOutputStream(s.getOutputStream()); int len =0; while ((len=bi.read(by))>0) { bo.write(by,0,len); bo.flush(); } //通知服务器传输结束 s.shutdownOutput(); //结束服务器传来的反馈 InputStream biServer = s.getInputStream(); len= biServer.read(by); String msg =new String(by,0,len); System.out.println(msg); s.close(); } }