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

时间:2011-12-29 12:22:46

标签: java networking

我在一个系统上编程,客户端一端运行Java Applet,另一端运行服务器。现在我在同一台计算机上安装了客户端和服务器。但作为一个客户端,我看不到applet,我得到如下错误:

        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

以下是问题追溯的地方:

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

connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); // exception happens

我检查了http://localhost:8080/和端口冲突。我已关闭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();
    }
}

1 个答案:

答案 0 :(得分:1)

基于您的评论,我的猜测是您的服务器绑定到环回地址(127.0.0.1,又名localhost)。您没有发布服务器软件的内容,但更改了它的配置,以便它在0.0.0.0上绑定。这应该可以解决你的问题。