0

我制作了一个程序,它是一个服务器 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
4

1 回答 1

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();
    }
}
于 2013-08-19T13:57:03.447 回答