[英]How to send data through socket in java
I am making a simple android app to send string to server(created in python).我正在制作一个简单的 android 应用程序来将字符串发送到服务器(在 python 中创建)。 I am connecting to server using socket in separate thread as Shown in code.
我正在使用单独线程中的套接字连接到服务器,如代码所示。 I want to send string on button click.
我想在按钮单击时发送字符串。 This is java code:
这是 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();
}
}
}
And this my python server这是我的 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
and this is server output这是服务器 output
ip = 192.168.0.12
Listening
('192.168.0.6', 45903) connected to us
Please give me suggestion to send data to server请给我建议将数据发送到服务器
Socket s = new Socket("address",port);
OutputStream os = s.getOutputStream();
//do write
os.close();
s.shutdownOutput();
When you want to use the socket next time,ReCreate it and connect.下次要使用socket时,重新创建并连接。
s.shutdownOutput()
This method do write data to server when you invoked it;此方法在您调用它时会向服务器写入数据;
After so much research I found solution for my problem.经过这么多的研究,我找到了解决我的问题的方法。 Here I am sharing my code
在这里我分享我的代码
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();
}
}
}
I run the thread on button click so it will be easy for me to send simple instruction to my IOT project我在按钮单击时运行线程,因此我可以轻松地向我的 IOT 项目发送简单的指令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.