2013-05-06 136 views
1
java.net.SocketException: Connection reset by peer: socket write error 
    at java.net.SocketOutputStream.socketWrite0(Native Method) 
    at java.net.SocketOutputStream.socketWrite(Unknown Source) 
    at java.net.SocketOutputStream.write(Unknown Source) 
    at java.io.DataOutputStream.write(Unknown Source) 
    at java.io.DataOutputStream.writeUTF(Unknown Source) 
    at java.io.DataOutputStream.writeUTF(Unknown Source) 
    at SignUp.setUser(SignUp.java:225) 
    at SignUp.jButton1_actionPerformed(SignUp.java:207) 
    at SignUp.access$3(SignUp.java:201) 
    at SignUp$4.actionPerformed(SignUp.java:135) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$200(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

Signup.javajava.net.SocketException異常:連接被對方​​復位:套接字寫錯誤

/********************************************************/ 
/*      SignUp       */ 
/********************************************************/ 
import java.awt.*; 
import java.awt.event.*; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.net.Socket; 

import javax.swing.*; 

/** 
* Summary description for SignUp 
*/ 
public class SignUp extends JFrame { 
    // Variables declaration 
    private JLabel jLabel1; 

    private JLabel jLabel2; 

    private JLabel jLabel3; 

    private JLabel jLabel4; 

    private JTextField jTextField1; 

    private JTextField jTextField2; 

    private JTextField jTextField3; 

    private JButton jButton1; 

    private JButton jButton2; 

    private JPanel contentPane; 

    private String username; 

    private String password; 

    private String port; 

    private Socket socket; 

    private DataOutputStream dos; 

    private DataInputStream dis; 

    private ImageIcon image1; 

    // End of variables declaration 

    public SignUp() { 
     super(); 
     initializeComponent(); 
     // 
     // TODO: Add any constructor code after initializeComponent call 
     // 

     this.setVisible(true); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Windows Form Designer. Otherwise, retrieving design 
    * might not work properly. Tip: If you must revise this method, please 
    * backup this GUI file for JFrameBuilder to retrieve your design properly 
    * in future, before revising this method. 
    */ 
    private void initializeComponent() { 
     image1 = new ImageIcon("SIGNUP.jpg"); 
     jLabel1 = new JLabel(); 
     jLabel2 = new JLabel(); 
     jLabel3 = new JLabel(); 
     jLabel4 = new JLabel(image1); 
     jTextField1 = new JTextField(); 
     jTextField2 = new JTextField(); 
     jTextField3 = new JTextField(); 
     jButton1 = new JButton(); 
     jButton2 = new JButton(); 
     contentPane = (JPanel) this.getContentPane(); 

     // 
     // jLabel1 
     // 
     jLabel1.setText("NAME"); 
     // 
     // jLabel2 
     // 
     jLabel2.setText("PASSWORD"); 
     // 
     // jLabel3 
     // 
     jLabel3.setText("PORT"); 
     // 
     // jLabel4 
     // 
     //jLabel4.setText("jLabel4"); 
     // 
     // jTextField1 
     // 
     jTextField1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       jTextField1_actionPerformed(e); 
      } 

     }); 
     // 
     // jTextField2 
     // 
     jTextField2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       jTextField2_actionPerformed(e); 
      } 

     }); 
     // 
     // jTextField3 
     // 
     jTextField3.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       jTextField3_actionPerformed(e); 
      } 

     }); 
     // 
     // jButton1 
     // 
     jButton1.setText("SUBMIT"); 
     jButton1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       jButton1_actionPerformed(e); 
      } 

     }); 
     // 
     // jButton2 
     // 
     jButton2.setText("RESET"); 
     jButton2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       jButton2_actionPerformed(e); 
      } 

     }); 
     // 
     // contentPane 
     // 
     contentPane.setLayout(null); 
     addComponent(contentPane, jLabel1, 55, 100, 100, 40); 
     addComponent(contentPane, jLabel2, 55, 157, 100, 40); 
     addComponent(contentPane, jLabel3, 55, 210, 100, 40); 
     addComponent(contentPane, jLabel4, 25, 10, 300, 75); 
     addComponent(contentPane, jTextField1, 189, 99, 120, 40); 
     addComponent(contentPane, jTextField2, 188, 153, 120, 40); 
     addComponent(contentPane, jTextField3, 190, 209, 120, 40); 
     addComponent(contentPane, jButton1, 65, 267, 100, 40); 
     addComponent(contentPane, jButton2, 189, 265, 100, 40); 
     // 
     // SignUp 
     // 
     this.setTitle("SignUp - extends JFrame"); 
     this.setLocation(new Point(0, 0)); 
     this.setSize(new Dimension(370, 366)); 
    } 

    /** Add Component Without a Layout Manager (Absolute Positioning) */ 
    private void addComponent(Container container, Component c, int x, int y, 
      int width, int height) { 
     c.setBounds(x, y, width, height); 
     container.add(c); 
    } 

    // 
    // TODO: Add any appropriate code in the following Event Handling Methods 
    // 
    private void jTextField1_actionPerformed(ActionEvent e) { 
     System.out 
       .println("\njTextField1_actionPerformed(ActionEvent e) called."); 
     // TODO: Add any handling code here 

    } 

    private void jTextField2_actionPerformed(ActionEvent e) { 
     System.out 
       .println("\njTextField2_actionPerformed(ActionEvent e) called."); 
     // TODO: Add any handling code here 

    } 

    private void jTextField3_actionPerformed(ActionEvent e) { 
     System.out 
       .println("\njTextField3_actionPerformed(ActionEvent e) called."); 
     // TODO: Add any handling code here 

    } 

    private void jButton1_actionPerformed(ActionEvent e) { 
     System.out.println("\njButton1_actionPerformed(ActionEvent e) called."); 
     // TODO: Add any handling code here 
     username = jTextField1.getText(); 
     password = jTextField2.getText(); 
     port = jTextField3.getText(); 
     setUser(username, password, port); 

    } 

    private void jButton2_actionPerformed(ActionEvent e) { 
     System.out.println("\njButton2_actionPerformed(ActionEvent e) called."); 
     // TODO: Add any handling code here 

    } 

    public void setUser(String username, String password, String port) { 
     try { 
      new source(); 
      if (true) { 
       socket = new Socket("localhost", 3306); 
       dos = new DataOutputStream(socket.getOutputStream()); 
       source.gdelete(); 
       dos.writeUTF("Signup"); 
       dos.writeUTF("user"); 
       dos.writeUTF("pass"); 
       dos.writeUTF(port); 
       dis = new DataInputStream(socket.getInputStream()); 
       String response = dis.readUTF(); 
       if (response.equalsIgnoreCase("success")) { 
        System.out.println("Success..."); 
       } else { 
        System.out.println("Failure..."); 
       } 
      } 
     } catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 

    } 

    // 
    // TODO: Add any method code to meet your needs in the following area 
    // 

    // ============================= Testing ================================// 
    // = =// 
    // = The following main method is just for testing this class you built.=// 
    // = After testing,you may simply delete it. =// 
    // ======================================================================// 
    public static void main(String[] args) { 
     JFrame.setDefaultLookAndFeelDecorated(true); 
     JDialog.setDefaultLookAndFeelDecorated(true); 
     /*try { 
      UIManager 
        .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
     } catch (Exception ex) { 
      System.out.println("Failed loading L&F: "); 
      System.out.println(ex); 
     }*/ 
     new SignUp(); 
    } 
    // = End of Testing = 

} 

基本上在我的應用我想創建一個發送器和接收器。但我無法註冊用戶。不斷得到這個錯誤嘗試過在線解決方案,但它沒有幫助。

socket = new Socket("localhost", 3306);//when I change the port number I get an error saying connection refused. 
dos = new DataOutputStream(socket.getOutputStream()); 
source.gdelete(); 
dos.writeUTF("Signup");//the exception is raised the moment I hit the next line 
dos.writeUTF("user"); 
dos.writeUTF("pass"); 
dos.writeUTF(port); 
+2

發現一個bug這裏是一個噩夢。 – 2013-05-06 15:50:07

+0

如果您使用流量分析器監控流量,您是否對TCP瞭解足夠的瞭解結果? – Pietu1998 2013-05-06 15:50:18

+0

@ Peity1998準備嘗試! – Aayush 2013-05-06 15:50:53

回答

5

連接由對等復位一般是指程序處理TCP連接的遠端已經崩潰或而不關閉其插座端退出。在這種情況下,「TCP連接的遠程端」是您在localhost:3306連接的任何服務器。

一些你在這一問題的早期版本包含的代碼表明,有上localhost:3306運行MySQL服務器,但在Signup()你是一個直接的TCP連接到該端口和寫入數據到插座,而不是使用JDBC。我懷疑問題在於你連接到MySQL服務器,並且它從你的程序獲取的數據與它期望從客戶端獲得的數據不匹配,並且它正在丟棄連接導致「連接重置」信息。

如果你想與MySQL服務器通信,你應該使用JDBC和MySQL驅動程序。使用JDBC與MySQL服務器進行通信的Here is an example。請特別注意示例中的readDataBase()函數;它顯示了連接到服務器的正確方法。

0

此異常的常見原因是寫入已由對等關閉的連接。換句話說,應用程序協議錯誤。由於您似乎直接寫入MySQL端口,這並不奇怪。使用JDBC。

其他可能性包括遠程應用程序崩潰,防火牆,電纜牽引等

相關問題