client.java
资源名称:QQ.rar [点击查看]
上传用户:hongdaled
上传日期:2007-10-03
资源大小:680k
文件大小:3k
源码类别:
ICQ/即时通讯
开发平台:
Java
- package client;
- import java.awt.BorderLayout;
- import java.awt.Button;
- import java.awt.GridBagLayout;
- import java.awt.GridLayout;
- import java.awt.TextField;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Properties;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import com.sun.image.codec.jpeg.JPEGDecodeParam;
- import sun.awt.windows.ThemeReader;
- class client extends Thread {
- int id;
- ObjectOutputStream dos;
- //去往客户的输出流
- ObjectInputStream din;
- //来自客户的输入流
- JFrame f;;
- JPanel panelx=new JPanel();
- JPanel jp=new JPanel();
- JTextArea input=new JTextArea(3,20);
- JTextArea display=new JTextArea(4,20);
- JScrollPane js=new JScrollPane(display);
- JScrollPane js1=new JScrollPane(input);
- JButton b=new JButton("发送:");
- public client(int id,ObjectOutputStream oos,ObjectInputStream din)
- {
- this.id=id;
- this.dos=oos;
- this.din=din;
- panelx.setLayout(new GridLayout(2,1,15,15));
- f=new JFrame("client-"+serverthread.name);
- panelx.add(js);
- panelx.add(js1);
- jp.add(b);
- //f.add(jp);
- //f.add(jp1);
- //f.add(b);
- f.add(panelx,BorderLayout.CENTER);
- f.add(jp,BorderLayout.SOUTH);
- //f.add(panelx);
- f.setSize(400,400);
- f.setVisible(true);
- // this.start();
- b.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- try{
- ArrayList arr=new ArrayList();
- arr.add(input.getText());
- arr.add(serverthread.name);
- dos.writeObject(arr);
- display.append(new Date().toString()+serverthread.name+"n"+input.getText());
- //发送数据
- input.setText("");
- }
- catch(IOException z)
- {
- }
- }
- }
- );
- }
- public void run() {
- //循环读取客户数据转发给其他客户
- while(true) {
- try{
- Object oo=din.readObject();
- int m=serverthread.clientnum;
- //读客户数据,无数据时线程将挂起
- if(oo instanceof ArrayList){
- ArrayList user=(ArrayList)oo;
- display.append(new Date().toString()+user.get(1)+"n"+user.get(0).toString()+"n");
- // String message=user.get(0);
- // System.out.println(message);
- // client c=(client)serverthread.l.get(m-1);
- // c.dos.writeObject(user);
- }
- }
- catch(IOException e)
- {
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }