我已经创建了一个程序,它是一个服务器Android,用于通过wifi从C程序客户端接收UDP数据包,但实际问题是我只能通过USB线将手机连接到我的PC客户端才能运行我的应用在我的手机中。 因为我是Android世界的新手,我会请你指导我如何通过wifi设置连接而不使用电缆?
帮助!
这是我的服务器代码:
package com.example.server_android;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
public class Server_Android extends Activity{
private final static int SERVER_PORT = 1234;
public final static int RECEIVING_TIMEOUT_SERVER = 3000;
DatagramSocket socket;
DatagramPacket packetOut;
DatagramPacket packetIn;
byte[] DataIn;
byte[] DataOut;
/*Android widgets*/
TextView text;
EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server_android);
//text = (EditText)findViewById(R.id.editText1);
text =(TextView)findViewById(R.id.textView2);
text.setText("");
/* Thread for receiving Data from CLient */
new Thread(new Receiver()).start();
try {
Thread.sleep(500);
}catch(InterruptedException e){
Log.e("UDP", "UDP receive failed!");
}
}
public class Receiver implements Runnable{
public void run(){
try {
while(socket != null && !socket.isClosed()){
DataOut = new byte[1024];
// InetAddress fromaddress = InetAddress.getByName("10.4.0.11");
socket = new DatagramSocket(SERVER_PORT);
socket.setSoTimeout(RECEIVING_TIMEOUT_SERVER);
packetIn = new DatagramPacket(DataOut,DataOut.length);
socket.receive(packetIn);
text.append("source port :" + packetIn.getPort() + "\n");
text.append("source address :" + packetIn.getAddress().toString() + "\n");
Log.d("UDP", "Packet receveid");
String message = new String(packetIn.getData());
text.append(message +"\n"+ packetIn.getPort() +"\n"+ packetIn.getAddress().toString());
Log.d("UDP", "le message reçu");
Log.d("Message : ", "" + message);
}
}
catch(UnknownHostException exc) {
System.out.println("Destinataire inconnu");
}
catch(SocketException exc) {
System.out.println("Probleme d'ouverture de la socket");
}
catch(IOException exc) {
System.out.println("Probleme sur la reception du message");
}
}
}
}
和我的客户代码: // ------------------------------------------------ ----------------------------
#include "biblio.h"
#define portnumber 1234
int
main(int argc,
char **argv)
{
//_________Seting_Destination_IP_adress____________________
in_addr_t ipAddress= inet_addr("10.4.0.156");
//---- create UDP socket ----
int udpSocket=socket(PF_INET,SOCK_DGRAM,0);
if(udpSocket==-1)
{ perror("socket"); exit(1); }
struct sockaddr_in toAddr;
int lenght = sizeof(toAddr);
// ... allowing broadcast (optional)
int on=1;
if(setsockopt(udpSocket,SOL_SOCKET,SO_BROADCAST,&on,sizeof(int))==-1)
{ perror("setsockopt"); exit(1); }
int s;
for(;;)
{
//---- read next line on standard input ----
char msg[0x100];
char *buffer = malloc(100);
if(!fgets(msg,0x100,stdin)) { break; }
//---- send message to the specified destination/port ----
bzero(&toAddr,lenght);
toAddr.sin_family=AF_INET;
toAddr.sin_port=htons(portnumber);
toAddr.sin_addr.s_addr=ipAddress;
s = sendto(udpSocket,msg,strlen(msg),0,(struct sockaddr *)&toAddr,lenght);
printf("Destination address is %s -- port is : %d ",inet_ntoa(toAddr.sin_addr),ntohs(toAddr.sin_port));
if( s == -1)
{ perror("sendto"); exit(1); }
}
//---- close UDP socket ----
close(udpSocket);
return 0;
}
//----------------------------------------------------------------------------
这是日志文件:
08-20 10:07:56.885: D/libEGL(22622): loaded /system/lib/egl/libEGL_mali.so
08-20 10:07:56.890: D/libEGL(22622): loaded /system/lib/egl/libGLESv1_CM_mali.so
08-20 10:07:56.895: D/libEGL(22622): loaded /system/lib/egl/libGLESv2_mali.so
08-20 10:07:56.900: D/(22622): Device driver API match
08-20 10:07:56.900: D/(22622): Device driver API version: 10
08-20 10:07:56.900: D/(22622): User space API version: 10
08-20 10:07:56.900: D/(22622): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Mon Mar 5 09:47:55 KST 2012
08-20 10:07:56.925: D/OpenGLRenderer(22622): Enabling debug mode 0
08-20 10:08:05.555: W/dalvikvm(22622): threadid=11: thread exiting with uncaught exception (group=0x40a2d1f8)
08-20 10:08:05.570: E/AndroidRuntime(22622): FATAL EXCEPTION: Thread-1053
08-20 10:08:05.570: E/AndroidRuntime(22622): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-20 10:08:05.570: E/AndroidRuntime(22622): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
08-20 10:08:05.570: E/AndroidRuntime(22622): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:709)
08-20 10:08:05.570: E/AndroidRuntime(22622): at android.view.View.requestLayout(View.java:12675)
08-20 10:08:05.570: E/AndroidRuntime(22622): at android.view.View.requestLayout(View.java:12675)
08-20 10:08:05.570: E/AndroidRuntime(22622): at android.view.View.requestLayout(View.java:12675)
08-20 10:08:05.570: E/AndroidRuntime(22622): at android.view.View.requestLayout(View.java:12675)
08-20 10:08:05.570: E/AndroidRuntime(22622): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:268)
08-20 10:08:05.570: E/AndroidRuntime(22622): at android.view.View.requestLayout(View.java:12675)
08-20 10:08:05.570: E/AndroidRuntime(22622): at android.widget.TextView.checkForRelayout(TextView.java:6773)
08-20 10:08:05.570: E/AndroidRuntime(22622): at android.widget.TextView.setText(TextView.java:3306)
08-20 10:08:05.570: E/AndroidRuntime(22622): at android.widget.TextView.setText(TextView.java:3162)
08-20 10:08:05.570: E/AndroidRuntime(22622): at android.widget.TextView.append(TextView.java:2822)
08-20 10:08:05.570: E/AndroidRuntime(22622): at android.widget.TextView.append(TextView.java:2812)
08-20 10:08:05.570: E/AndroidRuntime(22622): at com.example.test_udp.MainActivity$Receiver.run(MainActivity.java:70)
08-20 10:08:05.570: E/AndroidRuntime(22622): at java.lang.Thread.run(Thread.java:856)
08-20 10:08:05.790: D/OpenGLRenderer(22622): Flushing caches (mode 0)
08-20 10:08:06.325: D/OpenGLRenderer(22622): Flushing caches (mode 1)
08-20 10:13:05.715: I/Process(22622): Sending signal. PID: 22622 SIG: 9
答案 0 :(得分:0)
您确定通过电缆连接此程序有效吗?然后我们不需要检查代码,但是看看我在你的线程的run方法中看到,第一行代码说:while (socket != null && !socket.isClosed())
这两个都恰好是代码的方式书面。所以我怀疑它适用于Wifi或有线电视。
我认为您应该像这样更改线程的代码:
public void run()
{
try
{
socket = new DatagramSocket(SERVER_PORT);
socket.setSoTimeout(RECEIVING_TIMEOUT_SERVER);
DataOut = new byte[1024];
// InetAddress fromaddress = InetAddress.getByName("10.4.0.11");
packetIn = new DatagramPacket(DataOut, DataOut.length);
// keep receiving packets, until keepOn set to false
while (keepOn)
{
socket.receive(packetIn);
text.append("source port :" + packetIn.getPort() + "\n");
text.append("source address :" + packetIn.getAddress().toString() + "\n");
Log.d("UDP", "Packet receveid");
String message = new String(packetIn.getData());
text.append(message +"\n"+ packetIn.getPort() +"\n"+ packetIn.getAddress().toString());
Log.d("UDP", "le message reçu");
Log.d("Message : ", "" + message);
}
}
catch (UnknownHostException exc)
{
System.out.println("Destinataire inconnu");
}
catch (SocketException exc)
{
System.out.println("Probleme d'ouverture de la socket");
}
catch (IOException exc)
{
System.out.println("Probleme sur la reception du message");
}
catch (Exception exc)
{
exc.printStackTrace();
}
}