java小程序

作者在 2015-01-25 14:21:48 发布以下内容
import java.io.IOException;
import java.io.*;
import java.net.*;

public class ChatServer {
        
    static ServerSocket ss = null;
    static Socket s = null;

    public static void main(String[] args) {
        
        BufferedReader br = null;
        boolean start = false;
        
        try {
            ss = new ServerSocket(8888);
            start = true;
            while(start){
                boolean beconnected = false;
                s = ss.accept();
System.out.println("a client has connected");
                beconnected = true;
                br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                
                while(beconnected){
                    try {
                        System.out.println("accept:" + br.readLine());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                
                br.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;

public class ChatClient extends Frame {
    
    TextField tf = new TextField();
    TextArea ta = new TextArea();
    Socket s = null;
    BufferedWriter bw = null;
    
    public static void main(String[] args) {
        new ChatClient().launchFrame("Chat Client");
    }
    
    
    public void launchFrame(String s){
        setTitle(s);
        setBounds(200,100,600,400);
        setBackground(Color.green);
        add(tf,BorderLayout.SOUTH);
        add(ta,BorderLayout.NORTH);
        pack();
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                disconnect();
                setVisible(false);
                System.exit(0);
            }
        });
        tf.addActionListener(new MyWindowListener());
        setVisible(true);
        connect();
    }
    
    public void connect() {
        try {
            s = new Socket("127.0.0.1",8888);
System.out.println("connected");
            bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void disconnect() {
        try {
            bw.close();
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private class MyWindowListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //String str = tf.getText().trim();
            ta.setText(e.getActionCommand());
            //ta.setText(str);
            tf.setText(" ");    
            try {
                bw.write(e.getActionCommand());
                bw.flush();
                //bw.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}

这两个小程序有什么错误谁帮我看看,我怎么调试都不对,又找不出错误,谁能帮我看看

默认分类 | 阅读 1114 次
文章评论,共0条
游客请输入验证码
浏览1113次
文章分类
文章归档
最新评论