通过wifi android进行udp连接
我一直坚持通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要互联网权限。将其添加到您的清单中:
You need the Internet Permission. Add this to your manifest:
<uses-permission android:name="android.permission.INTERNET"/>