android linux网络连接,Android和Linux服务器之间的TCP连接

我正在编写一个代码,需要每秒从Android移动设备向台式计算机(linux服务器)发送数据。由于数据经常发送,通过Http命中无法实现(因为会消耗时间),所以Tcp通信似乎是更好的选择,因为android手机的数据可以通过此套接字编程快速发送。 客户端的Android手机上的代码是:Android和Linux服务器之间的TCP连接

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

}

清单文件的代码是:

package="com.spce" android:versionCode="1" android:versionName="1.0">

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

单击该按钮 得到InetAddress是否 java.net.SocketException异常:没有到主机的路由

我不能够发现的这种情况的原因没有路由到主机。

请帮忙解决问题。

2011-03-15

Khushboo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值