86. 基于TCP协议的Socket编程

it2022-05-09  53

基于TCP协议的Socket编程_双向通信_实现模拟用户登录

示例:

package com.bjsxt.server; import java.io.DataOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; import com.bjsxt.entity.User; import com.bjsxt.thread.ServerThread; public class Server { public static void main(String[] args) throws IOException, ClassNotFoundException { System.out.println("-----------服务器端已启动---------------"); //1. 创建ServerSocket对象 ServerSocket server = new ServerSocket(10000); while(true){ Socket socket = server.accept();//时刻监听是否有客户端发起连接请求 //创建线程类的对象并启动线程 ServerThread st = new ServerThread(socket); //启动线程 new Thread(st).start(); } } } package com.bjsxt.thread; import java.io.DataOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.net.Socket; import com.bjsxt.entity.User; public class ServerThread implements Runnable{ private Socket socket;//成员变量 public ServerThread(Socket socket) { this.socket = socket; } @Override public void run() { System.out.println(Thread.currentThread().getName()+"请求登录"); //2. 获取输入流--(对象流) ObjectInputStream ois = null; //4. 获取输出流(数据流) DataOutputStream dos = null; try { ois = new ObjectInputStream(socket.getInputStream()); User user = (User) ois.readObject(); System.out.println(socket.getInetAddress().getHostAddress()+"请求登录:用户名"+user.getUsername()+"\t密码"+user.getPassword()); String str = ""; //3. 对用户名和密码进行验证 if("bjsxt".equals(user.getUsername())&&"bjsxt".equals(user.getPassword())){ str = "登陆成功"; }else{ str = "对不起,账号或密码不正确!"; } dos = new DataOutputStream(socket.getOutputStream()); dos.writeUTF(str); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //5. 关闭流 if(dos!=null){ try { dos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(ois!=null){ try { ois.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(socket!=null){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } package com.bjsxt.thread; import java.io.DataOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.net.Socket; import com.bjsxt.entity.User; public class ServerThread implements Runnable{ private Socket socket;//成员变量 public ServerThread(Socket socket) { this.socket = socket; } @Override public void run() { System.out.println(Thread.currentThread().getName()+"请求登录"); //2. 获取输入流--(对象流) ObjectInputStream ois = null; //4. 获取输出流(数据流) DataOutputStream dos = null; try { ois = new ObjectInputStream(socket.getInputStream()); User user = (User) ois.readObject(); System.out.println(socket.getInetAddress().getHostAddress()+"请求登录:用户名"+user.getUsername()+"\t密码"+user.getPassword()); String str = ""; //3. 对用户名和密码进行验证 if("bjsxt".equals(user.getUsername())&&"bjsxt".equals(user.getPassword())){ str = "登陆成功"; }else{ str = "对不起,账号或密码不正确!"; } dos = new DataOutputStream(socket.getOutputStream()); dos.writeUTF(str); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //5. 关闭流 if(dos!=null){ try { dos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(ois!=null){ try { ois.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(socket!=null){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } package com.bjsxt.entity; import java.io.Serializable; public class User implements Serializable{//用于封装用户名和密码,为了用于在网络上传输,必须实现该接口 /** * */ private static final long serialVersionUID = 5795034850776172068L; private String userName; private String password; public String getUsername() { return userName; } public void setUsername(String username) { this.userName = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public User(String username, String password) { super(); this.userName = username; this.password = password; } public User() { super(); } }

最新回复(0)