return new BufferedReader(new InputStreamReader(socketIn));的作用
时间: 2023-05-23 15:06:16 浏览: 99
A:这行代码的作用是创建一个输入流读取器(BufferedReader),用于读取套接字(Socket)的输入流(socketIn)中的数据。具体步骤是:先使用InputStreamReader将字节流转换为字符流,再将字符流封装成BufferedReader,以便于逐行读取文本数据。最终返回的是一个BufferedReader对象,可以使用它的readLine()方法读取套接字输入流中的一行文本数据。
相关问题
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ipEditText = findViewById(R.id.ipEditText); portEditText = findViewById(R.id.portEditText); Button connectButton = findViewById(R.id.connectButton); connectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String ip = ipEditText.getText().toString(); int port = Integer.parseInt(portEditText.getText().toString()); new ConnectTask().execute(ip, String.valueOf(port)); } }); Button sendMessageButton = findViewById(R.id.sendMessageButton); sendMessageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showSendMessageDialog(); } }); } private class ConnectTask extends AsyncTask<String, Void, Void> { @Override protected Void doInBackground(String... params) { String ip = params[0]; int port = Integer.parseInt(params[1]); try { socket = new Socket(ip, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }加入一个滑块能向所连端口发送不同位置对应的数字
你可以在布局文件中添加一个SeekBar,然后在Java代码中获取其值并发送给所连接的端口。具体实现可以按照以下步骤进行:
1. 在布局文件中添加SeekBar组件:
```
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0" />
```
2. 在onCreate方法中获取SeekBar组件并为其设置监听器:
```
SeekBar seekBar = findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// 发送SeekBar的当前值给所连接的端口
out.println(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
```
3. 在ConnectTask类中,将out对象定义为成员变量,以便在SeekBar的监听器中使用:
```
private class ConnectTask extends AsyncTask<String, Void, Void> {
private PrintWriter out;
@Override
protected Void doInBackground(String... params) {
String ip = params[0];
int port = Integer.parseInt(params[1]);
try {
socket = new Socket(ip, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
```
import java.net. *; import java.io. *; public class EchoClient{ public static void main(String args[]){ try{ Socket connection = new Socket(args[0], 7); System.out.println("Connection established:"); DataInputStream in = new DataInputStream( connection.getInputStream()); DataOutputStream out = new DataOutputStream( connection.getoutputStream()); String line = new String(""); while(!line.toUpperCase().equals(".QUIT")){ System.out.print("Enter string: "); line = readString(); System.out.println("\tSending string to server... "); out.writeUTF(line); System.out.println("\tWaiting for server response... "); line = in.readUTF(); System.out.println("Received: " + line);} in.close(); out.close(); connection.close(); } catch(UnknownHostException uhe){ System.err.println("Unknon host: " + args[0]); } catch(IOException ioe) { System.err.println("IOException: " + ioe); } } public static String readString(){ String string = new String(); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); try{ string = in.readLine(); }catch(IOException e){ System. out. println("Console. readString: Unknon error..."); System.exit(-1); } return string;} }哪里错了
代码有一个小写字母错误,它导致了编译错误。第16行中的 `connection.getoutputStream()` 应该改为 `connection.getOutputStream()`。将该行代码更改为以下代码将解决问题:
```
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
```
除此之外,还有一些其他的小问题:
1. 在 `String` 对象初始化时,最好使用 `""` 而不是 `new String()`。
2. 在 `catch` 语句中,应该使用 `System.out.println()` 而不是 `System. out. println()`。
3. 在 `readString()` 方法中,有一个拼写错误,应该是 "unknown" 而不是 "unknon"。
4. 最后,建议在 `catch` 块中输出详细的错误信息,以便更好地调试问题。
修改后的代码如下所示:
```
import java.net.*;
import java.io.*;
public class EchoClient {
public static void main(String args[]) {
try {
Socket connection = new Socket(args[0], 7);
System.out.println("Connection established:");
DataInputStream in = new DataInputStream(connection.getInputStream());
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
String line = "";
while (!line.toUpperCase().equals(".QUIT")) {
System.out.print("Enter string: ");
line = readString();
System.out.println("\tSending string to server... ");
out.writeUTF(line);
System.out.println("\tWaiting for server response... ");
line = in.readUTF();
System.out.println("Received: " + line);
}
in.close();
out.close();
connection.close();
} catch (UnknownHostException uhe) {
System.err.println("Unknown host: " + args[0]);
} catch (IOException ioe) {
System.err.println("IOException: " + ioe.getMessage());
}
}
public static String readString() {
String string = "";
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
string = in.readLine();
} catch (IOException e) {
System.err.println("Console.readString: Unknown error...");
System.exit(-1);
}
return string;
}
}
```
希望这可以帮助您解决问题!
阅读全文