简体   繁体   English

android open socket在设备上崩溃,可在AVD上运行

[英]android open socket crashing on device, works on AVD

i try creating client-server app using socket. 我尝试使用套接字创建客户端服务器应用程序。 i already succeed doing that with the AVD and run both server and client on my pc machine. 我已经成功使用AVD做到了这一点,并在我的PC机上同时运行服务器和客户端。 but when i try make it work in same Wifi network on my device, the app just crash. 但是当我尝试使其在设备上的同一Wifi网络中运行时,该应用程序便崩溃了。

yes, i'm using seperate thread for the connection and already added the use of Internet to the manifest. 是的,我正在使用单独的线程进行连接,并且已经将Internet的使用添加到清单中。

here is some code... 这是一些代码...

the client thread: 客户端线程:

package com.mainlauncher;

import java.io.*;
import java.net.*;

public class ConnectionThread extends Thread {

private static final int SERVERPORT = 7777;
private static final String SERVERADDRESS = "My-PC";
private Socket socket;
private DataInputStream in;
private DataOutputStream out;

@Override
public void run() {
    super.run();
    open();
    close();
}

void open(){
    try{
        socket = new Socket(SERVERADDRESS,SERVERPORT);
        in = new DataInputStream(socket.getInputStream());
        out = new DataOutputStream(socket.getOutputStream());
    }
    catch(IOException e){}
}

void close(){
    try {
        if(in!=null)
            in.close();
        if(out!=null)
            out.close();
        if(socket!=null)
            socket.close();
    } 
    catch (IOException e) {}
    socket=null;

}


}

the Server side main: 服务器端主要:

import java.io.IOException;
import java.net.*;


public class Main {

public static void main(String[] args) {
    int port = 7777;
    new Main().handleClients(port);
}

private void handleClients(int port) {
    ServerSocket serverSocket = null;
    try{
        serverSocket = new ServerSocket(port);
        System.out.println("Server is ready...");
        for(int i=1; ; i++){
            Socket socket = serverSocket.accept();
            ServerThread thread = new ServerThread(i,socket);
            System.out.println(i + " Connected");
            thread.run();
        }
    }
    catch (Exception e){
        System.err.println(e.getMessage());
    }
    finally{
        if(serverSocket != null){
            try{ serverSocket.close(); }
            catch(IOException x){}
        }

    }


}

}

and the ServerThread: 和ServerThread:

import java.io.*;
import java.net.*;


public class ServerThread extends Thread {

private int serverIndex;
private Socket socket;
private DataOutputStream out;
private DataInputStream in;

public ServerThread (int serverIndex, Socket socket){
    this.serverIndex = serverIndex;
    this.socket = socket;
}

@Override
public void run() {
    super.run();

    try {
        in = new DataInputStream(socket.getInputStream());
        out = new DataOutputStream(socket.getOutputStream());
    } catch (IOException e) {
        System.out.println(serverIndex + " Disconnected");
    }

    finally{
        try {
            in.close();
            out.close();
            socket.close();
        } catch (IOException e) {}
    }
}
}

i tried looking for answers here \\ google etc... nothing helped. 我试图在这里寻找答案\\谷歌等...没有帮助。 there is no firewall or anything to block the incoming connection on my pc. 没有防火墙或任何东西来阻止我的电脑上的传入连接。

any ideas anyone? 有任何想法吗?

thanks, Lioz 谢谢,利兹

This won't immediately solve your problem, but you have a couple of bugs that are causing your program to throw away the evidence of the the real problem. 这不会立即解决您的问题,但是您会遇到一些错误,这些错误会导致您的程序丢弃真实问题的证据。

1) This simply squashed the exception, throwing away all evidence that it ever happened. 1)这只是消除了异常,扔掉了曾经发生过的所有证据。

    catch(IOException e){}

2) This is a bit better, but it only prints out the exception message ... not the stack trace. 2)更好一点,但是它只打印出异常消息,而不是堆栈跟踪。

    catch (Exception e){
        System.err.println(e.getMessage());
    }

The other problem with 2) is that it catches ALL exceptions ... not just IOExceptions. 2)的另一个问题是它捕获了所有异常……而不仅仅是IOExceptions。 That includes unchecked exceptions like NullPointerException , SecurityException and so on. 其中包括未经检查的异常,例如NullPointerExceptionSecurityException等。

Exceptions should be diagnosed like this: 应该这样诊断异常:

    catch (IOException e){
        e.printStackTrace();
    }

or logged. 或记录。


Finally, the way that you are handling requests is wrong: 最后,您处理请求的方式是错误的:

    Socket socket = serverSocket.accept();
    ServerThread thread = new ServerThread(i,socket);
    System.out.println(i + " Connected");
    thread.run();

The mistake is in the last statement. 错误是在最后一个语句中。 What this actually does is to simply call the thread object's run() method ... in the current thread. 这实际上是在当前线程中简单地调用thread对象的run()方法。 To run the run() method in a different thread you need to call the start method. 要在其他线程中run()方法,您需要调用start方法。 Like this: 像这样:

    Socket socket = serverSocket.accept();
    ServerThread thread = new ServerThread(i,socket);
    System.out.println(i + " Connected");
    thread.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM