




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、客户端:package mutil;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import .Socket;import javax.swing.JTextArea;public class ClienThread extends Threadprivate Socket socket = null;private javax.swing.JTextArea jTextArea1;public Clien
2、Thread(JTextArea jTextArea1)this.jTextArea1 = jTextArea1;public void run()try/ 创建连接socket = new Socket(, 12344);String message = null;while (true)/ 接受服务端请求message = receive();/ 显示到消息接收框if (message != null & !message.equals()if (this.jTextArea1.getText() = null| this.jTextArea1.getText().equals()this
3、.jTextArea1.setText(服务端: + message); elsethis.jTextArea1.setText(this.jTextArea1.getText()+ n服务端: + message); elsemessage = 服务端以断开连接或未连接!;if (this.jTextArea1.getText() = null| this.jTextArea1.getText().equals()this.jTextArea1.setText(服务端: + message); elsethis.jTextArea1.setText(this.jTextArea1.getTe
4、xt()+ n服务端: + message);break; catch (IOException e)/ TODO Auto-generated catch blocke.printStackTrace();/* * 向服务端发送消息 */public void send(String message)tryPrintWriter printWriter = new PrintWriter(socket.getOutputStream();printWriter.write(message + n);printWriter.flush(); catch (IOException e)/ TOD
5、O Auto-generated catch blocke.printStackTrace();/* * 接受服务端消息 */public String receive()String message = ;tryBufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream();message = bufferedReader.readLine(); catch (IOException e)System.out.println(服务端已断开或未连接!);return
6、 message;服务端:package mutil;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import .ServerSocket;import .Socket;import javax.swing.JTextArea;public class ServerThread extends Threadprivate Socket socket = null;private ServerSocket s
7、erverSocket = null;private javax.swing.JTextArea jTextArea1;public ServerThread(JTextArea jTextArea1)this.jTextArea1 = jTextArea1;public void run()try/ 创建连接serverSocket = new ServerSocket(12344);/ 监听客户连接socket = serverSocket.accept();String message = null;while (true)/ 接受客户请求message = receive(socket
8、);if (message != null & !message.equals()/ 显示到消息接收框if (this.jTextArea1.getText() = null| this.jTextArea1.getText().equals()this.jTextArea1.setText(客户端: + message); elsethis.jTextArea1.setText(this.jTextArea1.getText()+ n客户端: + message); elsemessage = 客户端以断开连接或未连接!;if (this.jTextArea1.getText() = nul
9、l| this.jTextArea1.getText().equals()this.jTextArea1.setText(客户端: + message); elsethis.jTextArea1.setText(this.jTextArea1.getText()+ n客户端: + message);break; catch (IOException e)/ TODO Auto-generated catch blocke.printStackTrace();/* * 向客户端发送消息 */public void send(String message)tryPrintWriter printW
10、riter = new PrintWriter(socket.getOutputStream();printWriter.write(message + n);printWriter.flush(); catch (IOException e)/ TODO Auto-generated catch blocke.printStackTrace();/* * 接受客户端消息 */public String receive(Socket socket)String message = ;tryBufferedReader bufferedReader = new BufferedReader(ne
11、w InputStreamReader(socket.getInputStream();message = bufferedReader.readLine(); catch (IOException e)System.out.println(服务端已断开或未连接!);return message;服务端界面:/* * SereveFrame.java * * Created on _DATE_, _TIME_ */package com.insigma.frame;import mutil.ServerThread;/* * * author _USER_ */private ServerTh
12、read serverThread;/* Creates new form SereveFrame */public ServerFrame()initComponents();serverThread = new ServerThread(this.jTextArea1);serverThread.start();/* * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method
13、 is always * regenerated by the Form Editor. */ GEN-BEGIN:initComponents/ private void initComponents()jScrollPane1 = new javax.swing.JScrollPane();jTextArea1 = new javax.swing.JTextArea();jScrollPane2 = new javax.swing.JScrollPane();jTextArea2 = new javax.swing.JTextArea();jButton1 = new javax.swin
14、g.JButton();jButton2 = new javax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setTitle(u670du52a1u7aef);setBackground(new java.awt.Color(153, 153, 255);jTextArea1.setColumns(20);jTextArea1.setRows(5);jScrollPane1.setViewportView(jTextArea1);jTextArea2.setColumn
15、s(20);jTextArea2.setRows(5);jScrollPane2.setViewportView(jTextArea2);jButton1.setText(u53d6u6d88);jButton1.addActionListener(new java.awt.event.ActionListener()public void actionPerformed(java.awt.event.ActionEvent evt)jButton1ActionPerformed(evt););jButton2.setText(u53d1u9001);jButton2.addActionLis
16、tener(new java.awt.event.ActionListener()public void actionPerformed(java.awt.event.ActionEvent evt)jButton2ActionPerformed(evt););javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane();getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(jav
17、ax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addComponent(jButton1).addGap(18, 18,18)
18、.addComponent(jButton2).addGap(35, 35,35).addGroup(layout.createSequentialGroup().addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED_SIZE,377,javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE).addGroup(javax.swing.GroupLayout.Alignm
19、ent.TRAILING,layout.createSequentialGroup().addComponent(jScrollPane2,javax.swing.GroupLayout.DEFAULT_SIZE,377,Short.MAX_VALUE).addContainerGap();layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().ad
20、dComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED_SIZE,242,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18, 18, 18).addComponent(jScrollPane2,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.Layo
21、utStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jButton2).addComponent(jButton1).addContainerGap();pack();/ / GEN-END:initComponents/* * 取消 * * param evt */private void jButton1ActionPerformed(java.awt.event.ActionEvent
22、 evt)this.jTextArea2.setText();/* * 发送 * * param evt */private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)/ 向客户端发消息String message = this.jTextArea2.getText();if (message != null & !message.equals()serverThread.send(message);/ 清空消息输入框this.jTextArea2.setText();/ 显示到消息接收框if (this.jText
23、Area1.getText() = null| this.jTextArea1.getText().equals()this.jTextArea1.setText(我说: + message); elsethis.jTextArea1.setText(this.jTextArea1.getText() + n我说:+ message);/* * param args * the command line arguments */public static void main(String args)java.awt.EventQueue.invokeLater(new Runnable()pu
24、blic void run()new ServerFrame().setVisible(true););/ GEN-BEGIN:variables/ Variables declaration - do not modifyprivate javax.swing.JButton jButton1;private javax.swing.JButton jButton2;private javax.swing.JScrollPane jScrollPane1;private javax.swing.JScrollPane jScrollPane2;private javax.swing.JTex
25、tArea jTextArea1;private javax.swing.JTextArea jTextArea2;/ End of variables declaration/GEN-END:variables客户端界面:/* * ClienFrame.java * * Created on _DATE_, _TIME_ */package com.insigma.frame;import mutil.ClienThread;/* * * author _USER_ */private ClienThread clienThread;/* Creates new form ClienFram
26、e */public ClienFrame()initComponents();clienThread = new ClienThread(this.jTextArea1);clienThread.start();/* * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ GEN-
27、BEGIN:initComponents/ private void initComponents()jScrollPane1 = new javax.swing.JScrollPane();jTextArea1 = new javax.swing.JTextArea();jScrollPane2 = new javax.swing.JScrollPane();jTextArea2 = new javax.swing.JTextArea();jButton1 = new javax.swing.JButton();jButton2 = new javax.swing.JButton();set
28、DefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setTitle(u5ba2u6237u7aef);setBackground(new java.awt.Color(153, 153, 255);jTextArea1.setColumns(20);jTextArea1.setRows(5);jScrollPane1.setViewportView(jTextArea1);jTextArea2.setColumns(20);jTextArea2.setRows(5);jScrollPane2.setViewport
29、View(jTextArea2);jButton1.setText(u53d6u6d88);jButton1.addActionListener(new java.awt.event.ActionListener()public void actionPerformed(java.awt.event.ActionEvent evt)jButton1ActionPerformed(evt););jButton2.setText(u53d1u9001);jButton2.addActionListener(new java.awt.event.ActionListener()public void
30、 actionPerformed(java.awt.event.ActionEvent evt)jButton2ActionPerformed(evt););javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane();getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(lay
31、out.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1,javax.swing.GroupLayout.DEFAULT_SIZE,376, Short.MAX_VALUE).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addComp
32、onent(jButton1).addGap(18, 18,18).addComponent(jButton2).addGap(25, 25,25).addComponent(jScrollPane2,javax.swing.GroupLayout.Alignment.TRAILING,javax.swing.GroupLayout.DEFAULT_SIZE,376, Short.MAX_VALUE).addContainerGap();layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alig
33、nment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1,javax.swing.GroupLayout.DEFAULT_SIZE,240, Short.MAX_VALUE).addGap(18, 18, 18).addComponent(jScrollPane2,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jButton1).addComponent(jButton2).addContainerG
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 盐城工业职业技术学院《外贸函电》2023-2024学年第一学期期末试卷
- 上海建桥学院《材料科学与工程专业导论》2023-2024学年第二学期期末试卷
- 金昌市2025届六年级下学期模拟数学试题含解析
- 长武县2025年数学三下期末调研试题含解析
- 2025年青海省大通回族土族自治县第一完全中学高考物理试题全真模拟密押卷(五)含解析
- 福州英华职业学院《人文生物学》2023-2024学年第二学期期末试卷
- 山西国际商务职业学院《幼儿教育名著选读》2023-2024学年第二学期期末试卷
- 德州职业技术学院《中医耳鼻咽喉科学》2023-2024学年第二学期期末试卷
- 南阳医学高等专科学校《小学写作指导》2023-2024学年第二学期期末试卷
- 阳江市阳春市2025年四年级数学第二学期期末达标检测试题含解析
- 生物科技产业园区发展现状与挑战
- 2025年上海青浦新城发展(集团)限公司自主招聘9名高频重点模拟试卷提升(共500题附带答案详解)
- 雪茄烟叶晾制技术规程
- 船舶概论习题及答案
- 《智能轮椅的整体结构设计案例综述》1400字
- 北师大版八年级下学期期末数学练习题及答案
- 《性病防治知识讲座》课件
- 《脑出血的外科治疗》课件
- 2025年部编版道德与法治小学三年级下册全册教案(含教学计划)
- 职业生涯规划-体验式学习知到智慧树章节测试答案2024年秋华侨大学
- 电商设计电子课件
评论
0/150
提交评论