java - Android与PC Socket连接

标签 java android

我在安卓设备上使用了以下代码作为客户端

/*
 * This is a simple Android mobile client
 * This application read any string message typed on the text field and
 * send it to the server when the Send button is pressed

 */
package lakj.comspace.simpleclient;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SimpleClientActivity extends Activity {

    private Socket client;
    private PrintWriter printwriter;
    private EditText textField;
    private Button button;
    private String messsage;

    @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) {

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

        try {

            client = new Socket("10.0.2.2", 4444);  //connect to server
             printwriter = new PrintWriter(client.getOutputStream(),true);
             printwriter.write(messsage);  //write the message to output stream

             printwriter.flush();
             printwriter.close();
             client.close();   //closing the connection

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

}
}

然后作为服务器端作为简单的 java 项目

/*
 * This is a simple server application
 * This server receive a string message from the Android mobile phone
 * and show it on the console.
 * Author by Lak J Comspace
 */
package simpleserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {

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(4444);  //Server socket

    } 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 client msg                    
            message = bufferedReader.readLine();

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

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

     }
}  

我使用简单的按钮将字符串从 Android Emulator 发送到 java 应用程序,但它给出了连接错误。我应该使用哪个端口和 ip 而不是代码中提到的......以及如何获得它,请帮助我

我如何修改此代码以将移动联系人从 android 发送到 PC??

最佳答案

您的计算机主机地址可能有误,如果您运行的是 Windows 机器,请转到开始菜单并在搜索框中键入“cmd”,您应该会看到一个黑框弹出窗口,键入“ipconfig”

enter image description here

因此,如果我要构建该应用程序,我会使用 IP 地址 10.0.0.129。使用从 9152 到 65535 的任何端口。您可能希望将 IP 地址设为静态,这样在您测试应用程序时它不会在您身上发生变化。按照本教程为您提供帮助 http://www.howtogeek.com/howto/19249/how-to-assign-a-static-ip-address-in-xp-vista-or-windows-7/这将允许您在本地网络上测试您的应用程序,而无需更改计算机的 IP 地址。

如果您想在本地网络之外使用此应用程序,您需要租用专用服务器、设置 Java Web 服务器或使用您的机器作为服务器。要使用您的机器,您需要一个静态 IP 地址或 DNS 服务,我使用 http://dyn.com/dns/为我的计算机分配一个主机名,以便我可以随时随地使用我的计算机(只要它打开)。另请注意,如果您确实选择使用计算机,则需要在路由器上设置端口转发。只需查找端口转发,您就会发现大量教程。

祝你好运。

关于java - Android与PC Socket连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16305121/

相关文章:

android - NetworkImageView 与 InfoWindowAdapter

android - 错误 :Execution failed for task ':app:transformClassesWithDexForDebug' - for multidex app

java - 在 CSS 中设置 JavaFX Anchor Pane 的 anchor

java - 泛型是否在编译时被编译器移除

java - JAVA 中 Streaming API 的最佳搜索算法

java - 如何仅在选择选项卡时加载操作栏选择选项卡的 fragment Activity oncreate() 方法?

Java 平台 - 目前有哪些可用?

java - Java 中的多线程排序动画

android - 如何在android应用程序中实现应用程序空闲超时?

android - Activity 的轻量级替代品