我正試圖在java和android中實現一個非常簡單的客戶端/服務器應用程序。服務器似乎運行良好,當我執行它,Android客戶端打開我的手機。但是,當我嘗試通過WiFi連接向服務器發送消息時,它不起作用。Android客戶端無法連接到Java服務器
這裏是我的服務器代碼:
package com.SentientShadow;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
Thread thread = new Thread(){
public void run() {
System.out.println("Server has started and is listening...");
try{
ServerSocket socket = new ServerSocket(6879);
while (true){
Socket connection = socket.accept();
DataInputStream input = new DataInputStream(connection.getInputStream());
System.out.println("Received from client: " + input.readUTF());
input.close();
connection.close();
}
}catch(IOException e){
System.out.println("problem accepting connection");
}
}
};
thread.start();
}
}
這裏是我的客戶端活動代碼:
package com.SentientShadow;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Bundle;
public class Client extends ActionBarActivity implements OnClickListener {
private EditText etMessage;
private Button bSend;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
etMessage = (EditText)findViewById(R.id.etMessage);
bSend = (Button)findViewById(R.id.bSend);
bSend.setOnClickListener(this);
}
public void onClick(View view) {
Thread thread = new Thread(){
public void run() {
try {
Socket connection = new Socket("127.0.0.1", 6789);
DataOutputStream output = new DataOutputStream(connection.getOutputStream());
output.writeUTF(etMessage.getText().toString());
output.flush();
output.close();
connection.close();
} catch (UnknownHostException e) {
System.out.println("problem connecting to specified address");
} catch (IOException e) {
System.out.println("problem connecting to port");
}
}
};
thread.start();
Toast.makeText(this, "Message has been sent!", Toast.LENGTH_SHORT).show();
}
}
下面是客戶端活動XML佈局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.SentientShadow.Client" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter message:" />
<EditText
android:id="@+id/etMessage"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="14dp"
android:ems="10" />
<Button
android:id="@+id/bSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:text="Send" />
</RelativeLayout>
這是我的代碼爲xml清單文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.SentientShadow"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="Client"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
我用System.out.println消息替換了IOException和UnknownHostException printstacktrace,以便我可以確定問題是否來自其中任何一個錯誤。但是,控制檯沒有打印任何內容。
我還使用端口掃描儀掃描了幾個端口,似乎我的學校網絡已經阻止了所有這些端口,因爲它們返回時關閉。但是,我早先製作了另一個客戶端/服務器應用程序,其中客戶端和服務器都是純粹用java編碼的桌面應用程序。在這種情況下,我使用localhost(127.0.0.1)作爲IP地址,並嘗試使用多個端口的應用程序,並且它工作正常。我也嘗試與我的連接的公共IP地址的應用程序,它的工作。
因此,我開始認爲這不是連接到端口的問題,而是應用程序的Android客戶端問題。但我無法弄清楚什麼是錯的。
順便說一下,當我嘗試發送消息時,運行客戶端的手機和運行服務器的筆記本電腦都連接到同一網絡。
您不能使用127.0.0.1,因爲您將用於連接到同一設備上的服務器。您應該使用服務器pc的本地ip地址。經常像192.168.0.23。如果使用ipconfig從命令行找到運行的窗口。 – greenapps 2014-09-24 19:04:34
是的,我試着輸入本地和公共IP。但是,都沒有工作。 – SentientShadow 2014-09-25 05:49:20
防火牆沒有防止這種情況發生? – greenapps 2014-09-25 06:27:43