简体   繁体   English

Java客户端和python服务器连接

[英]Java client and python server connection

i am working on an app in android studio and basiclly i need that whenever i clicked a specific button it will create a connection between the java client and python server. 我正在android studio中开发一个应用程序,基本上我需要每当我​​单击特定按钮时,它将在Java客户端和python服务器之间建立连接。
I first checked when u enter the page\\activity of the specific button if there is a wifi connection in the phone. 我首先检查了当您输入电话中是否存在wifi连接时,您进入特定按钮的页面\\活动。
It works fine. 工作正常。 then i tried to do this and it didnt work (Important to say that the current code make my phone and my app crash and stop) 然后我尝试执行此操作,但没有成功(重要的是,当前代码使我的手机和我的应用崩溃并停止)
simple server: 简单服务器:

HOST = '192.168.1.21'
PORT = 9000


def main():
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((HOST, PORT))
    server_socket.listen(10)
    client_socket, client_address = server_socket.accept()
    print 'Connect with ' + client_address[0]
    data = client_socket.recv(1024)
    print "data :"
    print data
    print " end"
    if data == HOST+"/n":
        print 'hi'
    else:
        i = 0
        while i < 15:
            print i
            i += 1
    client_socket.close()
    server_socket.close()
if __name__ == '__main__':
    main()

this is just to check a connection. 这只是检查连接。 it might look strange because of the host +/n in the if . 由于if中的主机+ / n,它可能看起来很奇怪。 i recently chenged it because i am new to java and dont know how the data is sent. 我最近发现它是因为我是java的新手,也不知道如何发送数据。 but that is not the problem rn. 但这不是问题。

public void ButtonClicked(View view) {
        EditText editText = findViewById(R.id.edit_text);
        final String ip = editText.getText().toString();
        Toast.makeText(this, ip, Toast.LENGTH_SHORT).show();
        try {
            InetAddress host = InetAddress.getByName(ip);
            final Client client = new Client(host, 9000);
            new Thread(new Runnable() {
                public void run() {
                    try {
                        client.send(ip);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            client.send(ip);
        }
        catch (IOException e) {
            System.out.println("Caught Exception: " + e.toString());
        }

i read that in android studio 3.0 u need to create a thread when u send data. 我读到在Android Studio 3.0中,您在发送数据时需要创建一个线程。 the client class that u see here : 您在这里看到的客户端类:

public class Client
{
    private Socket socket = null;
    private BufferedReader reader = null;
    private BufferedWriter writer = null;

    public Client(InetAddress address, int port) throws IOException
    {
        socket = new Socket(address, port);
        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    }

    public void send(String msg) throws IOException
    {
        writer.write(msg, 0, msg.length());
        writer.flush();
    }

    public String recv() throws IOException
    {
        return reader.readLine();
    }

}

it might be just a simple thing that i dont know of but those are the codes and i cant connect to the server. 这可能只是我不知道的简单事情,但是这些是代码,我无法连接到服务器。 i figured out that the server works fine because if im connecting from the phone to 192.168.1.21 on the net i receive the connection and it does thw little while. 我发现服务器工作正常,因为如果即时通讯从电话连接到网络上的192.168.1.21,我会收到连接,并且它的运行时间很少。 ty for the help - i would like to get the simplest fixes because im new to java. ty寻求帮助-我想获得最简单的修复方法,因为im是Java的新手。
(sorry if there where grammer\\ spelling mistakes) (很抱歉,如果那里有语法\\拼写错误)

Edit- logcat for the crash 崩溃的Edit-logcat
在此处输入图片说明

At the end of your log it says NetworkOnMainThread. 日志末尾显示“ NetworkOnMainThread”。 In your code there is a line client.send(ip) below your seperate thread which is executed at the main thread. 在您的代码中,在单独线程下方有一个client.send(ip)行,该行在主线程上执行。

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

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