简体   繁体   中英

Android client app not connecting to a server on pc

I have the following problem... I am writing a system consisting of a Server side - on my pc and a Client side on my lovely Xperia (aka an Android phone). The problem is that when I attempt to bind my phone to the pc, through the client app, the logcat says:

  failed to connect to /*xxx.xxx.x.xxx (port 2002): connect failed: ETIMEDOUT (Connection timed out)

*here goes my ip address, it starts with 192...

Actually I use a try - catch on the client side, within the try I set the socket but the problem is that the process fails, I guess, to create the socket properly as it goes in the catch block... I am running both devices on my home Wi-Fi hotspot. As I said I use sockets, which probably means that the type of my connection is TCP (? :) ). Please suggest some kind of a solution to this because the more I continue reading in forums (here as well), the more I get confused.

A snipped of my code:

  public class ClientSide extends AsyncTask<String, Void, String>{

protected String doInBackground(String... params){

    final String SERVER_HOSTNAME = "xxx.xxx.x.xxx";
    final int SERVER_PORT = 2002;
    BufferedReader mSocketReader;
    PrintWriter mSocketWriter;
    final String TAG = ClientSide.class.getSimpleName();
    String data="";
    String outputln = "Me. Android";

    try {
        Socket socket = new Socket(SERVER_HOSTNAME, SERVER_PORT);
        mSocketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        mSocketWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
        System.out.println("Connected to server " + SERVER_HOSTNAME + ":" + SERVER_PORT);

        //Reads from the socket
        data = mSocketReader.readLine();

        //Writes to the socket, a.k.a. sends info
        mSocketWriter.println(outputln);
        mSocketWriter.flush();

    } catch (IOException ioe) {
        System.err.println("Cannot connect to " + SERVER_HOSTNAME + ":" + SERVER_PORT);
        ioe.printStackTrace();

    }

    return data;
}

Thank you in advance, if you need more info I will do my best to provide it. I am an extreme beginner in the Android dev, sorry for my English.

*Update: The server side app is written in java, too build of 6-7 classes, I don't run all that through Apache or any of these. I don't want to just test my app, I want a real connection over the wi-fi, not through the usb.

试试这个:打开控制面板 -> Windows Defender 防火墙 -> 允许应用程序或功能通过 Windows Defender 防火墙(左侧) -> 检查 Apache HTTP 服务器和 mysqld 的私有和公共网络👍👍👍

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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