Android Socket.Inputstream 不响应任何数据发送
我在套接字模块中的输入流有问题
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
连接已建立,输出流也工作正常。
由于某些荒谬的原因,我无法通过“InputStream”获得任何响应,我花了三天时间思考这个问题,但无法了解问题的角度。
尝试使用扫描仪,但没有运气。
在客户端使用 ip = 10.0.2.2 和端口 8080 服务器端的 ip = localhost 和端口 8080
我在 Android Studio Bubblebee | 2021.1.1 补丁 2 并将最低 Android 版本设置为 Oreo(27)。
ClientSocketAPK
MainActivity.java
package com.example.testsocket2;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
@SuppressLint("SetTextI18n")
public class MainActivity extends AppCompatActivity {
Thread Thread1 = null;
EditText etIP, etPort;
TextView tvMessages;
EditText etMessage;
Button btnSend;
String SERVER_IP;
int SERVER_PORT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etIP = findViewById(R.id.etIP);
etPort = findViewById(R.id.etPort);
tvMessages = findViewById(R.id.tvMessages);
etMessage = findViewById(R.id.etMessage);
btnSend = findViewById(R.id.btnSend);
Button btnConnect = findViewById(R.id.btnConnect);
btnConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tvMessages.setText("");
SERVER_IP = etIP.getText().toString().trim();
SERVER_PORT = Integer.parseInt(etPort.getText().toString().trim());
Thread1 = new Thread(new Thread1());
Thread1.start();
}
});
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = etMessage.getText().toString().trim();
if (!message.isEmpty()) {
new Thread(new Thread3(message)).start();
}
}
});
}
private PrintWriter output;
private BufferedReader input;
class Thread1 implements Runnable {
public void run() {
Socket socket;
try {
socket = new Socket(SERVER_IP, SERVER_PORT);
output = new PrintWriter(socket.getOutputStream());
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
runOnUiThread(new Runnable() {
@Override
public void run() {
tvMessages.setText("Connected\n");
}
});
new Thread(new Thread2()).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Thread2 implements Runnable {
@Override
public void run() {
while (true) {
try {
final String message = input.readLine();
if (message != null) {
Log.i("Debug Info on Input","Right Here =>");
Log.i("Debug Info on Input", String.valueOf(message));
Log.i("Debug Info on Input","Ends Here =>");;
runOnUiThread(new Runnable() {
@Override
public void run() {
tvMessages.append("server: " + message + "\n");
}
});
} else {
Thread1 = new Thread(new Thread1());
Thread1.start();
return;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class Thread3 implements Runnable {
private String message;
Thread3(String message) {
this.message = message;
}
@Override
public void run() {
output.write(message);
output.flush();
Log.d("Debug Info on Output","Right Here =>");
Log.d("Debug Info on Output", String.valueOf(message));
Log.d("Debug Info on Output","Ends Here =>");;
runOnUiThread(new Runnable() {
@Override
public void run() {
tvMessages.append("client: " + message + "\n");
etMessage.setText("");
}
});
}
}
}
activity_main.xml
<?xml version = "1.0" encoding = "utf-8"?>
<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:layout_margin = "16dp"
tools:context = ".MainActivity">
<EditText
android:id = "@+id/etIP"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:hint = "IP-Address"
android:inputType = "text" />
<EditText
android:id = "@+id/etPort"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_below = "@+id/etIP"
android:hint = "Port No"
android:inputType = "number" />
<Button
android:id = "@+id/btnConnect"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_below = "@+id/etPort"
android:layout_gravity = "center"
android:layout_marginTop = "16dp"
android:text = "Connect To Server" />
<TextView
android:id = "@+id/tvMessages"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_above = "@+id/etMessage"
android:layout_below = "@+id/btnConnect"
android:inputType = "textMultiLine"
android:textAppearance = "@style/Base.TextAppearance.AppCompat.Medium" />
<EditText
android:id = "@+id/etMessage"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_above = "@+id/btnSend"
android:hint = "Enter Message"
android:inputType = "text" />
<Button
android:id = "@+id/btnSend"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_alignParentBottom = "true"
android:text = "SEND" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testsocket2">
<uses-permission android:name = "android.permission.INTERNET" />
<uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I am having problem with inputstream in the socket module
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Connection is established, the Outputstream is also working Fine.
for some absurd reason i'm not able to get any resposne through "InputStream", wrapping my head around this things for three days, but couldn't able get the angle of my problem.
Tried using scanner ,but no luck.
Using ip = 10.0.2.2 and port 8080 on the clientSide
And ip = localhost and port 8080 on the ServerSide
I am on Android Studio Bubblebee | 2021.1.1 Patch 2
and minimum android version set to Oreo(27).
ClientSocketAPK
MainActivity.java
package com.example.testsocket2;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
@SuppressLint("SetTextI18n")
public class MainActivity extends AppCompatActivity {
Thread Thread1 = null;
EditText etIP, etPort;
TextView tvMessages;
EditText etMessage;
Button btnSend;
String SERVER_IP;
int SERVER_PORT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etIP = findViewById(R.id.etIP);
etPort = findViewById(R.id.etPort);
tvMessages = findViewById(R.id.tvMessages);
etMessage = findViewById(R.id.etMessage);
btnSend = findViewById(R.id.btnSend);
Button btnConnect = findViewById(R.id.btnConnect);
btnConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tvMessages.setText("");
SERVER_IP = etIP.getText().toString().trim();
SERVER_PORT = Integer.parseInt(etPort.getText().toString().trim());
Thread1 = new Thread(new Thread1());
Thread1.start();
}
});
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = etMessage.getText().toString().trim();
if (!message.isEmpty()) {
new Thread(new Thread3(message)).start();
}
}
});
}
private PrintWriter output;
private BufferedReader input;
class Thread1 implements Runnable {
public void run() {
Socket socket;
try {
socket = new Socket(SERVER_IP, SERVER_PORT);
output = new PrintWriter(socket.getOutputStream());
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
runOnUiThread(new Runnable() {
@Override
public void run() {
tvMessages.setText("Connected\n");
}
});
new Thread(new Thread2()).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Thread2 implements Runnable {
@Override
public void run() {
while (true) {
try {
final String message = input.readLine();
if (message != null) {
Log.i("Debug Info on Input","Right Here =>");
Log.i("Debug Info on Input", String.valueOf(message));
Log.i("Debug Info on Input","Ends Here =>");;
runOnUiThread(new Runnable() {
@Override
public void run() {
tvMessages.append("server: " + message + "\n");
}
});
} else {
Thread1 = new Thread(new Thread1());
Thread1.start();
return;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class Thread3 implements Runnable {
private String message;
Thread3(String message) {
this.message = message;
}
@Override
public void run() {
output.write(message);
output.flush();
Log.d("Debug Info on Output","Right Here =>");
Log.d("Debug Info on Output", String.valueOf(message));
Log.d("Debug Info on Output","Ends Here =>");;
runOnUiThread(new Runnable() {
@Override
public void run() {
tvMessages.append("client: " + message + "\n");
etMessage.setText("");
}
});
}
}
}
activity_main.xml
<?xml version = "1.0" encoding = "utf-8"?>
<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:layout_margin = "16dp"
tools:context = ".MainActivity">
<EditText
android:id = "@+id/etIP"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:hint = "IP-Address"
android:inputType = "text" />
<EditText
android:id = "@+id/etPort"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_below = "@+id/etIP"
android:hint = "Port No"
android:inputType = "number" />
<Button
android:id = "@+id/btnConnect"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_below = "@+id/etPort"
android:layout_gravity = "center"
android:layout_marginTop = "16dp"
android:text = "Connect To Server" />
<TextView
android:id = "@+id/tvMessages"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_above = "@+id/etMessage"
android:layout_below = "@+id/btnConnect"
android:inputType = "textMultiLine"
android:textAppearance = "@style/Base.TextAppearance.AppCompat.Medium" />
<EditText
android:id = "@+id/etMessage"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_above = "@+id/btnSend"
android:hint = "Enter Message"
android:inputType = "text" />
<Button
android:id = "@+id/btnSend"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_alignParentBottom = "true"
android:text = "SEND" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testsocket2">
<uses-permission android:name = "android.permission.INTERNET" />
<uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论