Android和PC Socket连接 [英] Android and PC Socket connection

查看:126
本文介绍了Android和PC Socket连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I used the following code as Client side on android device

/*
 * 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项目

and following as a server side as simple java project

/*
 * 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应用程序,但它给出了连接错误。我应该使用哪个端口和ip而不是代码中提到的...以及如何获取它请帮助我

i used simple button to send string from Android Emulator to java application but it's giving connection error.Which port and ip i should use instead of that mentioned in the code... and how to get that please help me

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

and how can i modify this code to send mobile contacts from android to PC??

推荐答案

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

You probably have the host address wrong for your computer, If you are running a windows machine goto your start menu and in the search box type "cmd", you should see a black box popup, type in "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地址更改的情况下在本地网络上测试您的应用程序。

So if i was to be buliding that app I would use the Ip address 10.0.0.129. Use any port from 9152 to 65535. You will probably want to make the IP address static so it doesnt change around on you while you test your app. Follow this tutorial to assist you http://www.howtogeek.com/howto/19249/how-to-assign-a-static-ip-address-in-xp-vista-or-windows-7/ This will allow you to test on your app on local network without your computers Ip address changing.

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

If you want to use this app outside of your local network you will need to either rent a dedicated server, setup a java web server or use your machine as the server. To use your machine you will need a static ip address or a DNS service, I use http://dyn.com/dns/ to assign a host name to my computer so that I can use my computer anytime from anywhere (as long as its turned on). Also note that if you do choose to use your computer you will need to setup port forwarding on your router. Just look up port forwarding and you will find tons of tutorials.

祝您好运。

这篇关于Android和PC Socket连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆