2018.1.1T19B3-u4

package com.xdf.tcp.thread;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException;

/**  * 客户端1  */ public class Client {

 public static void main(String[] args) {   Socket socket = null;   InputStream inputStream = null;   OutputStream outputStream = null;   // 序列化   ObjectOutputStream oos = null;   BufferedReader br = null;   try {    // 创建客户端对象    socket = new Socket("localhost", 8800);    // 打开对应的输入和输出流    inputStream = socket.getInputStream();    outputStream = socket.getOutputStream();    // 用户开始登录    User user = new User("第一个用户", "111111");    // 序列化 给 服务器    oos = new ObjectOutputStream(outputStream);    oos.writeObject(user);    socket.shutdownOutput();

   // 接收服务器的回馈信息    br = new BufferedReader(new InputStreamReader(inputStream));    String line = null;    while ((line = br.readLine()) != null) {     System.out.println("服务器给我们的回馈信息是===》" + line);    }   } catch (UnknownHostException e) {    e.printStackTrace();   } catch (IOException e) {    e.printStackTrace();   } finally { // 释放资源    try {     br.close();     socket.close();     oos.close();     outputStream.close();     inputStream.close();    } catch (IOException e) {     e.printStackTrace();    }   }

 }********************************

package com.xdf.tcp.thread;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException;

/**  * 客户端2  */ public class Client2 {

 public static void main(String[] args) {   Socket socket = null;   InputStream inputStream = null;   OutputStream outputStream = null;   // 序列化   ObjectOutputStream oos = null;   BufferedReader br = null;   try {    // 创建客户端对象    socket = new Socket("localhost", 8800);    // 打开对应的输入和输出流    inputStream = socket.getInputStream();    outputStream = socket.getOutputStream();    // 用户开始登录    User user = new User("第二个用户", "222222");    // 序列化 给 服务器    oos = new ObjectOutputStream(outputStream);    oos.writeObject(user);    socket.shutdownOutput();

   // 接收服务器的回馈信息    br = new BufferedReader(new InputStreamReader(inputStream));    String line = null;    while ((line = br.readLine()) != null) {     System.out.println("服务器给我们的回馈信息是===》" + line);    }   } catch (UnknownHostException e) {    e.printStackTrace();   } catch (IOException e) {    e.printStackTrace();   } finally { // 释放资源    try {     br.close();     socket.close();     oos.close();     outputStream.close();     inputStream.close();    } catch (IOException e) {     e.printStackTrace();    }   }

 }

}**********************************

package com.xdf.tcp.thread;

import java.io.IOException; import java.net.ServerSocket; import java.net.Socket;

/**  * 服务器  */ public class Server {  public static void main(String[] args) {   ServerSocket serverSocket = null;   Socket socket = null;   try {    // 实例化服务器    serverSocket = new ServerSocket(8800);    // 服务器 一直 开启 24营业    while (true) {     socket = serverSocket.accept();     // 每个顾客对应一个线程     UserThread thread = new UserThread(socket);     thread.start();    }   } catch (IOException e) {    e.printStackTrace();   }  } }

*********************************

package com.xdf.tcp.thread;

import java.io.Serializable;

public class User implements Serializable { // 用户的实体类

 private String name;// 用户名  private String password; // 密码

 public String getName() {   return name;  }

 public void setName(String name) {   this.name = name;  }

 public String getPassword() {   return password;  }

 public void setPassword(String password) {   this.password = password;  }

 public User() {   super();  }

 public User(String name, String password) {   super();   this.name = name;   this.password = password;  }

 @Override  public String toString() {   return "User [name=" + name + ", password=" + password + "]";  }

***************************************

package com.xdf.tcp.thread;

import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.OutputStream; import java.net.Socket;

/**  * 用户的线程类   服务器端使用  */ public class UserThread extends Thread {  /**   * 因为run方法中要使用 定义成 全局变量   */  Socket socket = null;

 public UserThread(Socket socket) {   this.socket = socket;  }

 /**   * 获取登录用户的信息   输入流   * 并且给用户一个反馈   输出流   */  @Override  public void run() {   InputStream inputStream = null;   OutputStream outputStream = null;   // 反序列化 才能获取User对象的信息   ObjectInputStream ois = null;   try {    inputStream = socket.getInputStream();    outputStream = socket.getOutputStream();    ois = new ObjectInputStream(inputStream);    User user = (User) ois.readObject(); // 获取登录的用户信息    System.out.println("您的用户名是===》" + user.getName());    System.out.println("您的密码是===》" + user.getPassword());    socket.shutdownInput(); // 关闭用户的输入流    // 给用户的回馈信息    outputStream.write("恭喜您登录成功".getBytes());   } catch (IOException e) {    e.printStackTrace();   } catch (ClassNotFoundException e) {    e.printStackTrace();   } finally { // 释放资源    try {     outputStream.close();     socket.close();     ois.close();     inputStream.close();    } catch (IOException e) {     e.printStackTrace();    }   }

 }

}

 

posted @ 2018-01-06 23:55  水墨&丹青  阅读(89)  评论(0编辑  收藏  举报