2011-12-07 15 views
1

2人のプログラムのうち1人がAdnroidデバイスからUDPパケットを送信し、もう1つがJavaを受信する2つのプログラムを作成しています。 WiFiルータを使用するとうまくいく。しかし、私が直接接続を使用すると、Javaアプリケーションはこれらのパケットを受信しません。直接接続の下では、コンピュータ上にHOTSPOTを作成し、それにAndroidデバイスで接続することを意味します。AndroidはWi-Fi経由でJavaクライアントにUDPパケットを送信します

Serverのコード:

public class UDPServer { 
InetAddress groupAddress; 
DatagramPacket packet; 
byte[] buffer; 
DatagramSocket socket; 
static String ip = "227.0.25.57"; 
static int port = 6789; 

private boolean isRun = false; 
private String message = ""; 
private int broadcastInterval; 
public void StopBroadcasting(){ 
    isRun = false;  
} 
public void StartBroadcasting(String message, int broadcastInterval){ 
    isRun = true; 
    this.message = message; 
    this.broadcastInterval = broadcastInterval;  
    new Thread(runner).start();  
} 
Runnable runner = new Runnable() {  
    @Override 
    public void run() { 
     while(isRun){    
      try { 
       SendBroadcastMessage(message); 
       System.out.println("msg sended..."); 
       Thread.sleep(broadcastInterval); 
      } catch (InterruptedException e) {     
       e.printStackTrace(); 
      } catch (IOException e) {     
       e.printStackTrace(); 
      } 
     } 
     System.out.println("Stopping UDPServer..."); 
    } 
}; 

public UDPServer() { 
    buffer = new byte[4096]; 
    try { 
     groupAddress = InetAddress.getByName(ip); 
    } catch (UnknownHostException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     socket = new DatagramSocket(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }     
} 

public void SendBroadcastMessage(String msg) throws IOException{ 
    if(msg==null) return; 
    buffer = msg.getBytes(); 
    packet = new //DatagramPacket(buffer, buffer.length); 
    DatagramPacket(buffer, buffer.length, groupAddress, port); 
    socket.send(packet); 
} 

public static void Send(String msg){ 
    try {         
     InetAddress group = InetAddress.getByName(ip); 
     MulticastSocket s = new MulticastSocket(port); 
     s.joinGroup(group); 
     DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), 
     group, port); 
     s.send(hi); 
     System.out.println("send..."); 

     } catch (SocketException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
     catch (UnknownHostException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
}} 

クライアントのコード:私は怒鳴るコードスニペットを使用

public class UDPClient {} 
MulticastSocket socket; 
InetAddress groupAddress; 
DatagramPacket packet; 
byte[] buffer; 
static String ip = "227.0.25.57"; 
static int port = 6789; 


public interface OnReceiveDataListener{ 
    public abstract void onReceiveData(String data); 
} 

private OnReceiveDataListener ReceiveDataListener = null; 

public void setReceiveDataListener(OnReceiveDataListener ReceiveDataListener) { 
    this.ReceiveDataListener = ReceiveDataListener; 
} 

public OnReceiveDataListener getReceiveDataListener() { 
    return ReceiveDataListener; 
} 

private boolean isRun = false; 
private Thread broadcastReceiver; 
public void StopBroadcasting(){ 
    isRun = false; 
    if(broadcastReceiver!=null) 
     broadcastReceiver.interrupt(); 
} 
public void StartBroadcasting(){ 
    isRun = true;   
    broadcastReceiver = new Thread(runner);  
    broadcastReceiver.start(); 
} 
Runnable runner = new Runnable() {  
    @Override 
    public void run() { 
     while(isRun){    
      try { 
       String msg = ReceiveBroadcastMessage(); 
       if(ReceiveDataListener!=null) 
        ReceiveDataListener.onReceiveData(msg);    
      } catch (IOException e) {     
       e.printStackTrace(); 
      } 
     } 
    } 
}; 

public UDPClient(){ 
    buffer = new byte[4096]; 
    try { 
     groupAddress = InetAddress.getByName(ip); 
     socket = new MulticastSocket(port); 
     socket.joinGroup(groupAddress); 
    } catch (UnknownHostException e) {   
     e.printStackTrace(); 
    } catch (IOException e) {   
     e.printStackTrace(); 
    }  
} 

public String ReceiveBroadcastMessage() throws IOException{  
    packet = new DatagramPacket(buffer, buffer.length); 
    System.out.println("before receive"); 
    socket.receive(packet); 
    System.out.println(packet.getData()); 
    return new String(packet.getData()); 
} 

public void DeInit(){ 
    try { 
     socket.leaveGroup(groupAddress); 
    } catch (IOException e) { 

     e.printStackTrace(); 
    } 
    socket.close(); 
} 

MulticastSocket msocket; 
public static void Receive(){ 
     MulticastSocket msocket; 

     try { 
     msocket = new MulticastSocket(port); 
     InetAddress group = InetAddress.getByName(ip); 
     msocket.joinGroup(group); 

     byte[] inbuf = new byte[1024]; 
     DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length); 

     System.out.println("before receive"); 
     msocket.receive(packet); 
     System.out.println("after receive"); 
     int numBytesReceived = packet.getLength(); 

     System.out.println(new String(packet.getData())); 
     msocket.leaveGroup(group); 
     msocket.close(); 
     System.out.println(numBytesReceived); 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
    }} 

あなたが見ることができれば、私は、パケットを送受信する2つの方法があります。両方とも動作しません!何が悪いですか? お願いします。

答えて

関連する問題