我正在开发一个连接到用Java编程的服务器的Android应用程序。当我在Windows上测试服务器应用程序时,服务器和客户端按照编程进行通信。
现在我已将实验转移到Linux Ubuntu OS。 Android应用程序(客户端)能够发送字符串但是当Linux上的服务器尝试将字符串发送到android时它会失败。这需要更长的时间并且会出现此错误:
Testing.NewJFrame$1 run
SEVERE: null
java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at Testing.NewJFrame$1.run(NewJFrame.java:178)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
以下是服务器端的代码:
Socket clientSocket = null;
ServerSocket serverSocket = null;
String clientIP;
int port = 4447;
并根据某些条件触发以下内容:
System.out.println("Attempting connect to "+clientIP+" through "+ port);
Socket socket = new Socket(clientIP, port);
System.out.println("Connection made");
OutputStream output = socket.getOutputStream();
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output)), true);
out.println(appName + " has been installed and launched in Background");
System.out.println("Message sent...");
但它停在了第34行;试图连接......&#34;
以下是客户端的代码:
public void listenToServer(){
new Thread (new Runnable(){
@Override
public void run() {
try {
serverSocket = new ServerSocket(4447);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
client = serverSocket.accept();
Scanner in = new Scanner(client.getInputStream());
.
.
.
.
.
请注意,这两个代码在Windows中都可正常运行,但在Linux Ubuntu上无效!
答案 0 :(得分:1)
我想分享我几周前发现的答案,以防万一有人感兴趣。
我发现问题背后的原因是我正在使用的网络配置。我在大学办公室工作,通过允许这种p2p通信向本地计算机发送消息而不是相反的方式来应用某种程度的安全性。所以我能够从android发送消息到服务器,这基本上是我的桌面。后者通过隔离网络连接,以确保一定的安全水平。
很抱歉回来晚了,谢谢你们分享他们观点的人