java SocketException:权限被拒绝:连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13691966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:38:31  来源:igfitidea点击:

SocketException: Permission denied: connect

javasockets

提问by Hemmels

I was wondering in what other circumstances this (SocketException: Permission denied: connect) error would be thrown from the line

我想知道在其他什么情况下会从行中抛出这个 (SocketException: Permission denied: connect) 错误

SocketAddress socketAddress = new InetSocketAddress("86.143.5.165", 6464);
// Set a 3s timeout
clientSocket.connect(socketAddress, 3000);

There are a few Android issues relating to permissions, and when using a port < 1024. I am running a simple java client/server app, on port 6464, and i am using java 1.6.0_32 (after reading that Java 1.7.0_7 adds ipv6 support).

有一些与权限相关的 Android 问题,当使用端口 < 1024 时。我在端口 6464 上运行一个简单的 java 客户端/服务器应用程序,我使用的是 java 1.6.0_32(在阅读 Java 1.7.0_7 添加ipv6 支持)。

I have port 80 forwarded to my server (verified on the client machine by going to my external IP in a browser), and the port 6464 is open also.

我已将端口 80 转发到我的服务器(通过在浏览器中访问我的外部 IP 在客户端计算机上进行验证),并且端口 6464 也已打开。

Why would the client be refused connection?

为什么客户端会被拒绝连接?

EDIT: I did originally get this error when trying to connect to the server from the server itself. (Obviously, I guess it's like a telephone in that you get an engaged tone). I had a friend test it, and he could connect. I'm now connecting from a laptop that isn't on the LAN (i.e. using a 3g mobile as a hotspot), but strangely still getting the error.

编辑:当我尝试从服务器本身连接到服务器时,我最初确实收到了这个错误。(显然,我想这就像电话一样,您会听到很忙的声音)。我有一个朋友测试它,他可以连接。我现在正在从不在 LAN 上的笔记本电脑上进行连接(即使用 3g 手机作为热点),但奇怪的是仍然出现错误。

EDIT2:

编辑2:

java.net.SocketException: Permission denied: 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 runtime.MyGame.main(MyGame.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javaws.Launcher.executeApplication(Unknown Source)
at com.sun.javaws.Launcher.executeMainClass(Unknown Source)
at com.sun.javaws.Launcher.doLaunchApp(Unknown Source)
at com.sun.javaws.Launcher.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
#### Java Web Start Error:
#### Socket failed to connect

采纳答案by user207421

The client isn't being 'refused connection'. It is being refused permissionto connect, by the local operating system.

客户端没有被“拒绝连接”。本地操作系统拒绝连接权限

IPv6 support was added in Java 1.4.

Java 1.4 中添加了 IPv6 支持。

回答by Hemmels

This is to do with very localised network settings on the client. Other clients can connect without issue, I'm thinking it could be to do with workgroups, or specific local client firewalls. Thanks all.

这与客户端上非常本地化的网络设置有关。其他客户端可以毫无问题地连接,我认为这可能与工作组或特定的本地客户端防火墙有关。谢谢大家。

回答by OldCurmudgeon

For those landing here with a similar issue to mine.

对于那些与我有类似问题的人。

I was getting this error while running Spockgroovytests from maven. Running the groovytests directly worked fine while running them from mavenfailed.

在运行我得到这个错误Spockgroovy的测试mavengroovy直接运行测试工作正常,而从maven失败中运行它们。

To solve the problem configure the maven-surefire-pluginto never fork.

为了解决这个问题,配置maven-surefire-plugin永不分叉。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <forkMode>never</forkMode>
            </configuration>
        </plugin>

Not sure why this fixed the problem - would welcome comments.

不知道为什么这解决了这个问题 - 欢迎评论。

回答by VoidWhisperer

Well, first of all, giving us 'MyIpAsAString' and the port would be useful. Anyways, are you sure for a fact that the serve program is running on that IP and port? That would be generally why, or that your code for taking the connection in in itself is not correct.

好吧,首先,给我们“MyIpAsAString”和端口会很有用。无论如何,您确定服务程序正在该 IP 和端口上运行吗?这通常是为什么,或者您用于获取连接的代码本身不正确。