java简单的qq聊天源码_第1页
java简单的qq聊天源码_第2页
java简单的qq聊天源码_第3页
java简单的qq聊天源码_第4页
java简单的qq聊天源码_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、GetMessage.java服务端: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("对方说"+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

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("出错了。"); 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 简单的界面和测试类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("对方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(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();向对方发送信息之前必须输入对方ip和点击确定ip按钮,文本区显示“连接成功”然后可以发送信息作者:曾建路package com.neusoft.test.对文件的内容操作.字节流.每次读取固定大小的文件内容;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);/-创建一个byte类型数组,定义数据包的大小为2048 (2kb)byte b = new byte2048;int i = d.read(b);/读取文件的内容返回值是 本次读取到的字节的长

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论