标题:一个简单的聊天程序,谁知道哪里错了,谢了
只看楼主
飞黄腾达
Rank: 2
等 级:论坛游民
帖 子:46
专家分:27
注 册:2013-3-14
结帖率:29.41%
已结贴  问题点数:10 回复次数:6 
一个简单的聊天程序,谁知道哪里错了,谢了
这个是服务端
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: 3Rank: 3
来 自:代码空间
等 级:论坛游侠
威 望:1
帖 子:289
专家分:124
注 册:2012-9-8
得分:4 
好像没什么问题啊

我只是个演员,还是业余的!!
2013-11-01 21:59
飞黄腾达
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
xstar海绵
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:26
专家分:160
注 册:2013-11-3
得分:4 
没发现啥错 可以运行啊

2013-11-04 23:18
飞黄腾达
Rank: 2
等 级:论坛游民
帖 子:46
专家分:27
注 册:2013-3-14
得分:0 
以下是引用xstar海绵在2013-11-4 23:18:21的发言:

没发现啥错 可以运行啊

谢谢,很奇怪,必须重启eclipse才能再次运行
2013-11-05 12:45
husiwen
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:227
专家分:1125
注 册:2010-5-23
得分:4 
这是因为你没有第一次的运行关闭干净   端口被占用着
2013-11-05 22:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-422911-1-1.html




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

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