Android和PC Socket连接
我用Android设备Android和PC Socket连接
/*
* 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模拟器到Java应用程序,但它给连接error.Which端口和IP我应该使用,而不是代码中提到的...以及如何得到,请帮助我
,我如何修改此代码发送从android到PC的移动联系人?
您的电脑的主机地址可能有误,如果您正在运行Windows计算机转到您的开始菜单并在搜索框中输入“cmd”,您应该看到一个黑匣子弹出框,输入“ipconfig”
所以,如果我还是被栋该应用我会使用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/为我的计算机分配一个主机名,以便随时随地使用我的电脑(只要它打开)。另请注意,如果您选择使用计算机,则需要在路由器上设置端口转发。只需查看端口转发,你会发现大量的教程。
祝你好运。
它不工作 – 2013-04-30 17:45:07
这应该是“本地网络”,而不是“本地主机” – 2013-04-30 18:20:20
好抓克里斯,感谢澄清 – 2013-04-30 18:51:04
连接你的设备与同一网络,然后它应该工作。
简单的方法是:启用设备的热点并在此(热点)网络上连接您的PC。
不要忘记更改IP。
联系开发机器的特殊10.0.2.2地址只适用于模拟器,不适用于物理的android设备。此外,这个问题应该是封闭的,因为它与众多其他问题相同。 – 2013-04-30 18:21:31
它给*什么*连接错误? – EJP 2017-09-24 04:24:16