目录:
一、Socket
二、ServerSocket
三、ServerSocket(多线程实现)
1、UDP协议实现发送端和接收端(Socket)
发送端代码实现:
package com.yjxxt.oop.basic.Work04.day06; import java.io.IOException; import java.net.*; //实现放送端 public class UdpSendTest04 { public static void main(String[] args) throws IOException { //1、定义端口 DatagramSocket datagramSocket = new DatagramSocket(8888); //端口号 //2、准备数据 byte[] by = "你还好吗?".getBytes(); //定义字节数组,存储数据 //3、打包 - 》接收端的IP+端口 DatagramPacket(byte[] buf, int offset, int length, SocketAddress address) DatagramPacket packet = new DatagramPacket(by,0,by.length, new InetSocketAddress("localhost",8889)); //发送地址&&接收端的端口号 //4、发送 datagramSocket.send(packet); //通过send()方法 把打包好的数据发送至接收端 //5、关闭 datagramSocket.close(); //关闭端口 } }
接收端代码实现:
package com.yjxxt.oop.basic.Work04.day06; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UdpReceiveTest04 { public static void main(String[] args) throws IOException { //1、定义我是接口端 DatagramSocket datagramSocket = new DatagramSocket(8889); //定义端口号 //2、定义字节数组 byte[] by = new byte[1024]; //字节数组 存储传过来的流 //3、打包接收数据 DatagramPacket d1 = new DatagramPacket(by,0,by.length); //把接收过来的流 存储在by字节数组里 //4、接收 datagramSocket.receive(d1); //接收 //5、处理数据 System.out.println(new String(by, 0, d1.getLength())); //简单处理 打印至控制台 //6、关闭 datagramSocket.close(); //关闭端口 } }
TCP协议实行基础客户端&&服务端:
客户端代码实现(非多线程版):
package com.yjxxt.oop.basic.Work04.day06; import java.io.*; import java.net.Socket; /* TCP双向登录的客户端 */ public class TcpTwoWayClientTest07 { public static void main(String[] args) throws IOException, InterruptedException { System.out.println("-------我是客户端--------"); //1、定义我是客户端 Socket socket = new Socket("localhost",9988); //指定要发送的服务端端口 System.out.println("-------与服务器之间建立了连接-------"); //2、准备数据 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); //通过监听,获取用户名&密码 System.out.print("请输入用户名:"); String username = bufferedReader.readLine(); System.out.print("请输入密码:"); String password = bufferedReader.readLine(); //3、获取输出流 DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream()); //定义输出流 //4、IO操作 dataOutputStream.writeUTF("username=" + username + "&password=" + password); //发送流,输出 //5、刷出 dataOutputStream.flush(); //6、获取输入流 DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()); //获取服务端传回的数据 //7、读取 System.out.println(dataInputStream.readUTF()); //8、关闭 dataInputStream.close(); dataOutputStream.close(); socket.close(); } }
服务端代码实现(非多线程版):
package com.yjxxt.oop.basic.Work04.day06; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.Arrays; /* TCP双向登录的服务端 */ public class TcPTwoWayCleverTest077 { public static void main(String[] args) throws IOException { System.out.println("--------我是服务器--------"); //1、定义我是服务器 ServerSocket serverSocket = new ServerSocket(9988); //自己的端口号 //循环读取客户端 boolean flag = true; while (flag){ //循环监听 客户端排队进入 //2、阻塞监听 Socket client = serverSocket.accept(); System.out.println("---------一个客户端连接成功-----------"); //3、获取输入流 DataInputStream dataInputStream = new DataInputStream(client.getInputStream()); //4、IO操作 String str = dataInputStream.readUTF(); //讲获取的输入流存储进字符串里 //5、处理数据 ,判断 String name = ""; String pwd = ""; String[] string = str.split("&"); System.out.println(Arrays.toString(string)); for (String s: string){ String[] string2 = s.split("="); if("username".equals(string2[0])){ name = string2[1]; }else if("password".equals(string2[0])){ pwd = string2[1]; } } //6、获取输出流,结果响应给客户端 DataOutputStream dos = new DataOutputStream(client.getOutputStream()); if("kebang".equals(name) && "123456".equals(pwd)){ dos.writeUTF("登录成功"); }else { dos.writeUTF("登录失败"); } //7、刷出 dos.flush(); //8、关闭 dos.close(); dataInputStream.close(); client.close(); } serverSocket.close(); } }
TCP协议实行基础客户端&&服务端:
客户端代码实现(多线程版):
package com.yjxxt.oop.basic.Work04.day06; import java.io.*; import java.net.Socket; /* TCP实现多用户登录的服务端 */ public class TcpLulSocketTest08 { public static void main(String[] args) throws IOException { System.out.println("-------我是客户端--------"); //1、定义我是客户端 Socket socket = new Socket("localhost",8888); //指定要发送的服务端端口 System.out.println("-------与服务器之间建立了连接-------"); //2、准备数据 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); //通过监听,获取用户名&密码 System.out.print("请输入用户名:"); String username = bufferedReader.readLine(); System.out.print("请输入密码:"); String password = bufferedReader.readLine(); //3、获取输出流 DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream()); //定义输出流 //4、IO操作 dataOutputStream.writeUTF("username=" + username + "&password=" + password); //发送流,输出 //5、刷出 dataOutputStream.flush(); //6、获取输入流 DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()); //获取服务端传回的数据 //7、读取 System.out.println(dataInputStream.readUTF()); //8、关闭 dataInputStream.close(); dataOutputStream.close(); socket.close(); } }
服务端代码实现(多线程版):
package com.yjxxt.oop.basic.Work04.day06; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.Arrays; /* TCP实现多用户登录的服务端 通过多线程实现服务器端的多用户登录 一个客户端就要开启一个 线程为当前客户端服务 */ public class TcpLulServerSocketTest09 { public static void main(String[] args) throws IOException { System.out.println("--------我是服务器--------"); //1、定义我是服务器 ServerSocket serverSocket = new ServerSocket(8888); //自己的端口号 //循环接收多个客户端 boolean flag = true; while (flag){ //循环监听 客户端排队进入 //2、阻塞监听 Socket client = serverSocket.accept(); System.out.println("---------一个客户端连接成功-----------"); //开启多线程 new Thread(new Channel(client)).start(); } serverSocket.close(); } //把执行过程封装一个静态内部内里面 static class Channel implements Runnable{ //属性 //把局部变量的作用域提上来 private Socket client = null; private DataInputStream dis = null; private DataOutputStream dos = null; //空参构造器 public Channel() { } //构造器 public Channel(Socket client) { this.client = client; try { //3、获取输入流 dis = new DataInputStream(client.getInputStream()); //6、获取输出流,结果响应给客户端 dos = new DataOutputStream(client.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } //IO流,读入操作 public String read(){ String str = ""; try { str = dis.readUTF(); } catch (IOException e) { e.printStackTrace(); } return str; } //获取输出流,响应给客户端 public void write(String str){ try { dos.writeUTF(str); } catch (IOException e) { e.printStackTrace(); } } //关闭进程 public void close(){ if(client!=null){ try { client.close(); } catch (IOException e) { e.printStackTrace(); } } if(dis!=null){ try { dis.close(); } catch (IOException e) { e.printStackTrace(); } } if (dos!=null){ try { dos.close(); } catch (IOException e) { e.printStackTrace(); } } } @Override public void run() { //3、获取输入流 //4、IO操作 String str = read(); //讲获取的输入流存储进字符串里 //5、处理数据 ,判断 String name = ""; String pwd = ""; String[] string = str.split("&"); System.out.println(Arrays.toString(string)); for (String s: string){ String[] string2 = s.split("="); if("username".equals(string2[0])){ name = string2[1]; }else if("password".equals(string2[0])){ pwd = string2[1]; } } //6、获取输出流,结果响应给客户端 if("kebang".equals(name) && "123456".equals(pwd)){ write("登录成功"); }else { write("登录失败"); } // //7、刷出 // dos.flush(); //8、关闭 close(); } } }