TCP connection between Android and Linux Server
I am writing a code that needs to send data from Android mobile to desktop computer (linux server) every second. Since the data is send very often, this cannot be achieved via Http hit (as it consumes times), Tcp communication seems a better option as the data from android phone can be send very quickly through this socket programming. The code of client on Android phone is:
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());
}
}
});
}
}
The server side code is:
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
}
The code of manifest file is:
<?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>
I have executed the server code on the linux machine via "java" command on putty. It executed and stopped at this line "client = serverSocket.accept();" When I execute the client on android mobile, it says:
Clicked the button Got the InetAddress java.net.SocketException: No route to host
I am not able to spot the reason of this No route to host.
Please help in solving the problem.
Looks like you're doing a DNS lookup on an IP address
serveraddress=InetAddress.getByName("67.23.14.156")
Try to use the IP address directly.
new Socket("67.23.14.156",4447);
精彩评论