套接字服务器控制器 Android 应用程序,套接字错误

Socket Server controller Android App, Socket error

所以基本上我想为我的 pc 创建某种 控制器,在 pc 上运行 java 套接字服务器。服务器正常,我试过了。另外,路由器中的端口转发是可以的。 源代码在这里 :

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
    public static final String ANSI_RESET = "\u001B[0m";
    public static final String ANSI_GREEN = "\u001B[32m";
    public static final String ANSI_BOLD = "3[1m";
    public static final String ANSI_RED = "\u001B[31m";

    public static void main(String[] args) throws IOException {

        // Creating server instance
        ServerSocket serverSocket = new ServerSocket(2000);
        Socket socket = serverSocket.accept();

        System.out.println(ANSI_GREEN + "Controller connected" + ANSI_RESET);

        // Input Output streams
        DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());

        String command = "";

        do {

            command = dataInputStream.readUTF();

            switch (command.trim()){
                case "volume up":
                    Runtime.getRuntime().exec("cmd /c start src\sk\dzurik\main\volup.bat");
                    break;
                case "volume down":
                    Runtime.getRuntime().exec("cmd /c start src\sk\dzurik\main\voldown.bat");
                    break;
                default:
                    // Unknown command
                    break;
            }



        }while (!command.equals("stop"));

        socket.close();

        System.out.println(ANSI_RED + "Controller disconnected" + ANSI_RESET);

    }

}

它应该 与 android 应用 交互,但我收到 错误 但我不能很清楚为什么会这样,这是 ActivityMain 的源代码:

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
    private boolean connected;
    private DataOutputStream dataOutputStream;
    private Socket socket;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Pripájanie na server

        try {
            socket = new Socket("172.0.0.1",2000);
            dataOutputStream = new DataOutputStream(socket.getOutputStream());

        } catch (IOException e) {
            Toast.makeText(MainActivity.this, "Not Connected!",
                    Toast.LENGTH_LONG).show();
        }


        Button VolumeUp = (Button) findViewById(R.id.VolumeUpButton);
        VolumeUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    send("volume up");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        Button VolumeDown = (Button) findViewById(R.id.VolumeDownButton);
        VolumeDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    send("volume down");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        Button DisconnectButton = (Button) findViewById(R.id.DisconnectButton);
        DisconnectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    send("stop");

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }


    public void send(String command) throws IOException {
        if (socket != null){
            dataOutputStream.writeUTF(command);
        }
        else Toast.makeText(MainActivity.this, "Socket is not defined",
                Toast.LENGTH_SHORT).show();
    }

    public void disconnect() throws IOException {
        if (socket != null){
            socket.close();
        }
        else Toast.makeText(MainActivity.this, "Socket is not defined",
                Toast.LENGTH_SHORT).show();
    }

}

所以如果你能帮助我,我将不胜感激,<3

好吧,这样做的原因很愚蠢。当我从 Stream 加载数据时,它说 nextLine 这意味着结束点是 \n 所以我永远无法得到那条线,因为我没有把它放在那里。