我最近想要发送请求到测试服务器(托管在同一本地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