| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 635 人关注过本帖
标题:一个简单的聊天程序,谁知道哪里错了,谢了
取消只看楼主 加入收藏
飞黄腾达
Rank: 2
等 级:论坛游民
帖 子:46
专家分:27
注 册:2013-3-14
结帖率:29.41%
收藏
已结贴  问题点数:10 回复次数:3 
一个简单的聊天程序,谁知道哪里错了,谢了
这个是服务端
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import
import *;
import
import

import javax.imageio.IIOException;
import javax.swing.*;

public class server extends JFrame implements ActionListener {

    JTextField text;
   
    JTextArea area;
    InputStream in;
    OutputStream out;

    public server() throws IOException {

        setTitle("服务机");
        text = new JTextField(20);
        text.setBounds(10, 10, 270, 30);

        JPanel p = new JPanel();
        area = new JTextArea(30, 30);
        area.setBackground(Color.cyan);

        p.add(text);
        text.addActionListener(this);

        p.add(new JLabel("发送"));
        add("North", p);
        add("Center", area);
        setVisible(true);
        setSize(400, 400);
        try {
            ServerSocket server = new ServerSocket(400);
            Socket client = server.accept();
            area.append("已连接到客户机" + client.getInetAddress().getLocalHost());
            in = client.getInputStream();
            out = client.getOutputStream();
        } catch (IIOException e) {
        }
        while (true) {
            byte[] buf = new byte[128];
            in.read(buf);

            area.setText("客户机说" + new String(buf) + "\n");
        }
    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == text) {
            String s = text.getText();
            area.append("服务机说:" + s + "\n");
            try {
                out.write(s.getBytes());
            } catch (IOException e1) {

                e1.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        new server();
    }
}
这个是客户端

import java.awt.Color;
import java.awt.event.*;
import *;
import *;

import javax.imageio.IIOException;
import javax.swing.*;

public class Client extends JFrame implements ActionListener {
    JPanel p;
    JTextField text;
    JTextArea area;

    InputStream in;
    OutputStream out;

    public Client() throws UnknownHostException, IOException {

        setTitle("客户机");
        text = new JTextField(20);
        text.setBounds(10, 10, 270, 30);
        JPanel p = new JPanel();
        area = new JTextArea(30, 30);
        area.setBackground(Color.cyan);
        text.addActionListener(this);
        p.add(text);
        p.add(new JLabel("发送"));
        add("North", p);
        add("Center", area);
        setVisible(true);
        setSize(400, 400);
        try {
            Socket client = new Socket("127.127.0.0", 400);
            in = client.getInputStream();
            out = client.getOutputStream();
            area.append("   服务器为:" + client.getInetAddress().getHostName()
                    + "\n");
        } catch (IIOException e) {
        }
        while (true) {
            try {
                byte buf[] = new byte[256];
                in.read(buf);
                area.setText("服务器说" + new String(buf) + "\n");
            } catch (IIOException e0) {
            }
        }
    }

    public static void main(String[] args) {
        try {
            Client e = new Client();
        } catch (UnknownHostException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }

    @Override
    public void actionPerformed(ActionEvent a) {

        if (a.getSource() == text) {
            String st;

            st = text.getText();
            area.append("客户机说" + st + "\n");
            try {
                out.write(st.getBytes());
            } catch (IOException e) {

                e.printStackTrace();
            }
        }

    }
}
搜索更多相关主题的帖子: import server public 服务端 
2013-11-01 21:31
飞黄腾达
Rank: 2
等 级:论坛游民
帖 子:46
专家分:27
注 册:2013-3-14
收藏
得分:0 
回复 2楼 经哥
有问题呀
2013-11-02 12:25
飞黄腾达
Rank: 2
等 级:论坛游民
帖 子:46
专家分:27
注 册:2013-3-14
收藏
得分:0 
没人知道吗
2013-11-04 22:53
飞黄腾达
Rank: 2
等 级:论坛游民
帖 子:46
专家分:27
注 册:2013-3-14
收藏
得分:0 
以下是引用xstar海绵在2013-11-4 23:18:21的发言:

没发现啥错 可以运行啊

谢谢,很奇怪,必须重启eclipse才能再次运行
2013-11-05 12:45
快速回复:一个简单的聊天程序,谁知道哪里错了,谢了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017426 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved