日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

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

發(fā)布時間:2023/11/30 linux 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android linux网络连接,Android和Linux服务器之间的TCP连接 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我正在編寫一個代碼,需要每秒從Android移動設備向臺式計算機(linux服務器)發(fā)送數(shù)據(jù)。由于數(shù)據(jù)經(jīng)常發(fā)送,通過Http命中無法實現(xiàn)(因為會消耗時間),所以Tcp通信似乎是更好的選擇,因為android手機的數(shù)據(jù)可以通過此套接字編程快速發(fā)送。 客戶端的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">

我已經(jīng)執(zhí)行了服務器端的代碼在linux機器上通過putty上的“java”命令。它在此行執(zhí)行并停止“client = serverSocket.accept();” 當我執(zhí)行的Android手機客戶端,它說:

單擊該按鈕 得到InetAddress是否 java.net.SocketException異常:沒有到主機的路由

我不能夠發(fā)現(xiàn)的這種情況的原因沒有路由到主機。

請幫忙解決問題。

2011-03-15

Khushboo

總結(jié)

以上是生活随笔為你收集整理的android linux网络连接,Android和Linux服务器之间的TCP连接的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。