简体   繁体   English

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

[英]Socket Server controller Android App, Socket error

So basically I want to create some kind of controller for my pc , where on pc, runs java socket server.所以基本上我想为我的电脑创建某种 controller ,在电脑上运行 java 套接字服务器。 The server is working, I have tried it.服务器正常,我试过了。 Also, Port Forwarding in the router is allright.另外,路由器中的端口转发是可以的。 Source code is here :源代码在这里

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 = "\033[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);

    }

}

And it is supposed to interact with the android app , but I got an error and I can't quite figure out why is that so, here is the source code for ActivityMain :它应该与 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();
    }

}

So if you could help me with it, I would be thankful, <3所以,如果你能帮助我,我将不胜感激,<3

Well the reason for that was silly.好吧,这样做的原因很愚蠢。 When I'm loading data from Stream it says nextLine which means the ending point is \n so I could never get that line because I haven't put it there.当我从 Stream 加载数据时,它说 nextLine 这意味着结束点是 \n 所以我永远无法得到那条线,因为我没有把它放在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM