Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
URLConnection类java.net.ConnectException:拒绝连接:连接_Java_Networking - Fatal编程技术网

URLConnection类java.net.ConnectException:拒绝连接:连接

URLConnection类java.net.ConnectException:拒绝连接:连接,java,networking,Java,Networking,我在一个系统上编程,客户端在一端运行Java小程序,服务器在另一端运行。现在,客户机和服务器在同一台计算机上。但是作为一个客户端,我看不到小程序,出现如下错误: java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(Unk

我在一个系统上编程,客户端在一端运行Java小程序,服务器在另一端运行。现在,客户机和服务器在同一台计算机上。但是作为一个客户端,我看不到小程序,出现如下错误:

        java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.<init>(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
    at MainJApplet.init(MainJApplet.java:58)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.<init>(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
    at RequestServer.sendRequest(RequestServer.java:25)
    at createGUI.createEditingBar(createGUI.java:1313)
    at GUI.createAndShowGUI(GUI.java:813)
    at MainJApplet.init(MainJApplet.java:137)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
java.net.ConnectException: Connection refused: connect
java.lang.NullPointerException
    at createGUI.createEditingBar(createGUI.java:1315)
    at GUI.createAndShowGUI(GUI.java:813)
    at MainJApplet.init(MainJApplet.java:137)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Exception: java.lang.NullPointerException
我检查和端口冲突。我已关闭windows防火墙,但问题仍然存在。有人能给我指个方向吗?任何建议都很好!谢谢你

很抱歉代码太长。第一个是针对客户的

import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.io.*;

public class MainJApplet extends JApplet {

    GUI gui = new GUI();
    Thread renewThread;

    public void init() {
        //pass parameters from url to applet
        String filePath = this.getParameter("FilePath");
        //temporary solution to bypass the new extention
        String userName = this.getParameter("UserName");
        String ipAddr = this.getParameter("IpAddress");
        String userLevel = this.getParameter("UserLevel");
        String ticket = this.getParameter("Ticket");
        String accessMode = this.getParameter("AccessMode"); //1-normal 2-shared 3-email

        //sharedTo will be get from the file path infor
        String sharedTo = this.getParameter("ShareTo");
        String type = this.getParameter("Type");

        gui.shareTo = sharedTo;
        gui.ticket = ticket; //this gui.ticket is used to varify email access, will be replaced

        String relativePath = "";
        try {
            //connect to servlet

            URL url = new URL(ipAddr);
            URLConnection connection = url.openConnection();

            connection.setDoOutput(true);
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
            //Passing NAME+FILE+TICKET+TYPE to server at the initialization stage
            out.write("IN");
            out.write(userName + "#");
            out.write(filePath + "#");
            out.write(ticket + "#");
            out.write(type + "#");
            out.close();

            //Wait for server response
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    connection.getInputStream()));

            String dcodedStr;
            StringBuffer decodedStringBuffer = new StringBuffer("");
            while ((dcodedStr = in.readLine()) != null) {
                decodedStringBuffer.append(dcodedStr);
            }

            String dStr = decodedStringBuffer.toString();
            in.close();

            if (dStr.equals("NOT_AUTHORIZED")) { //not passing authentication check
                JOptionPane.showMessageDialog(new JFrame(),
                        "Access Denied",
                        "Warning!",
                        JOptionPane.ERROR_MESSAGE);
                return;
            } else {//pass authentication check
                String[] initResp = dStr.split("#");
                gui.FID = initResp[0]; //file ID
                gui.EditMode = Boolean.parseBoolean(initResp[1]);
                gui.userLevel = 1; //temporary set all as normal user
                boolean firstOpen = Boolean.parseBoolean(initResp[2]);
                relativePath = initResp[3];
                if (initResp[4].trim().equals("normal")) {//owner
                    gui.accessMode = 1;
                } else {//shared user
                    gui.accessMode = 2;
                }
            }
        }catch(UnknownServiceException exp){
            exp.printStackTrace();
        }catch(IOException ex){
            ex.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
}

    public void destroy() {
        gui.check_upon_window_close();

        if (gui.is_new_drawing == 0) {
            //2010.9.29 workDir -> FID
            if (gui.EditMode) {
                RequestServer.canClose(gui.IpAddress, gui.user, gui.FID);
            } else {
                RequestServer.canCloseNoEdit(gui.IpAddress, gui.user, gui.FID);
            }
        }
        renewThread.stop();
    }
}

根据您的评论,我猜测您的服务器正在绑定到环回地址(127.0.0.1,又名localhost)。您没有发布服务器软件是什么,但更改了它的配置,使其绑定到0.0.0.0。这应该可以解决您的问题。

根据您的评论,我猜测您的服务器正在绑定到环回地址(127.0.0.1,又名localhost)。您没有发布服务器软件是什么,但更改了它的配置,使其绑定到0.0.0.0。这应该可以解决您的问题。

我首先检查与服务器的网络连接。请发布产生此异常的代码。另外,还有post服务器套接字绑定代码。@Hippo。OP说客户端和服务器在同一台主机上运行。听起来很愚蠢:
ipAddr
在运行时的值是多少?Hi Hippo,你的意思是ping ip地址吗?因为我的客户机和服务器在同一台计算机上,所以它可能总是能够连接。在windows系统中无法识别“telnet”。。。。还有其他建议吗?还是我做得不对?我首先检查服务器的网络连接。请发布产生此异常的代码。另外,还有post服务器套接字绑定代码。@Hippo。OP说客户端和服务器在同一台主机上运行。听起来很愚蠢:
ipAddr
在运行时的值是多少?Hi Hippo,你的意思是ping ip地址吗?因为我的客户机和服务器在同一台计算机上,所以它可能总是能够连接。在windows系统中无法识别“telnet”。。。。还有其他建议吗?还是我做错了?对不起。因为服务器已经是打包的、分布式的。我自己没有原始代码。我只知道在我自己的计算机上运行服务器的一些配置。我这里有点模糊。我可以知道如何重新配置绑定吗?非常感谢您的帮助!一旦我理解了你说的话,我一定会核对一下。新年快乐~对不起。因为服务器已经是打包的、分布式的。我自己没有原始代码。我只知道在我自己的计算机上运行服务器的一些配置。我这里有点模糊。我可以知道如何重新配置绑定吗?非常感谢您的帮助!一旦我理解了你说的话,我一定会核对一下。新年快乐~
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.io.*;

public class MainJApplet extends JApplet {

    GUI gui = new GUI();
    Thread renewThread;

    public void init() {
        //pass parameters from url to applet
        String filePath = this.getParameter("FilePath");
        //temporary solution to bypass the new extention
        String userName = this.getParameter("UserName");
        String ipAddr = this.getParameter("IpAddress");
        String userLevel = this.getParameter("UserLevel");
        String ticket = this.getParameter("Ticket");
        String accessMode = this.getParameter("AccessMode"); //1-normal 2-shared 3-email

        //sharedTo will be get from the file path infor
        String sharedTo = this.getParameter("ShareTo");
        String type = this.getParameter("Type");

        gui.shareTo = sharedTo;
        gui.ticket = ticket; //this gui.ticket is used to varify email access, will be replaced

        String relativePath = "";
        try {
            //connect to servlet

            URL url = new URL(ipAddr);
            URLConnection connection = url.openConnection();

            connection.setDoOutput(true);
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
            //Passing NAME+FILE+TICKET+TYPE to server at the initialization stage
            out.write("IN");
            out.write(userName + "#");
            out.write(filePath + "#");
            out.write(ticket + "#");
            out.write(type + "#");
            out.close();

            //Wait for server response
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    connection.getInputStream()));

            String dcodedStr;
            StringBuffer decodedStringBuffer = new StringBuffer("");
            while ((dcodedStr = in.readLine()) != null) {
                decodedStringBuffer.append(dcodedStr);
            }

            String dStr = decodedStringBuffer.toString();
            in.close();

            if (dStr.equals("NOT_AUTHORIZED")) { //not passing authentication check
                JOptionPane.showMessageDialog(new JFrame(),
                        "Access Denied",
                        "Warning!",
                        JOptionPane.ERROR_MESSAGE);
                return;
            } else {//pass authentication check
                String[] initResp = dStr.split("#");
                gui.FID = initResp[0]; //file ID
                gui.EditMode = Boolean.parseBoolean(initResp[1]);
                gui.userLevel = 1; //temporary set all as normal user
                boolean firstOpen = Boolean.parseBoolean(initResp[2]);
                relativePath = initResp[3];
                if (initResp[4].trim().equals("normal")) {//owner
                    gui.accessMode = 1;
                } else {//shared user
                    gui.accessMode = 2;
                }
            }
        }catch(UnknownServiceException exp){
            exp.printStackTrace();
        }catch(IOException ex){
            ex.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
}

    public void destroy() {
        gui.check_upon_window_close();

        if (gui.is_new_drawing == 0) {
            //2010.9.29 workDir -> FID
            if (gui.EditMode) {
                RequestServer.canClose(gui.IpAddress, gui.user, gui.FID);
            } else {
                RequestServer.canCloseNoEdit(gui.IpAddress, gui.user, gui.FID);
            }
        }
        renewThread.stop();
    }
}