标题:很简单的聊天窗口,不能建立连接。郁闷啦
只看楼主
xufan
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:232
专家分:804
注 册:2008-10-20
结帖率:88.89%
已结贴  问题点数:20 回复次数:7 
很简单的聊天窗口,不能建立连接。郁闷啦
源程序是这样的:
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
shenyun345
Rank: 2
等 级:论坛游民
帖 子:32
专家分:10
注 册:2009-7-13
得分:7 
服务端程序呢?
  服务端程序也贴出来看看,这应该是有异常,直接显示异常信息了
2011-08-11 17:42
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
shenyun345
Rank: 2
等 级:论坛游民
帖 子:32
专家分:10
注 册:2009-7-13
得分:0 
暂时还没有仔细看,但是你这个不能连接滴问题大概就出现在服务器端上面,
你服务端程序滴意思是要先点击“发送”才开始监听端口,这样就导致了你不能连接上服务器吧
2011-08-11 18:35
xufan
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:232
专家分:804
注 册:2008-10-20
得分:0 
那到底怎么解决啊,大家帮忙看看啦

~~~~~~我的明天我知道~~~。
2011-08-12 09:55
husiwen
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:227
专家分:1125
注 册:2010-5-23
得分:7 
把连接的代码提到外面去,不然你点一次发送。就要重新连接一次。。端口会被 占用的。。。
最后用上线程。。不然你要自己发一条消息出去的时候才能收到对方发来的消息
2011-08-16 22:10
zyzab
Rank: 2
等 级:论坛游民
帖 子:29
专家分:25
注 册:2010-5-13
得分:7 
错误是说,服务器拒绝连接,你写的服务器代码是点击发送的时候,才连接,如果你没有点击服务器界面的发送,先点击客服端的发送,那么就没有连接上服务器了,你应该在服务器启动的时候就开始建立连接(而不是点击服务器的发送按钮才允许客服端连接),等待客服端的连接请求,那么就连的上服务器了。写代码的时候,先把连接弄好了,再增加别的功能,这样的编程习惯对以后很有好处!
2011-08-18 21:42
zyzab
Rank: 2
等 级:论坛游民
帖 子:29
专家分:25
注 册:2010-5-13
得分:0 
你解决了问题了吗?哈哈……
2011-08-19 01:22



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




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

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