简体   繁体   中英

java.net.ConnectException: Connection refused: connect ,,error in client /server tcp socket

i want to program application that student enter his name,ID,E-mail,Phone num and then he click on save button it must send to server and saved as file.txt,,,, i wrote this code but this error is showed every time i run it "java.net.ConnectException: Connection refused: connect" in client side, this is my code,,, please any one help me ??

The server:

   package server;  

   import java.io.BufferedReader;  
   import java.io.DataOutputStream;  
   import java.io.FileWriter;  
   import java.io.IOException;  
   import java.io.InputStreamReader;  
   import java.io.PrintWriter;  
   import java.net.ServerSocket;  
   import java.net.Socket;  

   public class Server {  

        public static void main(String[] args) {  
        try {  
            ServerSocket service;  
            service = new ServerSocket(1309);  
            Socket server = service.accept();  
            System.out.println("server has connected");  
            InputStreamReader input = new InputStreamReader(server.getInputStream());  
            DataOutputStream output = new DataOutputStream(server.getOutputStream());  
            BufferedReader in = new BufferedReader(input);  
            String inpput = in.readLine();  
            PrintWriter f = new PrintWriter("student.txt", "UTF-8");  
            f.println(inpput);  
            f.close();  

            service.close();  

        }   catch (IOException e) {  
            System.out.println(e);  
        }  

    }  

    private static FileWriter FileWriter(String studenttxt) {  
    throw new UnsupportedOperationException("Not supported yet.");   
    }  

}  

The client:

  package project4;  

  import java.io.BufferedReader;  
  import java.io.DataOutputStream;  
  import java.io.IOException;  
  import java.io.InputStreamReader;  
  import java.net.Socket; 

  public class p4 extends javax.swing.JFrame {  

    public p4() {  
        initComponents();}  

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {                                       
        try {  


            Socket client = new Socket("localhost", 1309);  
            InputStreamReader input = new InputStreamReader(client.getInputStream());  
            DataOutputStream output = new DataOutputStream(client.getOutputStream());  
            BufferedReader in = new BufferedReader(input);  

            String n, i, e, t;  
            n = name.getText();  
            i = id.getText();  
            e = email.getText();  
            t = phone.getText();  

            String mm = n + i + e + t;  

            output.writeBytes(mm);  
            output.flush();  
            System.out.println("save");  
            output.close();  
            input.close();  
            client.close();  

        } catch (IOException o) {  
            System.out.println(o);  

        }}     
 public static void main(String args[]) {  
        p4 n = new p4();  
        n.saveActionPerformed(null);  
        java.awt.EventQueue.invokeLater(new Runnable() {  
            public void run() {  
                new p4().setVisible(true);  
            }  
        });  
   }                     
    public javax.swing.JTextField email;  
    public javax.swing.JTextField id;  
    private javax.swing.JLabel jLabel1;  
    private javax.swing.JLabel jLabel2;  
    private javax.swing.JLabel jLabel3;  
    private javax.swing.JLabel jLabel4;  
    public javax.swing.JTextField name;  
    public javax.swing.JTextField phone;  
    protected javax.swing.JButton save; } 

The code seems OK (though you'll get a NullPointerException in the client as name is still null around line 22).

When I have done similar programs in Windows the problem has been the firewall. Have you tried shutting down (temporarily) the firewall ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM