gpt4 book ai didi

java - 通过WiFi连接Java DatagramSocket的问题

转载 作者:太空宇宙 更新时间:2023-11-04 13:11:06 26 4
gpt4 key购买 nike

以下代码的目的是通过WiFi从Android应用程序向TI CC3200 Launchpad发送数据报包。
建立与启动板的连接时,我目前遇到问题。我尝试使用笔记本电脑和应用程序测试该过程,但是我的Wireshark没有显示任何数据包。
有什么建议为什么我的代码无法正常工作?

BR

编辑1:

IP是正确的。
我收到:绑定失败:EINVAL(无效参数)。

表现:

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<uses-feature android:name="android.hardware.wifi.direct"/>


编辑2:

堆栈跟踪:





I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

D / CustomFrequencyManagerService(751):releaseDVFSLockLocked:获取锁类型frm列表:DVFS_MIN_LIMIT频率:2457600 uid:1000 pid:751标签:ROTATION_BOOSTER @ 32

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

D / StatusBar.NetworkController(1036):刷新视图已连接= {wifi}级别= 3 CombinedSignalIconId = 0x7f020495 / com.android.systemui:drawable / stat_sys_wifi_signal_4 mobileLabel = bob wifiLabel =“ Kugelfahrzeug” xxxxXXXXugxxxxxXXXX EmergencyOnly = false CombinedLabel =“ K = false mDataActivity = 0 mPhoneSignalIconId = 0x7f020415 / com.android.systemui:drawable / stat_sys_signal_3_auto_rotate mQSPhoneSignalIconId = 0x7f020113 / com.android.systemui:drawable / ic_qs_signal_3 mDataDirectionIconId = 0x0 /(null).mDataSignalIconId = 0x0 /(null)。 stat_sys_signal_3_auto_rotate mDataTypeIconId = 0x7f0202ac / com.android.systemui:drawable / stat_sys_data_connected_3g mQSDataTypeIconId = 0x7f020114 / com.android.systemui:drawable / ic_qs_signal_3g mNoSimIconId = 0x0 / 7_sys_stat_wifi_snable_con_i_iableId = 0x0 /(n /com.android.systemui:drawable/ic_qs_wifi_4 mWifiActivityIconId = 0x7f020468 / com.android.systemui:drawable / s tat_sys_signal_no_inout mBluetoothTetherIconId = 0x1080907 / android:drawable / stat_sys_tether_bluetooth

D / StatusBar.NetworkController(1036):refreshSignalCluster-setNWBoosterIndicators(false)

D / StatusBar.NetworkController(1036):applyOpen

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

D / StatusBar.NetworkController(1036):refreshSignalCluster-setNWBoosterIndicators(false)

D / StatusBar.NetworkController(1036):applyOpen

D / StatusBar.NetworkController(1036):refreshSignalCluster-setNWBoosterIndicators(false)

D / StatusBar.NetworkController(1036):applyOpen

D / StatusBar.NetworkController(1036):refreshSignalCluster-setNWBoosterIndicators(false)

D / StatusBar.NetworkController(1036):applyOpen

D / TaskPersister(751):removeObsoleteFile:删除file = 24_task.xml

D / TaskPersister(751):removeObsoleteFile:删除文件= 24_task_thumbnail.png

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

I / art(751):显式并发标记清除GC已释放17486(1092KB)AllocSpace对象,5(1360KB)LOS对象,25%空闲,45MB / 61MB,已暂停1.720ms,总计181.936ms

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

I / WifiStateMachine(751):CMD_RSSI_POLL:进入calculateWifiScore!

I / AudioPolicyManager(287):getAudioPolicyConfig:audioParam; outDevice

V / AudioPolicyManager(287):getNewOutputDevice()选择的设备0

V / AudioPolicyManager(287):### curdevice:2

I / WifiStateMachine(751):CMD_RSSI_POLL:计算出Wifi得分!

I / WifiStateMachine(751):CMD_RSSI_POLL:退出!

编辑3:

问题位于主网络呼叫中(已修复

 new Thread(new Runnable() 
{
@Override
public void run()
{
(Code below)
}
});




应用程序现在可以正常运行了!

码:

    public void runJavaSocket() throws IOException 
{
try
{
DSocket = new DatagramSocket(null);
DSocket.bind(new InetSocketAddress("192.168.173.2", 4242));
DSocket.connect(new InetSocketAddress("192.168.173.1", 4242));
String send_x = Float.toString(round_x);
byte[] buffer = send_x.getBytes();
byte [] IP = {(byte) 192,(byte) 168,(byte) 173, (byte) 1};
InetAddress address = InetAddress.getByAddress(IP);
int serverSocket = 4242;
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, serverSocket);
DSocket.send(packet);
DSocket.close();
}
catch (Exception e)
{
DSocket.close();
}


}

最佳答案

尝试使用此代码,并确保您的两个设备在同一网络上工作:

        new Thread(new Runnable() {
@Override
public void run() {
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
InetAddress address = InetAddress.getByName("10.0.0.8");
byte[] command = "Hello".getBytes("UTF-8");

DatagramPacket sendPacket = new DatagramPacket(command, command.length, address, 8080);
socket.send(sendPacket);
} catch (SocketException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.disconnect();
socket.close();
}
}
}
}).start();

关于java - 通过WiFi连接Java DatagramSocket的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33945483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com