我的目标是在 android studio 中设置一个服务器。我希望我的 android 应用程序使用套接字编程接收外部数据,所以我编写了下面显示的代码。我在学习 Android 应用程序的服务器 IP 时遇到问题。这就是连接被拒绝的原因。
android studio 中的这个服务器代码:
package com.tefrikatli.fatura.myapplicationas;
import android.net.wifi.WifiManager;
import android.view.View;
import android.widget.EditText;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Handler;
public class MyserverOdom implements Runnable{
ServerSocket ss;
Socket mysocket;
DataInputStream dis;
String message;
EditText dt;
@Override
public void run() {
try {
dt.findViewById(R.id.textView22);
ss = new ServerSocket(6000);
//ss.accept();
while (true) {
mysocket = ss.accept();
dis = new DataInputStream(mysocket.getInputStream());
message = dis.readUTF();
dt.setText(message.toString());
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
这是另一个文件:
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton manuelgec;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manuelgec =(ImageButton)findViewById(R.id.manuelTemizlik);
manuelgec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent gec = new Intent(MainActivity.this,manuel_temizlik.class);
startActivity(gec);
Thread myThread = new Thread(new MyserverOdom());
myThread.start();
}
});
}
}
wlp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.43.220 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::878d:6c3d:4ca4:4456 prefixlen 64 scopeid 0x20<link>
ether 54:8c:a0:32:32:f8 txqueuelen 1000 (Ethernet)
RX packets 647309 bytes 75238346 (75.2 MB)
RX errors 0 dropped 1086 overruns 0 frame 0
TX packets 681546 bytes 102623647 (102.6 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
mynet ip 是:192.168.43.220
我写 ifconfig :
客户端套接字是:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from math import degrees
import rospy
from nav_msgs.msg import Odometry
from nav_msgs.msg import OccupancyGrid
import cv2
import socket
global s
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.43.220', 6000)) #IP is the server IP
def odom_cb(msg):
seconds = rospy.get_time()
if (int(seconds) % 2 == 0):
try:
image = cv2.imread("/home/abdulsamet/catkin_ws/src/mobil/src/tubitak_v1.yaml.pgm")
den = image.shape
try:
xeksen = str(int(den[0]*11/20+(msg.pose.pose.position.x*19.2)))
yeksen = str((int(den[1]*11/20-msg.pose.pose.position.y*19.2)))
tam = xeksen + "," +yeksen
s.send(tam.encode())
except socket.error as msg:
print("de: ",msg)
except socket.error as msg:
print("Hata:",msg)
rospy.init_node('odom_to_path')
odom_sub = rospy.Subscriber('/odom', Odometry, odom_cb, queue_size=10)
rospy.spin()
显示此错误
<块引用>回溯(最近一次调用最后一次): 文件“/home/abdulsamet/catkin_ws/src/mobil/src/deneme.py”,第 12 行,在 s.connect(('192.168.43.220', 6000)) #IP是服务器IP ConnectionRefusedError: [Errno 111] 连接被拒绝
如何将 android 应用程序与我的 python 脚本连接,有人可以帮助我关于这个问题我应该查看哪些主题。