package com.hanlei.test02.client;
import javax.swing.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client extends Jframe {
Socket socket;
PrintWriter pWriter;
BufferedReader bReader;
String name;
public Client() {
try {
// 创建一个套接字
socket = new Socket("127.0.0.1", 28888);
// 创建一个往套接字中写数据的管道,即输出流,给服务器发送信息
pWriter = new PrintWriter(socket.getOutputStream());
// 创建一个从套接字读数据的管道,即输入流,读服务器的返回信息
bReader = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 启动线程
new GetMsgFromServer().start();
}
// 接收服务器的返回信息的线程
class GetMsgFromServer extends Thread {
public void run() {
while (this.isAlive()) {
try {
String strMsg = bReader.readLine();
if (strMsg != null) {
// 显示信息
System.out.println("success "+strMsg+"n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void Request(String name){
创建Scanner对象,接受从控制台输入
// Scanner input=new Scanner(System.in);
// //接受String类型
// String str=input.next();
// 通过输出流将数据发送给服务器 买票
pWriter.println(name);
pWriter.flush();
}
public static void main(String args[]) {
//创建聊天室客户端窗口实例,并显示
new Client().Request("zhang");
}
}
package com.hanlei.test02.server;
import com.hanlei.test02.utils.Coon;
import com.hanlei.test02.utils.Product;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.linkedList;
import java.util.Timer;
import java.util.TimerTask;
public class Server {
// 声明服务器端套接字ServerSocket
ServerSocket serverSocket;
// 客户集合
ArrayList coons = new ArrayList();
// 实例化一个产品对象,生产者和消费者共享该实例
Product product;
// 定时放票
Timer t;
// 总票数
int sum;
public Server() {
sum =0;
// 实例化一个产品对象,生产者和消费者共享该实例
product = new Product();
try {
// 创建服务器端套接字ServerSocket,在28888端口监听
serverSocket = new ServerSocket(28888);
} catch (IOException e) {
e.printStackTrace();
}
// 创建接收客户端Socket的线程实例,并启动
new AcceptSocketThread().start();
// 创建给客户端发送信息的线程实例,定时放票 并启动
t= new Timer();
t.schedule(new SendMsgToClient(), 0,5000);
System.out.println("服务器已启动...");
}
// 接收客户端Socket套接字线程
class AcceptSocketThread extends Thread {
public void run() {
while (this.isAlive()) {
try {
// 接收一个客户端Socket对象
Socket socket = serverSocket.accept();
// 建立该客户端的通信管道
if (socket != null) {
// 开启这个客户连接线程 接收他的买票请求
new Buy(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class Buy extends Thread{
Socket socket;
Coon coon;
public Buy(Socket socket){
this.socket = socket;
}
public void run() {
// 获取Socket对象的输入流
try {
BufferedReader bReader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter printWriter =new PrintWriter(socket.getOutputStream());
String strMsg = bReader.readLine();
// 将连接信息存入 ****必须先存入后开启线程
coon = new Coon(bReader,strMsg,printWriter);
coons.add(coon);
coon.toString();
// 开启一个购票线程
if (strMsg != null) {
new Consumer(strMsg);
// 买到票通知
new SendClient().start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 消费
class Consumer implements Runnable{
String name;
public Consumer(String name){
this.name = name;
new Thread(this,name).start();
}
public void run(){
product.get();
}
}
// 给所有客户发送信息的线程 放票
class SendMsgToClient extends TimerTask {
public void run() {
try {
// 如果票卖完了或有重新放票不空
if (sum<=100) {
product.put(2);
String msg = "已放票10张";
System.out.println("已放票10张");
sum +=10;
// 对输出流列表集合进行遍历,循环发送信息给所有 没买到的客户端
for(int i = 0; i< coons.size();i++){
coons.get(i).getpWriters().println(msg);
coons.get(i).getpWriters().flush();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 通知
class SendClient extends Thread {
linkedList name= product.getNames();
int count=0;
public void run() {
while (true){
if (!name.isEmpty()){
// 该顾客已买到票,移出
String nam =name.removeLast();
//System.out.println("SendClient "+nam);
for(Coon c :coons){
if (c.getName().equals(nam)){
count++;
c.getpWriters().println(""+ count);
c.getpWriters().flush();
}
}
}else{
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String args[]) {
new Server();
}
}
package com.hanlei.test02.test;
import com.hanlei.test02.utils.Product;
import java.util.linkedList;
import java.util.Timer;
import java.util.TimerTask;
public class TestSever {
// 实例化一个产品对象,生产者和消费者共享该实例
Product product;
// 定时放票
Timer t;
// 总票数
int sum;
public TestSever() {
sum =0;
// 实例化一个产品对象,生产者和消费者共享该实例
product = new Product();
// 创建接收客户端Socket的线程实例,并启动
new Buy().start();
// 买到票通知
new SendClient().start();
// 创建给客户端发送信息的线程实例,定时放票 并启动
t= new Timer();
t.schedule(new SendMsgToClient(), 0,3000);
System.out.println("服务器已启动...");
}
class Buy extends Thread{
public void run() {
// 获取Socket对象的输入流
for(int i=0; i<5;i++){
new Consumer(""+i);
}
}
}
// 消费
class Consumer implements Runnable{
String name;
public Consumer(String name){
this.name = name;
new Thread(this,name).start();
}
public void run(){
product.get();
}
}
// 给所有客户发送信息的线程 放票
class SendMsgToClient extends TimerTask {
public void run() {
try {
// 如果票卖完了或有重新放票不空
if (sum<=100) {
product.put(2);
System.out.println("已放票10张");
sum +=10;
}else{
System.out.println("100张票已放完");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 通知
class SendClient extends Thread {
linkedList name= product.getNames();
public void run() {
while (true){
if (!name.isEmpty()){
String aa =name.removeLast();
System.out.println("通知 "+aa +" 已经买到票");
}else{
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String args[]) {
new TestSever();
}
}
package com.hanlei.test02.utils;
import java.io.BufferedReader;
import java.io.PrintWriter;
public class Coon {
public BufferedReader bReader;
public String name;
public PrintWriter pWriters;
public Coon() {
}
public BufferedReader getbReader() {
return bReader;
}
public void setbReader(BufferedReader bReader) {
this.bReader = bReader;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public PrintWriter getpWriters() {
return pWriters;
}
public void setpWriters(PrintWriter pWriters) {
this.pWriters = pWriters;
}
public Coon(BufferedReader bReader, String name, PrintWriter pWriters) {
this.bReader = bReader;
this.name = name;
this.pWriters = pWriters;
}
@Override
public String toString() {
return "Coon{" +
"name='" + name + ''' +
'}';
}
}
package com.hanlei.test02.utils;
import java.util.linkedList;
//
public class Product {
int n;
// 为true时表示有值可取,为false时表示需要放入新值
boolean valueSet = false;
public linkedList getNames() {
return names;
}
// 完成列表集合
linkedList names = new linkedList();
// 生产方法
public synchronized void put(int n) {
this.n += n;
// 将valueSet设置为true,表示值已放入
valueSet = true;
// 通知等待线程,进行买票操作
notifyAll();
}
// 买票方法
public synchronized void get() {
// 如果没有值,等待新值放入
while(true){
if (!valueSet) {
try {
wait();
} catch (Exception e) {
}
} else {
int b = n;
b--;
if(b<=0){
// 将valueSet设置为false,表示票已卖完
valueSet = false;
}
n =b;
System.out.println(Thread.currentThread().getName() + "线程买到票,还剩:" + b);
names.add(Thread.currentThread().getName());//添加进去
break;
}
}
}
}