看看Think in java的网络编程那章吧,这个问题简直就是针对它出的。

解决方案 »

  1.   

    随便找本java书,都能找到这些内容。抄吧。
      

  2.   

    这样的题不难吧,我高中毕业自学JAVA,这样的题我也做过两遍了。
      

  3.   

    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.Timer;
    public class ChattingServer {
      static final int PORT = 8080;
      
      public static void main(String[] args)
          throws IOException {
        ToAll toall=new ToAll();
        ServerSocket s = new ServerSocket(PORT);
        System.out.println("Server Started");
        try {
          while(true) {
            Socket socket = s.accept();
            try {
              ServeOneJabber server=new ServeOneJabber(socket);
              toall.count++;
              toall.temp=toall.count;
              server.start();
            } catch(IOException e) {
              socket.close();
            }
            System.out.println(toall.count);
          }
        } finally {
          s.close();
        }
      } 

     class ServeOneJabber extends Thread{
       
     
       public ServeOneJabber(Socket s)
       throws IOException{
       socket=s;
       send=new ToAll();
       in=
       new BufferedReader(
       new InputStreamReader(
       socket.getInputStream()));
      
      
       send.out[send.count]=
       new PrintWriter(
       new BufferedWriter(
       new OutputStreamWriter(
       socket.getOutputStream())),true);
      
       }
      
       public void run(){
       receiveThread receivethread=new receiveThread();
       receivethread.start();
       sendThread sendthread=new sendThread();
       sendthread.start();
       }
      
       private class receiveThread extends Thread{
       public void run(){
       try{
       try{
      
       while(true){
       String str=in.readLine();
           // if(str.equals("end")) break;
       send.strcon=str;
       System.out.println(str);
       sleep(150);
       }
      
       }
       catch(InterruptedException e){
       }
       System.out.println("closing...");
       }
       catch(IOException e){
      
       }finally{
       try{
       socket.close();
       }
       catch(IOException e){
       }
       }
       }
       }
      
       private class sendThread extends Thread{
    public void run(){

    try{
    while(true){
    if(send.strcon!=null){
    send.temp=send.count;
    for(int i=0;i<send.temp;i++){
    send.out[i].println(send.strcon);
    }
    send.strcon=null;
    }
    sleep(100);

    }
    }
    catch(InterruptedException e){
    }
    }
    } private class TimeEvent implements ActionListener{
    public void actionPerformed(ActionEvent event){
    send.strcon=null;
    }
    }

    private ToAll send;
       private Socket socket;
       private BufferedReader in;
       public static PrintWriter out[];
      
      }
      
      class ToAll {
       public  static int count=0;
       public  static int temp;
       public  static String strcon;
       public  static PrintWriter out[]=new PrintWriter[10];
      }
      

  4.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.net.*;public class ChattingClient{
    public static void main(String[] args){
    try{
    RoomFrame frame=new RoomFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
    }
    catch(IOException e){

    }

    }
    }class RoomFrame extends JFrame{
    public RoomFrame()
    throws IOException {
    Toolkit kit=Toolkit.getDefaultToolkit();
    Dimension screenSize=kit.getScreenSize();
    setTitle("聊天室");
    setSize(screenSize.height/2,screenSize.width/2);
    setLocation(screenSize.height/2,screenSize.width/8);


    send=new JButton("发送");

    sendContent=new JTextField(27);

    sendPanel=new JPanel();
    sendPanel.add(sendContent);
    sendPanel.add(send);
    room=new JTextArea();
    JScrollPane scrollPane=new JScrollPane(room);

    sendAction senda=new sendAction();
    send.addActionListener(senda);
    Container contantPane=getContentPane();
    contantPane.add(sendPanel,BorderLayout.SOUTH);
    contantPane.add(scrollPane,BorderLayout.CENTER);
    room.setEditable(false);
    room.setLineWrap(true);


    InputMap imap=sendPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,Event.CTRL_MASK),"sendkey");
    ActionMap amap=sendPanel.getActionMap();
    amap.put("sendkey",senda);

    //connection
    InetAddress addr = 
           InetAddress.getByName(null);
           try{
           socket = new Socket(addr, 8080);
           socket.setSoTimeout(10000);       System.out.println("socket = " + socket);
           in =
             new BufferedReader(
               new InputStreamReader(
                 socket.getInputStream()));
          
           out =
             new PrintWriter(
              new BufferedWriter(
                new OutputStreamWriter(
                   socket.getOutputStream())),true);
               
                 
                
             }
             catch(IOException e){
             
             }
             receiveThread receive=new receiveThread();
             receive.start();
            }
             private class receiveThread extends Thread{
    public void run(){
    try{
    while(true){
    try{
    String str = in.readLine();
             room.append(str+'\n');
    }
    catch(IOException e){
    }
    sleep(1000);
    }
    }
    catch(InterruptedException exception){

    }
    }
    }

    private class sendAction extends AbstractAction{
     public void actionPerformed(ActionEvent evt){
      out.println(sendContent.getText());
      sendContent.setText("");
     
    }
    }

    private BufferedReader in;
    private PrintWriter out;
    private Socket socket;
    private JTextArea room;
    private JPanel sendPanel;
    private JTextField sendContent;
    private JButton send;
    }
      

  5.   

    快给我分!!我这个是可以几个用户一起聊天的 bye 退出那个你自己写吧