python—如何在java中通过socket发送数据

oxiaedzo  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(379)

我正在制作一个简单的android应用程序来向服务器发送字符串(用python创建)。我使用单独线程中的套接字连接到服务器,如代码所示。我想在点击按钮时发送字符串。这是java代码:

public class MainActivity extends AppCompatActivity {

private Socket socket;
Button btn;

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

    new Thread(new ClientThread()).start();

    btn = (Button) findViewById(R.id.button);
    if (socket != null){
        try {
            OutputStream output = socket.getOutputStream();
            PrintWriter writer = new PrintWriter(output, true);
            writer.println("1");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class ClientThread implements Runnable{
    @Override
    public void run() {
        try {
            socket = new Socket("192.168.0.12", 80);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这是我的python服务器

import socket

host = socket.gethostbyname(socket.gethostname())
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, 80))

print("ip = ", host)

s.listen(1)
print("Listening")

while True:
    conn, addr = s.accept()
    print(addr, " connected to us")
    conn.sendall(bytes("Hello", "utf-8"))
    while True:
        try:
            data = bytes.decode(conn.recv(1024), "utf-8")
            print(data)
        except:
            break

这是服务器输出

ip =  192.168.0.12
Listening
('192.168.0.6', 45903)  connected to us

请给我发送数据到服务器的建议

qni6mghb

qni6mghb1#

Socket s = new Socket("address",port);
    OutputStream os = s.getOutputStream();
    //do write
    os.close();
    s.shutdownOutput();

下次要使用套接字时,请重新创建并连接。

s.shutdownOutput()

当您调用此方法时,它不会将数据写入服务器;

weylhg0b

weylhg0b2#

经过这么多的研究,我找到了解决问题的办法。我在这里分享我的代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button);
    btn2 = (Button) findViewById(R.id.button2);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Thread(new UPThread()).start();
        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Thread(new DOWNThread()).start();
        }
    });

}
private PrintWriter output;
private BufferedReader input;
class UPThread implements Runnable{
    @Override
    public void run() {
        try {
            socket = new Socket("192.168.43.235", 80);
            output = new PrintWriter(socket.getOutputStream());
            output.write("UP");
            output.flush();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class DOWNThread implements Runnable{
    @Override
    public void run() {
        try{
            socket = new Socket("192.168.43.235", 80);
            output = new PrintWriter(socket.getOutputStream());
            output.write("DOWN");
            output.flush();
            socket.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

我运行按钮点击线程,这样我就可以很容易地发送简单的指示我的物联网项目

相关问题