Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连接手机和电脑android_Android_Sockets - Fatal编程技术网

连接手机和电脑android

连接手机和电脑android,android,sockets,Android,Sockets,所以,我想制作一个像遥控器一样工作的android应用程序,这样我就可以在我的电脑上从我的android应用程序执行命令。到目前为止,我已经查看了套接字框架,如果服务器和客户端在同一台计算机上,它可以正常工作,但是如果我将客户端移动到我的手机上,我会发现错误,主机找不到。我很确定这是关于我如何连接的问题,但我不知道如何解决它。如果它们在同一个wifi网络上,是否可以通过插座从手机连接到我的电脑 编辑: 对不起,我以为我已经链接了。我只是想让java教程在这个主题上发挥作用 客户端:knockcl

所以,我想制作一个像遥控器一样工作的android应用程序,这样我就可以在我的电脑上从我的android应用程序执行命令。到目前为止,我已经查看了套接字框架,如果服务器和客户端在同一台计算机上,它可以正常工作,但是如果我将客户端移动到我的手机上,我会发现错误,主机找不到。我很确定这是关于我如何连接的问题,但我不知道如何解决它。如果它们在同一个wifi网络上,是否可以通过插座从手机连接到我的电脑

编辑: 对不起,我以为我已经链接了。我只是想让java教程在这个主题上发挥作用

客户端:knockclient.java 服务器:knockServer.java
我遵循了教程。

有,但正如您所说,这是关于我如何连接的
,请发布您的代码,以便您可以得到帮助。在这里,很抱歉!你的安卓手机知道什么是ip“taranis”吗?不,我没有放任何关于ip的信息,因为我不知道怎么放。
public class KnockKnockClient {
    public static void main(String[] args) throws IOException {

        Socket kkSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            kkSocket = new Socket("taranis", 4444);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: taranis.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer;
        String fromUser;

        while ((fromServer = in.readLine()) != null) {
            System.out.println("Server: " + fromServer);
            if (fromServer.equals("Bye."))
                break;

            fromUser = stdIn.readLine();
        if (fromUser != null) {
                System.out.println("Client: " + fromUser);
                out.println(fromUser);
        }
        }

        out.close();
        in.close();
        stdIn.close();
        kkSocket.close();
    }
}
public class KnockKnockServer {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                clientSocket.getInputStream()));
        String inputLine, outputLine;
        KnockKnockProtocol kkp = new KnockKnockProtocol();

        outputLine = kkp.processInput(null);
        out.println(outputLine);

        while ((inputLine = in.readLine()) != null) {
             outputLine = kkp.processInput(inputLine);
             out.println(outputLine);
             if (outputLine.equals("Bye."))
                break;
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}