Java 如何在windows中使用Socket将消息从Android发送到python应用程序
我试图在windows中将消息从android应用程序发送到python应用程序,但它不起作用 Android应用程序运行良好,但在windows中使用python创建的客户端未启动。它显示错误:-回溯(最近一次调用): 文件“client.py”,第14行,在 s、 连接((socket.gethostname(),9876)) ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了它 这是我的client.py文件Java 如何在windows中使用Socket将消息从Android发送到python应用程序,java,python,android,sockets,Java,Python,Android,Sockets,我试图在windows中将消息从android应用程序发送到python应用程序,但它不起作用 Android应用程序运行良好,但在windows中使用python创建的客户端未启动。它显示错误:-回溯(最近一次调用): 文件“client.py”,第14行,在 s、 连接((socket.gethostname(),9876)) ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了它 这是我的client.py文件 #!/usr/
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import socket
# In[2]:
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(), 9876))
# In[ ]:
while True:
msg = s.recv(1024)
print(msg.decode("utf-8"))
这是我在android中的messageSender.java
package com.example.sockets;
import android.os.AsyncTask;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
public class MessegeSender extends AsyncTask<String,Void,Void> {
Socket socket;
DataOutputStream dataOutputStream;
PrintWriter printWriter;
@Override
protected Void doInBackground(String... strings) {
String message=strings[0];
try {
socket = new Socket("192.168.1.6",9876);
printWriter= new PrintWriter(socket.getOutputStream());
printWriter.write(message);
printWriter.flush();
printWriter.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
package com.example.sockets;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText) findViewById(R.id.msgBox);
}
public void sendMsg(View view)
{
Toast.makeText(this, "Function is running", Toast.LENGTH_SHORT).show();
MessegeSender messegeSender =new MessegeSender();
messegeSender.execute(editText.getText().toString());
}
}
我不确定确切的情况,但如果您试图通过internet连接,您可能需要查看此权限,或类似于此权限的权限 Android有一个安全层,除非使用应用程序的用户授予应用程序权限,否则不允许连接。查看此操作的权限。我的回答可能只是一个起点
<uses-permission android:name="android.permission.INTERNET" />
以下是一些资源:
如果您希望通过套接字将android应用程序连接到python,则在android中执行上述操作,但通过-
#Imports Modules
import socket
import time
listensocket = socket.socket()
Port = 9876
maxConnections = 2
IP = socket.gethostname() #Gets Hostname Of Current Macheine
listensocket.bind(('',Port))
#Opens Server
listensocket.listen(maxConnections)
print("Server started at " + IP + " on port " + str(Port))
#Accepts Incoming Connection
# (clientsocket, address) = listensocket.accept()
# print("New connection made!")
# running = True
#Main
while True:
(clientsocket, address) = listensocket.accept()
message = clientsocket.recv(1024).decode() #Receives Message
print(message) #Prints Message
# if not message == "":
# print(message) #Prints Message
#Closes Server If Message Is Nothing (Client Terminated)
# elif message =="":
# clientsocket.close()
# # running = False
那么,客户是什么?谁是服务器?你应该从那开始,好的。你可能想在帖子中包含这个,除非你找到了答案,希望你能得到这个!