2018.1.1T19-B3-U3jiangyi

Posted 晓……晓

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018.1.1T19-B3-U3jiangyi相关的知识,希望对你有一定的参考价值。

package com.xdf.tcp;

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

/**  *  服务器  ===》超市  */ public class ServerSocketDemo {

 public static void main(String[] args) {   ServerSocket serverSocket = null; // 超市   Socket socket = null; // 顾客   InputStream inputStream = null; // 购物车   BufferedReader br = null; // 收银员   OutputStream outputStream = null; // 收银员的反馈   try {    // 在指定的位置创建一个超市    serverSocket = new ServerSocket(8800);    // 超市开门等待 顾客上门    socket = serverSocket.accept();    // 顾客开始购物    inputStream = socket.getInputStream();    // 收银员进行多次扫码    br = new BufferedReader(new InputStreamReader(inputStream));    String line = null;    while ((line = br.readLine()) != null) {     System.out.println("您购买的商品是===》" + line);    }    // 结束购物 不代表顾客 走出超市    socket.shutdownInput();    // 打开输出流 给顾客回馈 购物清单    outputStream = socket.getOutputStream();    outputStream.write("欢迎您下次光临".getBytes());   } catch (IOException e) {    e.printStackTrace();   } finally { // 释放资源    try {     outputStream.close();     socket.close();     br.close();     inputStream.close();     serverSocket.close();    } catch (IOException e) {     e.printStackTrace();    }   }

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

package com.xdf.tcp;

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

/**  * 客户端 ====》顾客  */ public class SocketDemo {

 public static void main(String[] args) {

  Socket socket = null; // 顾客   OutputStream outputStream = null;// 钱包   InputStream inputStream = null; // 收银员的回馈信息 购物清单   BufferedReader br = null; // 获取收银员的回馈信息   try {    socket = new Socket("localhost", 8800);    outputStream = socket.getOutputStream(); // 钱 从钱包出去    outputStream.write("回家的车票".getBytes());    // 购物完毕    socket.shutdownOutput();

   // 接收收银员的回馈信息    inputStream = socket.getInputStream();    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();     inputStream.close();     outputStream.close();     socket.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;

/**  * 客户端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();    }   }

 }

}

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

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();    }   }

 }

}

*

 

以上是关于2018.1.1T19-B3-U3jiangyi的主要内容,如果未能解决你的问题,请参考以下文章

2018.1.1T19B3-u4

2017.12.27T19-B3-U2

2017.12.29T19-B3-U3I/Ojiangyi

2017.12.25T19-B3-U2ketangjiangyi

2017.12.24T19-B3-U1zuoye

周总结06(2018.1.1-2018.1.6)