第二周周三
2015-08-05 20:38
232 查看
聊天工具(初级版)
网络连接 多线程 综合应用程序分为四部分,包括服务器、客户端
public class MyServer extends JFrame { private JPanel contentPane; private Socket socket; private boolean isRunning = true; private JTextArea textArea; 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 { MyServer frame = new MyServer(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public MyServer() { 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 button = new JButton("\u542F\u52A8\u670D\u52A1"); 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(MyServer.this)); t.start(); System.out.println("有客户端链接"); }catch (IOException e1){ e1.printStackTrace(); } } }); button.setBounds(22, 10, 475, 87); contentPane.add(button); textArea = new JTextArea(); textArea.setBounds(100, 476, 518, 138); contentPane.add(textArea); JButton button_1 = new JButton("\u53D1\u7ED9\u5BA2\u6237\u7AEF"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { write(); } }); button_1.setBounds(671, 531, 93, 23); contentPane.add(button_1); } 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) { // TODO Auto-generated catch block 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) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public class MyServerRead implements Runnable{ private MyServer server; public MyServerRead(MyServer server){ this.server = server; } @Override public void run() { while (server.isRunning()){ server.read(); } } }
public class MyClient extends JFrame { private JPanel contentPane; private Socket socket; private JTextArea textArea; private JList list; private DefaultListModel<String> modle; /** * 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, 850, 500); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); textArea = new JTextArea(); textArea.setBounds(49, 436, 500, 101); contentPane.add(textArea); JButton btnNewButton = new JButton("发送"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { write(); } }); btnNewButton.setBounds(589, 436, 129, 101); contentPane.add(btnNewButton); list = new JList(); list.setBounds(61,10,484,416); modle = new DefaultListModel<>(); list.setModel(modle); contentPane.add(list); 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.41", 8080); System.out.println("连接服务器成功"); Thread t=new Thread(new MyClientRead(MyClient.this)); t.start(); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); btnNewButton_1.setBounds(615, 68, 103, 86); contentPane.add(btnNewButton_1); } public void read() { try { InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); modle.addElement("服务器说的"+line); System.out.println(line); } catch (IOException e) { // TODO Auto-generated catch block 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"); modle.addElement(words); textArea.setText(""); bw.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public class MyClientRead implements Runnable { private MyClient client; public MyClientRead(MyClient client){ this.client = client; } @Override public void run() { while(true){ client.read(); } } }
相关文章推荐
- 观察者模式、简单一对一双向聊天系统
- 枚举的简单应用(二)
- java工具类(三)之生成若干位随机数
- hdu1372(最爱的广搜)
- JavaScript基础----10Javascript语法-跳转语句----break和continue区别
- UIView详解
- Java中简单的生产者和消费者问题
- java工具类(三)之生成若干位随机数
- C语言:使用realloc函数对malloc或者calloc动态分配的内存大小进行扩展
- 200之内被7整除的数
- iOS之UIView的变更动画
- c#构造函数
- vim中的airline插件不显示箭头(解决)
- 虚拟化项目之运维实践
- 关于QT的系统总结
- JavaScript基础----09Javascript语法-循环语句while循环
- VS2010的MFC对话框程序中添加菜单栏的过程
- 随便写写
- 搜索引擎收录大全
- DayuCMS 1.525 /include/global.func.php Foreground Arbitrary Code Execution