简单的TCP编程2
加入实体类User编程:
LoginServer.java:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package tcp1; 2 3 import java.io.DataInputStream; 4 import java.io.DataOutputStream; 5 import java.io.FileInputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.ObjectInputStream; 9 import java.net.ServerSocket; 10 import java.net.Socket; 11 12 /** 13 * 登录的服务器 14 * @author superdrew 15 * TCP 登录 16 * 功能:用户在客户端输入用户名和密码提交给服务器,服务器接收并处理,处理完后给客户端一个返回信息 17 * 客户端接口服务器的反馈消息并输出 18 * 问题分解1.先做一个单向通道,用户输入用户名和密码,服务器接收并输出 19 * 2.双向通道,服务器进行处理,处理完反馈一个信息给客户端,客户端接收并输出 20 * 采用对象流传输数据 21 * 22 * 缺点:服务器处理一次用户请求之后,就关闭了 23 * 应该是服务器一直监听,可以处理多个用户的请求 24 */ 25 public class LoginServer { 26 public static void main(String[] args) throws IOException, ClassNotFoundException { 27 //1.创建一个serversocket 创建服务 28 ServerSocket serverSocket = new ServerSocket(8888);//端口 8888 IP地址是本机 29 //2.使用 serversocket 监听 socket 套接字 30 Socket socket = serverSocket.accept(); 31 32 //3.接收用户请求,并处理 33 //InputStream is = new FileInputStream("d:/b.txt");从文件读取 34 InputStream is = socket.getInputStream(); 35 ObjectInputStream ois = new ObjectInputStream(is); 36 User user = (User)ois.readObject();//向下转型 37 38 //4.给客户端反馈一个消息 39 DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); 40 if("drew".equals(user.getName()) && "123".equals(user.getPassword())){ 41 dos.writeUTF("登录成功!--Welcome to here."); 42 }else{ 43 dos.writeUTF("登录失败,请重新输入用户名和密码!"); 44 } 45 System.out.println(user); 46 //5.关闭资源 --关闭的顺序有关系吗? 47 dos.close(); 48 ois.close(); 49 socket.close(); 50 serverSocket.close(); 51 } 52 }
LoginClient.java:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package tcp1; 2 3 import java.io.DataInputStream; 4 import java.io.DataOutputStream; 5 import java.io.IOException; 6 import java.io.ObjectOutputStream; 7 import java.io.OutputStream; 8 import java.net.InetAddress; 9 import java.net.Socket; 10 import java.net.UnknownHostException; 11 import java.util.Scanner; 12 13 /** 14 * 登录的客户端 15 * @author superdrew 16 * 17 */ 18 public class LoginClient { 19 public static void main(String[] args) throws UnknownHostException, IOException { 20 //1.创建一个socket 连接服务器 21 Socket socket = new Socket(InetAddress.getLocalHost(), 8888);//必须要知道服务器IP和端口号 22 23 //2.通过 socket 发送数据给服务器 24 //OutputStream os = new FileOutputStream("d:/b.txt");输出到文件 25 //发送对象到服务器 26 Scanner sc = new Scanner(System.in); 27 System.out.println("请输入用户名:"); 28 String name = sc.nextLine(); 29 System.out.println("请输入密码:"); 30 String password= sc.nextLine(); 31 User user = new User(name,password); 32 33 OutputStream os = socket.getOutputStream(); 34 ObjectOutputStream oos = new ObjectOutputStream(os); 35 oos.writeObject(user); 36 37 //3.接收服务器返回的消息 38 DataInputStream dis = new DataInputStream(socket.getInputStream()); 39 String result = dis.readUTF(); 40 System.out.println("服务器返回的消息:"+result); 41 42 //4.关闭资源 43 dis.close(); 44 oos.close(); 45 socket.close(); 46 } 47 }
实体类User.java:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package tcp1; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable{ 6 7 private static final long serialVersionUID = 4624000778154303345L; 8 private String name; 9 private String password; 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public String getPassword() { 17 return password; 18 } 19 public void setPassword(String password) { 20 this.password = password; 21 } 22 public User(String name, String password) { 23 super(); 24 this.name = name; 25 this.password = password; 26 } 27 public User() { 28 super(); 29 } 30 @Override 31 public String toString() { 32 return "User [name=" + name + ", password=" + password + "]"; 33 } 34 35 36 }
首先开启运行服务LoginService.java,然后开启客户端LoginClient.java,输入用户名drew,输入密码123;看看结果展示:
①②
③