Android和Linux Server之间的TCP连接

时间:2011-03-15 10:01:45

标签: android tcp

我正在编写一个代码,需要每秒将数据从Android手机发送到台式计算机(Linux服务器)。由于数据经常发送,这不能通过Http命中实现(因为它消耗时间),Tcp通信似乎是更好的选择,因为来自android手机的数据可以通过此套接字编程非常快速地发送。 Android手机上的客户端代码为:

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

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


public class GetWebPage extends Activity {

    //Handler h;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final EditText eText = (EditText) findViewById(R.id.address);
        final TextView tView = (TextView) findViewById(R.id.pagetext);

        final Button button = (Button) findViewById(R.id.ButtonGo);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                try {
                        Log.v("Tcp","Clicked the button");
                           InetAddress serveraddress=InetAddress.getByName("67.23.14.156");
                           Log.v("Tcp", "Got the InetAddress");
                        Socket s = new Socket(serveraddress,4447);
                        Log.v("Tcp","Got the Socket address");
                        OutputStream out = s.getOutputStream();
                        PrintWriter output = new PrintWriter(out);
                        output.println("Hello Android!");
                        out.close();

                } catch (UnknownHostException e) {
                    tView.setText(e.toString());
                    Log.v("Tcp",e.toString());
                } catch (IOException e) {
                    tView.setText(e.toString());
                    Log.v("Tcp",e.toString());
                }catch (Exception e) {
                    tView.setText(e.toString());

                } 
            }
        });        
    }
}

服务器端代码是:

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

public class ListenIncomingTcpConnection {

    public static void main(String[] args) {
        ServerSocket serverSocket=null;
        Socket  client=null;
        try {
            System.out.println("Creating the server object...");
            serverSocket = new ServerSocket(4447);
            System.out.println("Waiting for the connection...");
        } catch (IOException e1) {
            System.out.println(e1);

        }

        while (true) {

            try {
                client = serverSocket.accept();
                System.out.println("Reading the content...");
            } catch (IOException e1) {
                System.out.println(e1);
                e1.printStackTrace();
            }
            try {
                BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                String str = in.readLine();
                System.out.println("Reading the content.....");
            } catch(Exception e) {
                System.out.println(e);
            } finally {
                try{
                    client.close();
                }catch(Exception e){
                    System.out.println(e);
                }
            }
        }//while 
    }//PSVM

}

清单文件的代码是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.spce" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="GetWebPage">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
</manifest>

我已经通过putty上的“java”命令在linux机器上执行了服务器代码。它在此行执行并停止“client = serverSocket.accept();” 当我在Android手机上执行客户端时,它说:

单击按钮 得到了InetAddress java.net.SocketException:没有到主机的路由

我无法找到这条无路由主持人的原因。

请帮助解决问题。

1 个答案:

答案 0 :(得分:6)

看起来你正在对IP地址进行DNS查找

serveraddress=InetAddress.getByName("67.23.14.156")

尝试直接使用IP地址。

new Socket("67.23.14.156",4447);