Android套接字连接拒绝错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22474466/
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
Android socket connection refused error
提问by Tony
I want to implement socket connection between 2 deceives , client keep sending GPS data to the server and I need both of it run in new thread , the client send first one data then keep show error like this
我想在两个欺骗之间实现套接字连接,客户端不断向服务器发送 GPS 数据,我需要它们都在新线程中运行,客户端先发送一个数据,然后像这样保持显示错误
03-18 16:35:11.805: E/Client run:(8163): java.net.ConnectException: failed to connect to /192.168.2.103 (port 5678): connect failed: ECONNREFUSED (Connection refused)
03-18 16:35:11.805:E/客户端运行:(8163):java.net.ConnectException:无法连接到/192.168.2.103(端口5678):连接失败:ECONNREFUSED(连接被拒绝)
here is the client code
这是客户端代码
public class Send implements Runnable{
private boolean Connect = true;
public void Connect(){
Connect = true;
}
public void Disconnect(){
Connect = false;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(Connect){
try {
SocketClient = new Socket("192.168.2.103", 5678);
ObjectOutputStream oos = new ObjectOutputStream(SocketClient.getOutputStream());
oos.writeDouble(GPSinfo[2]);
//ObjectInputStream ois = new ObjectInputStream(SocketClient.getInputStream());
//ois.readInt();
oos.close();
//ois.close();
} catch (Exception e) {
Log.e("Client run: ", e.toString());
}
}
}
}
here is server code
这是服务器代码
public class Receive implements Runnable{
private boolean CanReceive = true;
private double Data;
public void Connect(){
CanReceive = true;
}
public void Disconnect(){
CanReceive = false;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(CanReceive){
try {
SocketServer = new ServerSocket(5678);
Socket connectedSocket = SocketServer.accept();
ObjectInputStream ois = new ObjectInputStream(connectedSocket.getInputStream());
Data = ois.readDouble();
DataText.setText("" + Data);
//ObjectOutputStream oos = new ObjectOutputStream(connectedSocket.getOutputStream());
//oos.writeInt(1);
//ois.close();
//oos.close();
} catch (Exception e) {
Log.e("Server run: ", e.toString());
}
}
}
}
by the way , the both code is inner class , and INTERNET permission is added.
顺便说一下,这两个代码都是内部类,并且添加了INTERNET权限。
回答by nKn
It's obvious it's not a router-firewall related problem as you are under the same net, so there are only a few possibilities:
很明显,这不是路由器防火墙相关的问题,因为您在同一个网络下,所以只有几种可能性:
- There's nothing listening on that port on that IP on the server-side
- There's a local firewall on the server-side that is blocking that connection attempt
- You are not using WIFI so you're not under the same net.
- 在服务器端的那个 IP 上的那个端口上没有任何东西在监听
- 服务器端有一个本地防火墙阻止该连接尝试
- 您没有使用 WIFI,因此您不在同一个网络下。
You should make sure you can open that service some ther way, that would help you debugging where the culprit is. If you've already done this, I'd suggest using some debugging tool to trace TCP packets (I don't know either what kind of operating system you use on the destination machine; if it's some linux distribution, tcpdump
might help, in Win environments WireShark
works just good).
您应该确保可以以某种方式打开该服务,这将帮助您调试罪魁祸首。如果您已经这样做了,我建议您使用一些调试工具来跟踪 TCP 数据包(我不知道您在目标机器上使用的是哪种操作系统;如果它是一些 linux 发行版,tcpdump
可能会有所帮助,在 Win环境WireShark
很好)。
回答by user207421
This isn't a 'data transfer error'. This is a 'connection refused' error. It means the server you want to transfer the data to or from isn't running at the IP:port you specified.
这不是“数据传输错误”。这是“连接被拒绝”错误。这意味着您要向其传输数据或从中传输数据的服务器未在您指定的 IP:port 上运行。
回答by nerd
Try killing the adb service before you begin the connection. I had a similar problem and killing the adb service before the connection resolved the issue.
在开始连接之前尝试终止 adb 服务。我遇到了类似的问题,并在连接解决问题之前终止了 adb 服务。
回答by Silas
I had the same error. I simply used ServerSocket and it worked well.
我有同样的错误。我只是使用 ServerSocket 并且效果很好。
ServerSocket socket = new ServerSocket(8888);
ServerSocket socket = new ServerSocket(8888);