【问题标题】:Android phone communicating with PC: Phone can't find PCAndroid手机与PC通信:手机找不到PC
【发布时间】:2012-11-13 07:57:39
【问题描述】:

我有一个简单的 Java 服务器在我的笔记本电脑上运行,它打开一个 ServerSocket。一个简单的 android 客户端尝试连接到该服务器(使用我的笔记本电脑的 IP 地址和我在ServerSocket 中指定的端口),并向其发送一个字符串。客户端挂了:

client = new Socket(IP_ADDRESS, DEST_PORT);

我正在使用连接到笔记本电脑的三星 Galaxy S3。请注意,我尝试在 AsyncTask 中从客户端建立连接。

我一直在努力解决这个问题,感觉我错过了一些简单的东西。

所以问题是:如何让我的 android 手机识别/查看 PC?提前致谢!

这是我的 Java 服务器代码:

// 导入等

public class Main
{
    private static final int PORT = 4444;

    private static ServerSocket serverSocket;
    private static Socket clientSocket;
    private static InputStreamReader inputStreamReader;
    private static BufferedReader bufferedReader;
    private static String message;

    public static void main(String[] args)
    {
        try
        {
            serverSocket = new ServerSocket(PORT, 0, InetAddress.getLocalHost());

            System.out.println("IP:  " + serverSocket.getInetAddress() + "  Port:  " +  serverSocket.getLocalPort());

        } catch (IOException e)
        {
            System.out.println("Could not listen on port: 4444");
        }

        System.out.println("Server started. Listening to the port 4444");

        while (true)
        {
            try
            {
                clientSocket = serverSocket.accept(); // accept the client connection
                inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader); // get the client message
                message = bufferedReader.readLine();

                System.out.println(message);
                inputStreamReader.close();
                clientSocket.close();

            } catch (IOException ex)
            {
                System.out.println("Problem in message reading");
            }
        }
    }
}

这是我的安卓客户端代码:

// 导入等

public class SimpleClientActivity extends Activity
{
    private EditText textField;
    private Button button;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textField = (EditText) findViewById(R.id.editText1); // reference to the text field
        button = (Button) findViewById(R.id.button1); // reference to the send button

        // Button press event listener
        button.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v)
            {
                new ConnectToServerTask().execute(textField);
            }
        });
    }
}

这是我的 AsyncTask 代码:

// 导入等

public class ConnectToServerTask extends AsyncTask<View, Integer, Socket>
{
    private static final String IP_ADDRESS = "192.168.56.1";  // Toshiba laptop
    private static final int DEST_PORT = 4444;

    private EditText mTextField;

    /**
     * Store provided views (used later in onPostExecute(...)).
     * 
     * Create socket to communicate with server (blocking call).
     */
    protected Socket doInBackground(View... params)
    {
        // Store provided views.
        if (params.length != 1)
            throw new IllegalArgumentException();

        mTextField = (EditText) params[0];


        // Create socket.
        Socket client = null;

        try
        {
            client = new Socket(IP_ADDRESS, DEST_PORT); // connect to server
        } catch (UnknownHostException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        return client;
    }

    /**
     * Write to server.
     */
    protected void onPostExecute(Socket client)
    {
        try
        {
            PrintWriter printwriter;
            String messsage;

            messsage = mTextField.getText().toString(); // get the text message on the text field
            mTextField.setText(""); // Reset the text field to blank

            printwriter = new PrintWriter(client.getOutputStream(), true);
            printwriter.write(messsage); // write the message to output stream

            printwriter.flush();
            printwriter.close();

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

【问题讨论】:

  • 你有任何打印/异常吗?你能在你的服务器上的accept() 后面打印吗?
  • 检查笔记本电脑的防火墙。代码看起来应该可以工作
  • 同意 zapl - 确认任何防火墙软件都允许访问侦听器端口。
  • @Danpe,我在服务器中的accept() 之后打印了一个打印件,但什么也没有。我猜它永远不会连接。
  • @zapl,你就是那个男人!这是有趣的。错误的 IP 地址需要 5 小时。非常感谢大家的cmets!下次我会首先检查 IP 地址。

标签: java android tcp


【解决方案1】:

感谢@zapl,检查 PC 的 IP 地址。当正确的地址是 192.168.1.56 时有 192.168.56.1。

【讨论】:

    猜你喜欢
    • 2018-10-30
    • 2023-03-23
    • 1970-01-01
    • 2011-09-23
    • 2010-09-25
    • 1970-01-01
    • 2013-02-19
    • 2013-03-26
    • 1970-01-01
    相关资源
    最近更新 更多