




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、GetMessage.java服務(wù)端:import java.io.BufferedReader;import java.io.FileReader;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=
2、null;private 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.readL
3、ine(); text.append("對(duì)方說(shuō)"+v+"n"); catch (IOException e) /label.setText("對(duì)方已經(jīng)下線");text.append("對(duì)方下線了。");SendMessage.java客戶(hù)端:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import .Socket;import
4、 .UnknownHostException;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) /
5、TODO Auto-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("出錯(cuò)了。"); pub
6、lic void send(String message) try PrintStream p = new PrintStream(s.getOutputStream();p.println(message); catch (Exception e1) System.out.println("異常"+e1.getMessage(); Test.java 簡(jiǎn)單的界面和測(cè)試類(lèi)import java.awt.*;import java.awt.event.*;import java.io.ByteArrayInputStream;import java.io.InputStrea
7、m;import java.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
8、=s; lable1=new JLabel("對(duì)方ip"); text=new JTextField(20); text1=new JTextArea(6,18); text2=new JTextArea(6,18); text2.setEditable(false); button1=new JButton("發(fā)送"); button2=new JButton("關(guān)閉"); button3=new JButton("確定ip"); setBounds(100,100,450,300); setVisible(tr
9、ue); Container 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.addActi
10、onListener(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()=
11、button2)System.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();ne
12、w WindowTextArea();向?qū)Ψ桨l(fā)送信息之前必須輸入對(duì)方ip和點(diǎn)擊確定ip按鈕,文本區(qū)顯示“連接成功”然后可以發(fā)送信息作者:曾建路package com.neusoft.test.對(duì)文件的內(nèi)容操作.字節(jié)流.每次讀取固定大小的文件內(nèi)容;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;public class T
13、est /* * param args * throws Exception */public static void main(String args) throws Exception FileOutputStream fout = new FileOutputStream("e:/heihei.RMVB");DataOutputStream dout = new DataOutputStream(fout);long start = System.currentTimeMillis();/讀取原文件的輸入流FileInputStream f = new FileInputStream("d:/heihei.RMVB");DataInputStream d = new DataInputStream(f);/-創(chuàng)建一個(gè)byte類(lèi)型數(shù)組,定義數(shù)據(jù)包的大小為2048 (2kb)byte b = new byte2048;int i = d.read(b);/讀取文件的內(nèi)容返回值是 本次讀取到的字節(jié)的長(zhǎng)
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 富士康終止合同協(xié)議書(shū)
- 合同簽訂后三方協(xié)議書(shū)
- 科研獻(xiàn)血協(xié)議書(shū)
- 投資人和運(yùn)營(yíng)人協(xié)議書(shū)
- 戀愛(ài)買(mǎi)房分手有協(xié)議書(shū)
- 喝酒前先簽免責(zé)協(xié)議書(shū)
- 結(jié)對(duì)合作協(xié)議書(shū)
- 員工大飯?zhí)贸邪鼌f(xié)議書(shū)
- 電費(fèi)起碼協(xié)議書(shū)
- 終止謠言協(xié)議書(shū)
- 色卡-CBCC中國(guó)建筑標(biāo)準(zhǔn)色卡(千色卡1026色)
- 《數(shù)據(jù)資產(chǎn)會(huì)計(jì)》 課件 第二章 數(shù)據(jù)的資產(chǎn)化
- 2024年河北省高考?xì)v史試卷(含答案解析)
- 2024年危險(xiǎn)品二手車(chē)收購(gòu)協(xié)議書(shū)范文
- 高考英語(yǔ)高頻詞600
- 2022年江蘇省江陰市四校高一物理第二學(xué)期期末經(jīng)典試題含解析
- 2023年江蘇省南京市中考化學(xué)真題(原卷版)
- DB15-T 3619-2024 旅游風(fēng)景道驛站等級(jí)劃分與評(píng)定
- (高清版)DB15∕T 3585-2024 高標(biāo)準(zhǔn)農(nóng)田施工質(zhì)量評(píng)定規(guī)程
- 中考物理實(shí)驗(yàn)19 (考點(diǎn)精講)測(cè)量滑輪組的機(jī)械效率
- 武進(jìn)經(jīng)濟(jì)發(fā)展集團(tuán)筆試
評(píng)論
0/150
提交評(píng)論