线程“主”java.net.ConnectException 中的异常:连接被拒绝:套接字编程 Java 中的线程

Posted

技术标签:

【中文标题】线程“主”java.net.ConnectException 中的异常:连接被拒绝:套接字编程 Java 中的线程【英文标题】:Exception in thread "main" java.net.ConnectException: Connection refused:thread in Socket Programming Java 【发布时间】:2022-01-19 17:05:40 【问题描述】:

我最近创建了一个多线程服务器,它可以同时从具有 GUI 的数据库中验证许多客户端的用户名和密码(在我已经创建了我的界面之间)。但是,当我尝试时遇到此错误消息运行代码:线程“main”中的异常 java.net.ConnectException:连接被拒绝:连接。这是我第一次在这里提问。

这是我的服务器代码:

public class Server 
public static void main(String[] args) 
    
    try
        ServerSocket server_socket=new ServerSocket(6969);
        while(true)
        Socket client=server_socket.accept();
        System.out.println("Un nouveau client est connecté");
        Client_Thread clientSock= new Client_Thread(client);
        new Thread(clientSock).start();
        
catch(Exception e)
    e.printStackTrace();


        
class Client_Thread implements Runnable 
private final Socket clientSocket;


public Client_Thread(Socket socket)  this.clientSocket = socket; 
@Override
public void run() 
    try
        PrintWriter out = new PrintWriter(
                clientSocket.getOutputStream());
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                    clientSocket.getInputStream()));
         String name;
         String password;
            while ((name = in.readLine()) != null &&(password= in.readLine()) != null) 
             System.out.printf(
                    "nom d'utilisateur recue du client: %s\n",
                    name); 
             out.println(name);
             System.out.printf(
                    "mot de passe recue du client: %s\n",
                    password); 
             out.println(password); 
             Connection sql = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/school","root", "Canada20Canada20");
             Statement stm = sql.createStatement();
             ResultSet  rs = stm.executeQuery("Select * from student");
             while(rs.next()) 
          String user= rs.getString("select username from student");
              String motdepasse = rs.getString("select password from student");
              if(name.equals(user)&&password.equals(motdepasse))
              System.out.println("nom d'utilisateur et mot de passe correcte");
              else
              System.out.println("nom d'utilisateur et mot de passe incorrecte");
              
              in.close();
              out.close();
         
                  
    catch(Exception e)
    e.printStackTrace();
    


这是我的客户代码:

public class Client extends javax.swing.JFrame 
public Client() 
    initComponents();

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)                                          
    try
        Socket SocketClient = new Socket ("127.0.0.1",6969);
        BufferedReader out = new BufferedReader(new InputStreamReader (SocketClient.getInputStream()));
    PrintStream in= new PrintStream(SocketClient.getOutputStream());
        in.println(jTextField1.getText());
        in.println(String.valueOf(jPasswordField1.getPassword()));
        if (out.readLine().equals("true"))
        JFrame frame=new JFrame("JOptionPane showMessageDialog example");
        JOptionPane.showMessageDialog(frame,
             "utilisateurs trouvable",
             "WELCOME",
             JOptionPane.ERROR_MESSAGE);
        jLabel3.setText("Connexion avec succés");
        else
        JFrame frame=new JFrame("JOptionPane showMessageDialog example");    
        JOptionPane.showMessageDialog(frame,
             "utilisateurs introuvable",
             "ERREUR",
             JOptionPane.ERROR_MESSAGE);
             jLabel3.setText("Connexion échouer");
        
        in.close();
        out.close();
        
    catch(Exception e)
    e.printStackTrace();
    
 

完整的错误信息是:

java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at essai.Client.jButton1ActionPerformed(Client.java:116)
at essai.Client.access$100(Client.java:16)
at essai.Client$2.actionPerformed(Client.java:58)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

          

【问题讨论】:

您正在端口6969 上运行服务器,而客户端正在连接到端口5201?那将如何运作。将客户端连接到端口 6969,在端口 5201 上运行服务器,任何一个都可以完成这项工作 我将服务器和客户端连接到同一个端口,但错误仍然存​​在 用您的编辑和最新错误更新问题。 堆栈跟踪中应该有一个“caused by:”…… 【参考方案1】:

问题是没有正确配置监听服务器,当你按下按钮时,就会抛出错误。可能的问题是,因为ServerSocket IP 地址没有明确配置,所以它会获取您的机器 IP 地址,该地址是您的路由器分配给您的机器的。我相信要解决此问题,您应该明确设置侦听服务器的 IP 地址。见HERE

ServerSocket(int port, int backlog, InetAddress bindAddr)
Create a server with the specified port, listen backlog, and local IP address to bind to.

【讨论】:

确实是问题所在,非常感谢,感谢您的帮助。

以上是关于线程“主”java.net.ConnectException 中的异常:连接被拒绝:套接字编程 Java 中的线程的主要内容,如果未能解决你的问题,请参考以下文章

Android 异步操作Android 线程切换 ( 判定当前线程是否是主线程 | 子线程中执行主线程方法 | 主线程中执行子线程方法 )

子线程怎么不阻塞主线程

QT中UI主窗口如何与子线程相互传递参数

java 子线程 回调 主线程

C++怎么在主线程中使用子线程的数据? 比如说主线程中有一个数组,如何在子线程中调用这个数组

EventBus事件通信框架 ( 发送事件 | 判断发布线程是否是主线程 | 子线程切换主线程 | 主线程切换子线程 )