通过wifi android进行udp连接

发布于 2024-12-18 03:16:55 字数 2496 浏览 0 评论 0原文

我一直坚持通过 wifi 在 android 手机(2.3)和我的电脑之间创建 UDP 连接。

我知道如何在本地服务器中创建 UDP 连接。我的问题是 android 支持 adhock 网络,因为每当我尝试直接搜索我的电脑的 wifi 时,它都不会显示它,因此我必须首先在我的电脑上创建一个虚拟热点,然后将我的手机连接到它。

之后,我只是尝试将数据包从手机发送到电脑上运行的服务器。

public class WifitestActivity extends Activity {
    WifiManager w;
    TextView status;
    InetAddress server_ip;
    int server_port = 9876;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    status = (TextView) findViewById(R.id.status);

    w = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

    if (!w.isWifiEnabled()) {
        status.setText("switching ON wifi ");
        w.setWifiEnabled(true);
    } else {
        status.setText("Its already ON ");

    }

    int x;
    WifiInfo info = w.getConnectionInfo();
    status.append("\n\nWiFi Status: " + info.toString());

    x = info.getIpAddress();
    String str = info.getMacAddress();

    status.append("\n\nmac address===" + str + "  ,ip===" + x);

    try {
        server_ip = InetAddress.getByName("192.168.181.1"); // ip of my server.How to dynamically update it
    } catch (UnknownHostException e) {
        status.append("Error at fetching inetAddress");
    }

    DatagramSocket s = new DatagramSocket(server_port, server_ip);
    // **ERROR AT PREVIOUS LINE, I HAD TO FORCE STOP MY APP EVERTIME I RUN
    // MY CODE**
    String str = "TEST MESSAGE !!!";
    byte b1[];
    b1 = new byte[100];
    b1 = str.getBytes();
    DatagramPacket p1 = new DatagramPacket(b1, b1.length, server_ip,
            server_port);
}

客户

在我的 PC 上运行的服务器代码:

    import java.io.*;
    import java.net.*;
    class server2
    {
        static InetAddress clientip;
        static int clientport;
        static DatagramPacket p3;

        public static void main(String args[])throws Exception
        {

        DatagramSocket s = new DatagramSocket(9876);

        byte b1[],b2[];
        b1=new byte[100];
        b2=new byte[100];

        DatagramPacket p1 = new DatagramPacket(b1,b1.length);
        s.receive(p1);

        b1=p1.getData();
        String str = new String( b1);

        clientport = p1.getPort();  //packet mein save hota hai 
        clientip=p1.getAddress();

        System.out.println("RECIEVED FROM CLIENT IP ="+clientip+" port="+clientport+" data="+str);
        }

}

端代码 (WifiTestActivity) 出现错误,每当我尝试在手机上运行它时,我的应用程序都会崩溃。请帮帮我!

i'm stuck at creating UDP connection between android phone(2.3) and my PC over wifi.

I know how to create a UDP connection in local server. My problem is do android support adhock network because whenever i try searching for my PC's wifi directly , it doesn't show it , hence i have to first create a virtual hotspot over my PC and then connect my phone to it .

After this , i simply try sending data packets from my phone to server running on my PC.

public class WifitestActivity extends Activity {
    WifiManager w;
    TextView status;
    InetAddress server_ip;
    int server_port = 9876;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    status = (TextView) findViewById(R.id.status);

    w = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

    if (!w.isWifiEnabled()) {
        status.setText("switching ON wifi ");
        w.setWifiEnabled(true);
    } else {
        status.setText("Its already ON ");

    }

    int x;
    WifiInfo info = w.getConnectionInfo();
    status.append("\n\nWiFi Status: " + info.toString());

    x = info.getIpAddress();
    String str = info.getMacAddress();

    status.append("\n\nmac address===" + str + "  ,ip===" + x);

    try {
        server_ip = InetAddress.getByName("192.168.181.1"); // ip of my server.How to dynamically update it
    } catch (UnknownHostException e) {
        status.append("Error at fetching inetAddress");
    }

    DatagramSocket s = new DatagramSocket(server_port, server_ip);
    // **ERROR AT PREVIOUS LINE, I HAD TO FORCE STOP MY APP EVERTIME I RUN
    // MY CODE**
    String str = "TEST MESSAGE !!!";
    byte b1[];
    b1 = new byte[100];
    b1 = str.getBytes();
    DatagramPacket p1 = new DatagramPacket(b1, b1.length, server_ip,
            server_port);
}

}

Server code running on my PC :

    import java.io.*;
    import java.net.*;
    class server2
    {
        static InetAddress clientip;
        static int clientport;
        static DatagramPacket p3;

        public static void main(String args[])throws Exception
        {

        DatagramSocket s = new DatagramSocket(9876);

        byte b1[],b2[];
        b1=new byte[100];
        b2=new byte[100];

        DatagramPacket p1 = new DatagramPacket(b1,b1.length);
        s.receive(p1);

        b1=p1.getData();
        String str = new String( b1);

        clientport = p1.getPort();  //packet mein save hota hai 
        clientip=p1.getAddress();

        System.out.println("RECIEVED FROM CLIENT IP ="+clientip+" port="+clientport+" data="+str);
        }

}

There is an error at client code (WifiTestActivity) and my app crashes whenever i try running it on my phone . Plzz help me out !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白况 2024-12-25 03:16:55

您需要互联网权限。将其添加到您的清单中:

You need the Internet Permission. Add this to your manifest:

<uses-permission android:name="android.permission.INTERNET"/>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文