版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、GetMessage.java服务端:import java.io.BufferedReader;import java.io.;import java.io.IOException;import java.io.InputStreamReader;import .ServerSocket;import .Socket;import javax.swing.JLabel;import javax.swing.JTextArea;public class GetMessage extends Threadprivate int i;String v;JLabel label=null;priva
2、te JTextArea text;public GetMessage(int i,JTextArea text) this.i=i;this.text=text;public void run()try ServerSocket so = new ServerSocket(i);Socket s = so.accept();while(true)InputStreamReader i = new InputStreamReader(s.getInputStream();BufferedReader b = new BufferedReader(i); v= b.readLine(); tex
3、t.append("对方说"+v+"n"); catch (IOException e) /label.setText("对方已经下线");text.append("对方下线了。");SendMessage.java客户端:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import .Socket;import .UnknownH
4、ostException;import javax.swing.JLabel;import javax.swing.JTextArea;import javax.swing.JTextField;public class SendMessage extends Thread private String ip;private int i;Socket s = null;JLabel label=null;JTextField text;JTextArea text1;public SendMessage(String ip,int i,JTextArea text1) / TODO Auto-
5、generated constructor stubthis.ip=ip;this.i=i;this.text1=text1; public void run() while(true)try s = new Socket(ip,i); text1.setText("连接成功"+"n"); break; catch (Exception e) try Thread.sleep(1000); catch (InterruptedException e1) System.out.println("出错了。"); public void s
6、end(String message) try PrintStream p = new PrintStream(s.getOutputStream();p.println(message); catch (Exception e1) System.out.println("异常"+e1.getMessage(); Test.java 简单的界面和测试类import java.awt.*;import java.awt.event.*;import java.io.ByteArrayInputStream;import java.io.InputStream;import j
7、ava.util.*;import javax.swing.*;import javax.swing.event.*;class WindowTextArea extends JFrame implements ActionListener String s;JTextArea text1; JTextArea text2; JButton button1,button2,button3; SendMessage t2; GetMessage t1; JLabel lable1,lable2; JTextField text; WindowTextArea() this.s=s; lable1
8、=new JLabel("对方ip"); text=new JTextField(20); text1=new JTextArea(6,18); text2=new JTextArea(6,18); text2.setEditable(false); button1=new JButton("发送"); button2=new JButton("关闭"); button3=new JButton("确定ip"); setBounds(100,100,450,300); setVisible(true); Conta
9、iner con=getContentPane(); con.setLayout(new FlowLayout(); con.add(lable1); con.add(text); con.add(button3); con.add(new JScrollPane(text1); con.add(new JScrollPane(text2); con.add(button1); con.add(button2); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener
10、(this); con.validate(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public void runthread(String ip) t1 = new GetMessage(4066,text2); t1.start(); t2=new SendMessage(ip,4066,text2); t2.start(); public void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSource()=button2)Sy
11、stem.exit(0);if(e.getSource()=button1) text2.append(text1.getText()+"n"); t2.send(text1.getText(); text1.setText("");if(e.getSource()=button3) s=text.getText();runthread(s); public class Test /* * param args */public static void main(String args) new WindowTextArea();new WindowTe
12、xtArea();向对方发送信息之前必须输入对方ip和点击确定ip按钮,文本区显示“连接成功”然后可以发送信息作者:曾建路package com.neusoft.test.对文件的内容操作.字节流.每次读取固定大小的文件内容;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.;import java.io.;import java.io.;public class Test /* * param args * throws Exception */public static void ma
13、in(String args) throws Exception fout = new ("e:/heihei.RMVB");DataOutputStream dout = new DataOutputStream(fout);long start = System.currentTimeMillis();/读取原文件的输入流 f = new ("d:/heihei.RMVB");DataInputStream d = new DataInputStream(f);/-创建一个byte类型数组,定义数据包的大小为2048 (2kb)byte b = new byte2048;int i = d.read(b);/读取文件的内容返回值是 本次读取到的字节的长度while(i != -1)dout.w
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 医院防疫绩效考核制度
- 国企风控制度
- 经络学护理的社会推广与应用
- 公证处绩效考核制度
- 审计政务服务工作制度范本
- 公司审计档案保管制度
- 4s店经员工绩效考核制度范本
- 2026年及未来5年市场数据中国机动车检测行业市场发展数据监测及投资前景展望报告
- 内审审计食堂购卖制度
- 乡镇慢性病绩效考核制度
- DB32∕T 5167-2025 超低能耗建筑技术规程
- 2026年湖南商务职业技术学院单招职业技能测试必刷测试卷及答案1套
- 雨课堂学堂在线学堂云《科学研究方法与论文写作(复大)》单元测试考核答案
- 高考语文二轮复习高中语文选用变换句式练习题含答案
- 异地升学协议书范本
- 水利工程项目法人保证安全生产措施方案
- 《AIGC文案策划与写作》-课程标准
- 企业管理顶层设计方案
- 2025年军事基础知识试题及答案
- 雨课堂在线学堂《资治通鉴》导读课后作业单元考核答案
- 2025年南京市事业单位招聘考试综合类专业能力测试试卷(管理类)真题
评论
0/150
提交评论