1

我有一个简单的 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");
            }
        }
    }
}

这是我的android客户端代码:

// 导入等

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();
        }
    }
}
4

1 回答 1

0

Thanks to @zapl, check the IP address of the PC. Had 192.168.56.1 when the correct address was 192.168.1.56.

于 2012-11-25T22:31:34.347 回答