我最近想要發送請求到測試服務器(託管在同一本地wifi上),並在Android應用程序開發中探索教育。Android套接字連接到計算機上的服務器不連接
我所創建的,到目前爲止是在我的筆記本電腦小迅捷服務器:
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception
{
System.out.println("Starting...");
ServerSocket server = new ServerSocket(1234);
Socket socket = server.accept();
loop(socket);
}
public static void loop(Socket socket) throws Exception
{
Scanner socketScanner = new Scanner(socket.getInputStream());
int num = socketScanner.nextInt();
System.out.println("Received: " + num);
num *= 2;
PrintStream out = new PrintStream(socket.getOutputStream());
out.println(num);
loop(socket);
}
}
測試客戶端(在筆記本電腦上也一樣)的偉大工程:
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception
{
Socket socket = new Socket("localhost",1234);
loop(socket);
}
public static void loop(Socket socket) throws Exception
{
Scanner scanner = new Scanner(System.in);
Scanner socketScanner = new Scanner(socket.getInputStream());
System.out.println("Input number:");
int num = scanner.nextInt();
PrintStream p = new PrintStream(socket.getOutputStream());
p.println(num);
int response = socketScanner.nextInt();
System.out.println("Received: " + response);
loop(socket);
}
}
但後來我嘗試從Android Studio連接到Android模擬器,並在運行請求之後顯示一條消息(不幸的是)應用程序已停止。
是的,我已經在用戶權限添加允許連接?
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.artish1.testapplication" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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>
最後的實際代碼正在運行:
public static TextView status;
public static EditText ip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (TextView)findViewById(R.id.statusText);
ip = (EditText)findViewById(R.id.ipText);
Log.i(TAG, "Application: onCreate!");
Button button = (Button)findViewById(R.id.payBenButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
status.setText("Sending...");
Log.i(TAG,"Starting socket sending... to: localhost");
Socket socket = new Socket("localhost",1234);
Log.i(TAG, "Connected.");
int num = Integer.parseInt(((EditText) findViewById(R.id.numberText)).getText().toString());
PrintStream out = new PrintStream(socket.getOutputStream());
out.println(num);
Log.i(TAG, "Sending: " + num);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String response = reader.readLine();
status.setText("Received: " + response);
Log.i(TAG, "Received: " + response);
}catch(SocketException e)
{
Log.i(TAG,e.getMessage());
}catch(IOException e)
{
Log.i(TAG,e.getMessage());
}
}
});
}
我花了好3-4小時擺弄周圍,但我只是不明白的問題是什麼?
雙方都在等待輸入。首先必須發送 – user210504
服務器正在等待首先發送回來的東西,但客戶端先發送然後接收回來的東西。我看不出兩個人如何等待同時接收?那麼這將是什麼? – Artish1