| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1066 人关注过本帖, 1 人收藏
标题:很简单的聊天窗口,不能建立连接。郁闷啦
取消只看楼主 加入收藏
xufan
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:232
专家分:804
注 册:2008-10-20
结帖率:88.89%
收藏(1)
已结贴  问题点数:20 回复次数:2 
很简单的聊天窗口,不能建立连接。郁闷啦
源程序是这样的:
import java.awt.*;
import java.awt.event.*;
import
import
import
import
import
import java.util.Scanner;

import javax.swing.*;
public class ChatClient extends JFrame implements ActionListener {

    private JButton jbtSend,jbtExit;
    private JTextField jtfTxt1;
    private JTextArea jtaArea;
    private JLabel jlfLabel;
    private JPanel p1,p2;
    public ChatClient()
    {
        super("Chat Client");
        p1 = new JPanel();
        p2 = new JPanel();
        jtaArea = new JTextArea(8,37);
        JScrollPane pane = new JScrollPane(jtaArea);
        jtaArea.setEditable(false);
        
        jtfTxt1 = new JTextField(20);
        jbtSend = new JButton("发送");
        jbtExit = new JButton("退出");
        
        jlfLabel = new JLabel("请输入:");
        
        p1.add(jtaArea);
        p2.add(jlfLabel);
        p2.add(jtfTxt1);
        p2.add(jbtSend);
        p2.add(jbtExit);
        setLayout(new FlowLayout());

        add(p1,BorderLayout.NORTH);
        add(p2,BorderLayout.SOUTH);;
        
        jtfTxt1.addActionListener(this);
        jbtSend.addActionListener(this);
        jbtExit.addActionListener(this);
        setVisible(true);
        setSize(420, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
   
    public void actionPerformed(ActionEvent e)
    {
        String command = e.getActionCommand();
        if(command.equals("发送"))
        {
            try
            {
                Socket socket = new Socket("localhost",4700);
                BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter os = new PrintWriter(socket.getOutputStream());
        
                Scanner sc = new Scanner(System.in);
                jtaArea.setText("客户:");
                String line = sc.nextLine();
                while(!line.equals("bye"))
                {
                    os.println(line);
                    os.flush();
                    jtaArea.setText("服务器:"+is.readLine()+"\n");
                    jtaArea.setText("客户:");
                    line = sc.nextLine();
                }
                sc.close();
                os.close();
                is.close();
                socket.close();
        
            }
            catch (IOException ex)
            {
            System.out.println("ERROR:"+ex);
            }
        }
        else if(command.equals("退出"))
            System.exit(0);
    }
    public static void main(String[] args){
        ChatClient client = new ChatClient();
    }

}
图片附件: 游客没有浏览图片的权限,请 登录注册

如图示:
可惜  我运行之后提示 ERROR: Connection refused: connect
因为是自觉,所以不知道错误怎么解决。在线等
搜索更多相关主题的帖子: 聊天 private public import 
2011-08-11 16:57
xufan
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:232
专家分:804
注 册:2008-10-20
收藏
得分:0 
恩,这是服务器端的:
import java.awt.*;
import java.awt.event.*;
import
import
import
import
import
import
import java.util.Scanner;
import javax.swing.*;

public class ChatServer extends JFrame implements ActionListener {

    private JButton jbtSend,jbtExit;
    private JTextField jtfTxt1;
    private JTextArea jtaArea;
    private JLabel jlfLabel;
    private JPanel p1,p2;
    public ChatServer()
    {
        super("Chat Server");
        p1 = new JPanel();
        p2 = new JPanel();
        jtaArea = new JTextArea(8,37);
        JScrollPane pane = new JScrollPane(jtaArea);
        jtaArea.setEditable(false);
        
        jtfTxt1 = new JTextField(20);
        jbtSend = new JButton("发送");
        jbtExit = new JButton("退出");
        
        jlfLabel = new JLabel("请输入:");
        
        p1.add(jtaArea);
        p2.add(jlfLabel);
        p2.add(jtfTxt1);
        p2.add(jbtSend);
        p2.add(jbtExit);
        setLayout(new FlowLayout());

        add(p1,BorderLayout.NORTH);
        add(p2,BorderLayout.SOUTH);;
        
        jtfTxt1.addActionListener(this);
        jbtSend.addActionListener(this);
        jbtExit.addActionListener(this);
        setVisible(true);
        setSize(420, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
   
    public void actionPerformed(ActionEvent e)
    {
        String command = e.getActionCommand();
        if(command.equals("发送"))
        {
            try
            {
                ServerSocket serversocket = new ServerSocket(4700);
                Socket socket = serversocket.accept();
                BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter os = new PrintWriter(socket.getOutputStream());
        
                Scanner sc = new Scanner(System.in);
                jtaArea.setText("客户:"+is.readLine()+"\n");
                jtaArea.setText("服务器:");
                String line = sc.nextLine();
                while(!line.equals("bye"))
                {
                    os.println(line);
                    os.flush();
                    jtaArea.setText("客户:"+is.readLine()+"\n");
                    jtaArea.setText("服务器:");
                    line = sc.nextLine();
                }
                sc.close();
                os.close();
                is.close();
                socket.close();
                serversocket.close();
            }
            catch (IOException ex)
            {
            System.out.println("ERROR:"+ex);
            }
        }
        else if(command.equals("退出"))
            System.exit(0);
    }
    public static void main(String[] args){
        ChatServer client = new ChatServer();
    }

}

~~~~~~我的明天我知道~~~。
2011-08-11 18:09
xufan
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:232
专家分:804
注 册:2008-10-20
收藏
得分:0 
那到底怎么解决啊,大家帮忙看看啦

~~~~~~我的明天我知道~~~。
2011-08-12 09:55
快速回复:很简单的聊天窗口,不能建立连接。郁闷啦
数据加载中...
 
   



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

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