客户端和服务端的消息传递
| package com.NetDemo02; |
| |
| import java.io.*; |
| import java.net.Socket; |
| import java.util.Scanner; |
| public class ClicentService { |
| public static void main(String[] args) throws IOException { |
| |
| Socket socket =new Socket("192.168.201.23",8080); |
| |
| Scanner scanner = new Scanner(System.in); |
| System.out.println("请录入您的账号:"); |
| String name = scanner.next(); |
| System.out.println("请录入您的密码:"); |
| String pwd = scanner.next(); |
| |
| Information information =new Information(name,pwd); |
| OutputStream output =socket.getOutputStream(); |
| ObjectOutputStream oops = new ObjectOutputStream(output); |
| oops.writeObject(information); |
| |
| InputStream inputStream = socket.getInputStream(); |
| DataInputStream dataInputStream = new DataInputStream(inputStream); |
| if(dataInputStream.readBoolean()){ |
| System.out.println("欢迎使用登录"); |
| }else { |
| System.out.println("账号或密码错误"); |
| } |
| inputStream.close(); |
| oops.close(); |
| output.close(); |
| socket.close(); |
| } |
| } |
| |
| package com.NetDemo02; |
| |
| import java.io.IOException; |
| import java.net.InetAddress; |
| import java.net.ServerSocket; |
| import java.net.Socket; |
| |
| public class ServiceDemo { |
| public static void main(String[] args) { |
| try { |
| ServerSocket serverSocket = null; |
| serverSocket = new ServerSocket(8080); |
| |
| int count = 0; |
| |
| |
| while (true) { |
| Socket accept = serverSocket.accept(); |
| |
| new ServiceTreand(accept).start(); |
| count++; |
| System.out.println("记录的IP:"+InetAddress.getLocalHost()+";访问的个数:"+count); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| |
| } |
| } |
| |
| |
| |
| package com.NetDemo02; |
| |
| import java.io.Serializable; |
| |
| public class Information implements Serializable { |
| private static final long serialVersionUID = 1039520093218203872L; |
| private String name; |
| private String pwd; |
| |
| public Information(String name, String pwd) { |
| this.name = name; |
| this.pwd = pwd; |
| } |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
| public String getPwd() { |
| return pwd; |
| } |
| |
| public void setPwd(String pwd) { |
| this.pwd = pwd; |
| } |
| |
| } |
| |
| package com.NetDemo02; |
| |
| import java.io.*; |
| import java.net.Socket; |
| |
| public class ServiceTreand extends Thread{ |
| Socket accept = null; |
| DataOutputStream dataOutputStream = null; |
| OutputStream outputStream= null; |
| ObjectInputStream obips=null; |
| InputStream inputStream = null; |
| public ServiceTreand(Socket accept) { |
| this.accept = accept; |
| } |
| |
| @Override |
| public void run() { |
| try { |
| inputStream =accept.getInputStream(); |
| obips = new ObjectInputStream(inputStream); |
| Information information = (Information)(obips.readObject()); |
| |
| outputStream = accept.getOutputStream(); |
| dataOutputStream = new DataOutputStream(outputStream); |
| if (information.getName().equals("丽丽")||information.getPwd().equals("123123")){ |
| dataOutputStream.writeBoolean(true); |
| }else { |
| dataOutputStream.writeBoolean(false); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } catch (ClassNotFoundException e) { |
| e.printStackTrace(); |
| }finally { |
| try { |
| if(dataOutputStream!= null) { |
| dataOutputStream.close(); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| try { |
| if(outputStream!=null){ |
| outputStream.close(); |
| } |
| |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| try { |
| if(obips!=null){ |
| obips.close(); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| try { |
| if(inputStream!=null){ |
| inputStream.close(); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| try { |
| if(accept!=null){ |
| accept.close(); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义