这篇文章主要介绍“java怎么通过TCP协议传输javabean对象”,在日常操作中,相信很多人在java怎么通过TCP协议传输javabean对象问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”java怎么通过TCP协议传输javabean对象”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
javabean
package tcp.entity; import java.io.Serializable; public class User implements Serializable { /** * */ private static final long serialVersionUID = 1L; 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(); } }
客户端
package tcp.client; import java.io.DataInputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.Scanner; import tcp.entity.User; public class MyClient { public static void main(String[] args) { System.out.println("-----------客户端启动...------------"); Socket client = null; try { Scanner sc = new Scanner(System.in); while (true) { User user = null; String str = null; System.out.println("请输入用户名:"); str = sc.nextLine(); if (!"".equals(str)) { user = new User(); user.setUsername(str); } else { continue; } System.out.println("请输入用户密码:"); str = sc.nextLine(); user.setPassword(str); client = new Socket("localhost", 8848); // 一新建对象就会向服务器发起TCP连接 /** * 注意:如果使用BufferedOutputStream提高效率,那么需要在write输出完成后调用flush()将缓存的数据真正输出到网络中去,否则将报找不到EOF结尾的异常。 */ ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream())); DataInputStream dis = new DataInputStream(new BufferedInputStream(client.getInputStream())); oos.writeObject(user); // 发送到服务器 oos.flush(); System.out.println(dis.readUTF());// 从服务器接受回显 System.out.print("是否退出?"); str = sc.nextLine(); if ("y".equalsIgnoreCase(str)) { break; } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (client != null) client.close(); } catch (IOException e) { e.printStackTrace(); } } } }
服务器端
package tcp.server; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class MyServer { public static void main(String[] args) { System.out.println("=================服务器启动...==================="); ServerSocket server = null; try { server = new ServerSocket(8848); while (true) { // 侦听端口等待客户端连接 Socket client = server.accept(); // 阻塞。通过说明新产生了一个client。 new Thread(new LoginHandler(client)).start(); // 当LoginHandler()处理完后,当前client就被gc了。 } } catch (IOException e) { e.printStackTrace(); } finally { try { if (server != null) server.close(); } catch (IOException e) { e.printStackTrace(); } } } }
线程处理类
package tcp.server; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.OutputStream; import java.net.Socket; import tcp.entity.User; public class LoginHandler implements Runnable { private Socket client; @Override public void run() { InputStream is = null; OutputStream os = null; try { is = client.getInputStream(); os = client.getOutputStream(); /** * 注意:如果使用BufferedOutputStream提高效率,那么需要在write输出完成后调用flush()将缓存的数据真正输出到网络中去,否则将报找不到EOF结尾的异常。 */ ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(is)); DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os)); // 反序列化 User user = (User) ois.readObject(); // 登陆判断 if (user != null) { System.out.println("用户 :" + user.getUsername() + "[" + client.getInetAddress().getHostAddress() + ":" + client.getPort() + "]" + "密码:" + user.getPassword()); if ("cerana".equals(user.getUsername()) && "bulabula".equals(user.getPassword())) { dos.writeUTF("登陆成功"); dos.flush(); } else { dos.writeUTF("登陆失败"); dos.flush(); } } else { System.out.println("反序列化失败"); } } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (os != null) os.close(); } catch (IOException e) { e.printStackTrace(); } try { if (is != null) is.close(); } catch (IOException e) { e.printStackTrace(); } } } public LoginHandler(Socket client) { super(); this.client = client; } }
到此,关于“java怎么通过TCP协议传输javabean对象”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。