第十四天 简单聊天
来源:互联网 发布:hannah英文名好吗知乎 编辑:程序博客网 时间:2024/06/05 08:52
生产者和消费者
package com.java.baidu;public class Creater implements Runnable{ private Product product; public Creater(Product product){ this.product=product; } @Override public void run() { while(true){ synchronized(product){ if(product.getNum()==0){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("生产者生产一件产品"); } product.setNum(1); try { product.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } }}
package com.java.baidu;public class Consumer implements Runnable{ private Product product; public Consumer(Product product){ this.product=product; } @Override public void run() { while(true){ synchronized(product){ if(product.getNum()!=0){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("消费了一件产品"); } product.setNum(0); product.notify(); } } }}
package com.java.baidu;public class Product { private int num=0; public int getNum() { return num; } public void setNum(int num) { this.num = num; }}
package com.java.baidu;public class Test { public static void main(String[] args) { Product product=new Product(); Thread t1=new Thread(new Creater(product)); Thread t2=new Thread(new Consumer(product)); t1.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } t2.start(); }}
基于TCP的服务端与客户端
package com.java.test;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import com.java.test.MyServerRead;import javax.swing.JButton;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.ServerSocket;import java.net.Socket;import java.awt.event.ActionEvent;import javax.swing.JTextArea;import javax.swing.JList;public class StartServer extends JFrame { private JPanel contentPane; private JTextArea textArea; private Socket socket; public boolean isRunning=true; private JButton button; public boolean isRunning() { return isRunning; } public void setRunning(boolean isRunning) { this.isRunning = isRunning; } /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { StartServer frame = new StartServer(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public StartServer() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JButton btnNewButton = new JButton("发送"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { write(); } }); btnNewButton.setBounds(283, 189, 127, 52); contentPane.add(btnNewButton); textArea = new JTextArea(); textArea.setBounds(10, 189, 250, 52); contentPane.add(textArea); button = new JButton("启动服务器"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { ServerSocket server = new ServerSocket(8080); System.out.println("服务器启动"); socket = server.accept(); Thread t=new Thread(new MyServerRead(StartServer.this)); t.start(); System.out.println("有客户端连接"); } catch (IOException e1) { e1.printStackTrace(); } } }); button.setBounds(0, 10, 225, 69); contentPane.add(button); } public void read() { try { InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); System.out.println("服务器接收到的消息:"+line); } catch (IOException e) { e.printStackTrace(); } } public void write(){ try { OutputStream os=socket.getOutputStream(); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os)); String words=textArea.getText(); System.out.println("服务器发送信息"); bw.write("服务器说: "+words+"\n"); textArea.setText(""); bw.flush(); } catch (IOException e) { e.printStackTrace(); } }}
package com.java.test;import java.awt.EventQueue;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.Socket;import java.net.UnknownHostException;import javax.swing.DefaultListModel;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.JTextArea;import javax.swing.JList;public class MyClient extends JFrame { private JPanel contentPane; private Socket socket; private JTextArea textArea; private JList list; DefaultListModel<String> model; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MyClient frame = new MyClient(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public MyClient() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JButton btnNewButton = new JButton("发送"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { write(); } }); btnNewButton.setBounds(305, 200, 99, 36); contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("连接服务器"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { System.out.println("连接服务器"); socket =new Socket("192.168.0.85",8080); System.out.println("服务器连接成功"); Thread t=new Thread(new MyClientRead(MyClient.this)); t.start(); } catch (UnknownHostException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } }); btnNewButton_1.setBounds(264, 68, 160, 60); contentPane.add(btnNewButton_1); textArea = new JTextArea(); textArea.setBounds(10, 207, 228, 45); contentPane.add(textArea); list = new JList(); list.setBounds(10, 22, 228, 173); model=new DefaultListModel<>(); list.setModel(model); contentPane.add(list); } public void read() { try { InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); model.addElement(line); System.out.println(line); } catch (IOException e) { e.printStackTrace(); } } public void write(){ try { OutputStream os=socket.getOutputStream(); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os)); String words=textArea.getText(); bw.write(words+"\n"); model.addElement("客户端说: "+words); textArea.setText(""); bw.flush(); } catch (IOException e) { e.printStackTrace(); } }}
package com.java.test;public class MyClientRead implements Runnable { private MyClient client; public MyClientRead(MyClient client){ this.client=client; } @Override public void run() { while(true){ client.read(); } } }
package com.java.test;public class MyServerRead implements Runnable{ private StartServer startServer; public MyServerRead(StartServer startServer){ this.startServer=startServer; } @Override public void run() { while(startServer.isRunning){ startServer.read(); } }}
0 0
- 第十四天 简单聊天
- 第十四天
- 第十四天
- 第十四天
- 第十四天
- 第十四天
- 第十四天
- 第十四天
- 第十四天
- 世界杯第十四天
- 第十四天 工具
- 第十四天:听课笔记
- 第十四天:总结
- 第十四天总结0408
- 实习日记->第十四天
- java第十四天 泛型
- 程序学习第十四天
- 第十四天(內部類)
- this对象的引用
- 机房收费系统之修改
- leetcode 090 —— Subsets II
- linux(ubuntu 14) 安装git
- Android InputMethodManager输入法简介
- 第十四天 简单聊天
- PIC汇编指令
- 利用多线程搭建的聊天窗口(8.5)
- C++ 中的虚继承
- TelephonyUtils 工具类
- uva11237(数学题)
- 一切成功源于积累——20150804 外汇经典之商品货币 澳美 单日15H连续涨 2.10% 疯狂
- 应该读什么书——读《刘瑜的秘密书架:从经典到经验》有感
- 把BaseAdapter内部类搬运出来单独形成一个类的方法时,出现的错误