谢谢啦!急用!

解决方案 »

  1.   

    GetMessage.java服务端:import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;import javax.swing.JLabel;
    import javax.swing.JTextArea;
    public class GetMessage extends Thread{
    private int i;
    String v;JLabel label=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.readLine();
    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 java.net.Socket;
    import java.net.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) {
    // TODO Auto-generated constructor stub
    this.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 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.InputStream;
    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=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(true);
      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.addActionListener(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 stub
    if(e.getSource()==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();}
    }
    new WindowTextArea();}
    }向对方发送信息之前必须输入对方ip和点击确定ip按钮,文本区显示“连接成功”
    然后可以发送信息
      

  2.   

    直接下个openfire去用吧。源码什么的都有。
      

  3.   

    //Client 程序:package Chat;import java.awt.BorderLayout;
    import java.awt.Frame;
    import java.awt.TextArea;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.ConnectException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.SocketException;import org.omg.CORBA.portable.UnknownException;public class Client extends Frame {
    TextField tf = new TextField();
    TextArea ta = new TextArea();
    Socket s = null;
    DataOutputStream daout = null;
    DataInputStream dain = null;
    boolean connected=false;
    Thread reThread =new Thread(new receiveThread()); void Lanchframe() { // 登陆主窗口
    // TODO Auto-generated method stub
    this.setLocation(600, 300);
    this.setSize(600, 600);
    this.add(tf, BorderLayout.SOUTH);
    this.add(ta, BorderLayout.NORTH);
    this.pack();
    this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    disconnect();
    System.exit(0);
    } });
    tf.addActionListener(new MyListen1er());
    this.setVisible(true);
    this.Connect();
       reThread.start();
    } public static void main(String[] args) {
    Client client = new Client();
    client.Lanchframe();
    } void Connect() {
    // TODO Auto-generated method stub
    try {
    //建立于服务器的链接
    s = new Socket("127.0.0.1", 8888);
    daout = new DataOutputStream(s.getOutputStream());
    dain = new DataInputStream(s.getInputStream());
    connected=true;
    System.out.println("connected");
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (UnknownException e) {
    e.printStackTrace(); } } void disconnect() {
    try {
    daout.close();
    dain.close();
    s.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    // TODO Auto-generated method stub
    /*try {
    connected=false;
    reThread.join();

    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    try {
    daout.close();
    dain.close();
    s.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }*/ } private class MyListen1er implements ActionListener { @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    String s1 = tf.getText().trim();
         tf.setText("");
    try {
    // System.out.println(s);
    daout.writeUTF(s1);
    daout.flush();
    // daout.close(); } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    } } } class receiveThread implements Runnable
    {
                @Override
    public void run() {
    // TODO Auto-generated method stub
                  try {
                 while(connected){
    String str=dain.readUTF();
    System.out.println(str);
    ta.setText(ta.getText()+str+"\n");
                 }
                  }catch(SocketException e){
                  
                  System.out.println("退出了!!!");
                
                

                  catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    System.out.println("connected close");

              
              
             }
    } }
    }//Server:程序package Chat;import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.EOFException;
    import java.io.IOException;
    import java.net.BindException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.SocketException;public class Serve {
    boolean start = false;
    ServerSocket ss = null;
    List<client> clients =new ArrayList<client>();

    public static void main(String[] args) {
     new Serve().start();
    } public void start() {
    try {
    //设置端口
    ss = new ServerSocket(8888);
    start = true;
    } catch (BindException e) {
    System.out.println("端口使用中");
       // System.out.println("请关掉服务器");
    System.exit(0);
    } catch (IOException e) {
    // System.out.println("系统启动失败");
    e.printStackTrace();
    }
    try {
        while (start) { 
         // 判断服务器端是否启动
    // boolean connected = false;
         //建立一个客户端接受信息
    Socket s = ss.accept();
    //client继承了线程类
    client c = new client(s);
    System.out.println("a client connected");
    //开启线程
    new Thread(c).start();
    clients.add(c);
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    try {
    ss.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }
    }
    // 开启线程之后执行此类
      class client implements Runnable {
    Socket s = null;
    DataInputStream datain = null;
    DataOutputStream datout=null;
    boolean connected = false;
            
    public client(Socket s) {
    this.s = s;

    try { 
    //信息通过端口接收而来
    datain = new DataInputStream(s.getInputStream());
    datout=new DataOutputStream(s.getOutputStream());
    connected=true;
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }    
    void send (String str){
    try {
    datout.writeUTF(str);
    } catch (IOException e) {
    clients.remove(this);
    System.out.println( " the other quit!!!");
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    @Override
    public void run() {
    client c = null;
    try {
    while (connected) { // 判断是否连接上
    String str = datain.readUTF();
    System.out.println(str);
    //通过建立的树实现全发信息
    for(int i=0;i<clients.size();i++){
       c=clients.get(i);
    c.send(str);
    }
    // for(Iterator<client> it=clients.iterator();it.hasNext();){
    // client c=it.next();
    // c.send(str);
    // }



    }
    }catch(SocketException e){
    clients.remove(this);
    }
    catch (EOFException e) {
    System.out.println("client closed");
    System.exit(0);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    finally {
    try {
    if (datain != null)
    datain.close();
    if(datout!=null) 
    datout.close();
    if (s != null)
    s.close();

    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    // if(c!=null){
    // clients.remove(c);
    // }
    //
    }

    }
    }
    }

      

  4.   

    课程设计?我们学Java的时候也让做这个来着