m0_51140283
2021-11-30 19:21
采纳率: 100%
浏览 27

java仿qq聊天程序显示refused connected

问题遇到的现象和发生背景

客户端服务端无法进行通信,发送消息

问题相关代码,请勿粘贴截图
package 大作业;

import java.awt.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

import javax.swing.*;
//import javax.xml.soap.Text;

public class TheMain {
    JFrame frame1 = new JFrame();
    JTextArea text1 = new JTextArea();
    public int portnum;
    public String name = "";
    // Ser1 ser1;
    /*
     * public static void main(String args[]) throws IOException,
     * InterruptedException { TheMain l=new TheMain(); //l.initDengLu();
     * 
     * l.initLiaoTian();
     * 
     * Ser1 ser1=new Ser1(); ser1.start();
     * 
     * }
     */

    public TheMain(int portnumber, String Name) {
        portnum = portnumber;
        name = Name;
        Ser1 ser1 = new Ser1();
        ser1.start();
    }

    public void initLiaoTian() {
        // JFrame frame1=new JFrame();
        frame1.setTitle("聊天界面");
        frame1.setSize(800, 820);
        frame1.setResizable(false);
        frame1.setLocationRelativeTo(null);
        frame1.setDefaultCloseOperation(2);
        JLabel people = new JLabel("好友                        聊天对象");
        JButton button1 = new JButton("返回");

        JPanel leftPanel = new JPanel();
        people.setPreferredSize(new Dimension(700, 30));
        button1.setPreferredSize(new Dimension(100, 30));
        people.setFont(new Font("宋体", Font.BOLD, 20));
        button1.setFont(new Font("宋体", Font.BOLD, 20));
        leftPanel.add(button1);
        leftPanel.add(people);
        frame1.add(leftPanel, BorderLayout.CENTER);
        // JTextArea text1=new JTextArea();
        text1.setPreferredSize(new Dimension(780, 400));
        text1.setBackground(Color.lightGray);
        text1.setEditable(false);
        frame1.add(text1, BorderLayout.CENTER);
        JTextArea text2 = new JTextArea();
        text2.setPreferredSize(new Dimension(780, 200));
        text2.setBackground(Color.white);
        text2.setEditable(true);
        frame1.add(text2, BorderLayout.CENTER);
        JPanel rightPanel = new JPanel();
        JButton button2 = new JButton("发送");
        JButton button3 = new JButton("文件");
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                text1.setEditable(true);
                String str = null;
                str = name + ":" + text2.getText();
                Scanner reader = new Scanner(System.in);
                try {
                    Socket socket = new Socket("127.0.0.1", portnum);
                    PrintWriter printwriter = new PrintWriter(socket.getOutputStream());// 发给服务器的数据
                    printwriter.println(str);
                    printwriter.flush();
                    BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String line = bufferedreader.readLine();
                    text1.append("\r\n");
                    text1.append(line);

                    text1.setEditable(false);
                    printwriter.close();
                    bufferedreader.close();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                text2.setText("");
            }
        });

        /*
         * button2.addActionListener(new ActionListener() { public void
         * actionPerformed(ActionEvent e) { text1.setEditable(true); String
         * str=text1.getText(); if(!str.isEmpty()) str+="\r\n";
         * str=str+"我:\r\n"+text2.getText(); text1.setText(str);
         * text1.setEditable(false); text2.setText(""); } });
         */

        button2.setPreferredSize(new Dimension(100, 30));
        button2.setFont(new Font("宋体", Font.BOLD, 20));
        button3.setPreferredSize(new Dimension(100, 30));
        button3.setFont(new Font("宋体", Font.BOLD, 20));
        rightPanel.add(button2);
        rightPanel.add(button3);
        frame1.add(rightPanel, BorderLayout.CENTER);
        FlowLayout f1 = new FlowLayout(FlowLayout.CENTER, 10, 20);
        frame1.setLayout(f1);
        /*
         * JLabel label=new JLabel(new ImageIcon("001.jpg"));
         * 
         * frame1.getLayeredPane().add(label);
         * 
         * JPanel j=(JPanel)frame1.getContentPane(); j.setOpaque(false);
         */
        frame1.setVisible(true);

    }

    public class Ser1 extends Thread {
        public void run() {
            // while(true) {
            BufferedReader bufferedReader = null;
            ServerSocket serverSocket1 = null;
            Socket socket = null;
            try {
                // serverSocket1=new ServerSocket(8092);
                // socket=serverSocket1.accept();
                serverSocket1 = new ServerSocket(portnum + 1);/// 每个用户占用两个端口
                while (true) {

                    System.out.println("开始等待请求。。。");
                    socket = serverSocket1.accept();
                    bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String line = bufferedReader.readLine();
                    // if (line.isEmpty()) {
                    text1.append("\r\n");
                    System.out.println("从客户端得到:" + line);
                    text1.append(line);
                    socket.close();
                    // }
                }
            } catch (IOException e) {
                e.printStackTrace();
                // System.out.println("8952");
            }
            try {
                bufferedReader.close();
                // socket.close();
            } catch (Exception e) {
            }
        }
    }
}
package 大作业;

import java.awt.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

import javax.swing.*;
//import javax.xml.soap.Text;

public class TheMain {
    JFrame frame1 = new JFrame();
    JTextArea text1 = new JTextArea();
    public int portnum;
    public String name = "";
    // Ser1 ser1;
    /*
     * public static void main(String args[]) throws IOException,
     * InterruptedException { TheMain l=new TheMain(); //l.initDengLu();
     * 
     * l.initLiaoTian();
     * 
     * Ser1 ser1=new Ser1(); ser1.start();
     * 
     * }
     */

    public TheMain(int portnumber, String Name) {
        portnum = portnumber;
        name = Name;
        Ser1 ser1 = new Ser1();
        ser1.start();
    }

    public void initLiaoTian() {
        // JFrame frame1=new JFrame();
        frame1.setTitle("聊天界面");
        frame1.setSize(800, 820);
        frame1.setResizable(false);
        frame1.setLocationRelativeTo(null);
        frame1.setDefaultCloseOperation(2);
        JLabel people = new JLabel("好友                        聊天对象");
        JButton button1 = new JButton("返回");

        JPanel leftPanel = new JPanel();
        people.setPreferredSize(new Dimension(700, 30));
        button1.setPreferredSize(new Dimension(100, 30));
        people.setFont(new Font("宋体", Font.BOLD, 20));
        button1.setFont(new Font("宋体", Font.BOLD, 20));
        leftPanel.add(button1);
        leftPanel.add(people);
        frame1.add(leftPanel, BorderLayout.CENTER);
        // JTextArea text1=new JTextArea();
        text1.setPreferredSize(new Dimension(780, 400));
        text1.setBackground(Color.lightGray);
        text1.setEditable(false);
        frame1.add(text1, BorderLayout.CENTER);
        JTextArea text2 = new JTextArea();
        text2.setPreferredSize(new Dimension(780, 200));
        text2.setBackground(Color.white);
        text2.setEditable(true);
        frame1.add(text2, BorderLayout.CENTER);
        JPanel rightPanel = new JPanel();
        JButton button2 = new JButton("发送");
        JButton button3 = new JButton("文件");
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                text1.setEditable(true);
                String str = null;
                str = name + ":" + text2.getText();
                Scanner reader = new Scanner(System.in);
                try {
                    Socket socket = new Socket("127.0.0.1", portnum);
                    PrintWriter printwriter = new PrintWriter(socket.getOutputStream());// 发给服务器的数据
                    printwriter.println(str);
                    printwriter.flush();
                    BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String line = bufferedreader.readLine();
                    text1.append("\r\n");
                    text1.append(line);

                    text1.setEditable(false);
                    printwriter.close();
                    bufferedreader.close();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                text2.setText("");
            }
        });

        /*
         * button2.addActionListener(new ActionListener() { public void
         * actionPerformed(ActionEvent e) { text1.setEditable(true); String
         * str=text1.getText(); if(!str.isEmpty()) str+="\r\n";
         * str=str+"我:\r\n"+text2.getText(); text1.setText(str);
         * text1.setEditable(false); text2.setText(""); } });
         */

        button2.setPreferredSize(new Dimension(100, 30));
        button2.setFont(new Font("宋体", Font.BOLD, 20));
        button3.setPreferredSize(new Dimension(100, 30));
        button3.setFont(new Font("宋体", Font.BOLD, 20));
        rightPanel.add(button2);
        rightPanel.add(button3);
        frame1.add(rightPanel, BorderLayout.CENTER);
        FlowLayout f1 = new FlowLayout(FlowLayout.CENTER, 10, 20);
        frame1.setLayout(f1);
        /*
         * JLabel label=new JLabel(new ImageIcon("001.jpg"));
         * 
         * frame1.getLayeredPane().add(label);
         * 
         * JPanel j=(JPanel)frame1.getContentPane(); j.setOpaque(false);
         */
        frame1.setVisible(true);

    }

    public class Ser1 extends Thread {
        public void run() {
            // while(true) {
            BufferedReader bufferedReader = null;
            ServerSocket serverSocket1 = null;
            Socket socket = null;
            try {
                // serverSocket1=new ServerSocket(8092);
                // socket=serverSocket1.accept();
                serverSocket1 = new ServerSocket(portnum + 1);/// 每个用户占用两个端口
                while (true) {

                    System.out.println("开始等待请求。。。");
                    socket = serverSocket1.accept();
                    bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String line = bufferedReader.readLine();
                    // if (line.isEmpty()) {
                    text1.append("\r\n");
                    System.out.println("从客户端得到:" + line);
                    text1.append(line);
                    socket.close();
                    // }
                }
            } catch (IOException e) {
                e.printStackTrace();
                // System.out.println("8952");
            }
            try {
                bufferedReader.close();
                // socket.close();
            } catch (Exception e) {
            }
        }
    }
}


###### 运行结果及报错内容 

```java
java.net.ConnectException: Connection refused: connect
        at java.base/java.net.PlainSocketImpl.connect0(Native Method)
        at java.base/java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:101)
        at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
        at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
        at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.base/java.net.Socket.connect(Socket.java:609)
        at java.base/java.net.Socket.connect(Socket.java:558)
        at java.base/java.net.Socket.<init>(Socket.java:454)
        at java.base/java.net.Socket.<init>(Socket.java:231)
        at 二号客户端.newOne$1.actionPerformed(newOne.java:153)
        at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)


###### 我的解答思路和尝试过的方法 

###### 我想要达到的结果
二者正常通信

1条回答 默认 最新

相关推荐 更多相似问题