Java Android和PC套接字连接

Java Android和PC套接字连接,java,android,Java,Android,我在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 jav

我在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来代替代码中提到的端口和ip。。。怎么弄到的请帮帮我


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

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

因此,如果我要构建该应用程序,我将使用Ip地址10.0.0.129。使用9152到65535之间的任何端口。你可能会希望IP地址是静态的,这样当你测试你的应用程序时,它不会在你身上改变。按照本教程进行操作,这将允许您在本地网络上测试应用程序,而无需更改计算机的Ip地址

如果您想在本地网络之外使用此应用程序,则需要租用专用服务器、安装java web服务器或使用您的计算机作为服务器。要使用您的计算机,您需要一个静态ip地址或DNS服务,我使用为我的计算机分配一个主机名,以便我可以随时随地使用我的计算机(只要它已打开)。另外请注意,如果您选择使用计算机,则需要在路由器上设置端口转发。只需查找端口转发,您将发现大量教程


祝你好运。

将你的设备连接到同一个网络,然后它就会工作。
简单的方法是:启用你设备的热点,并将你的电脑连接到这个(热点)网络上。
别忘了更改IP。

应该在“本地网络”之外,而不是“localhost.10.0.2.2用于联系开发机器的特殊地址仅在模拟器上可用,而在物理android设备上不可用。此外,这个问题应该被关闭,因为它是许多其他问题的重复。它给出了什么连接错误?