2013-10-23 2 views
0

내가 239.255.255.253멀티 캐스트 소켓 서버 수없는 안드로이드 모바일

코드에 멀티 캐스트 패킷을 클라이언트 역할을하며 보내는 안드로이드 모바일되어 있습니다

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
      Thread t=new Thread(new Multi()); 
     t.start(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

class Multi implements Runnable 
{ 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     try { 
       InetAddress ip=InetAddress.getByName("239.255.255.253"); 
      int port=4270; 
      //Create a Multicast socket 
      MulticastSocket sock=new MulticastSocket(); 
      String msg="Hello All"; 

      DatagramPacket pack=new DatagramPacket(msg.getBytes(),msg.length(),ip,port); 
      sock.send(pack); 
      sock.close(); 
       System.out.println("Packet sent"); 


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

    } 

} 

및 내 안드로이드 매니페스트 파일

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.multicast" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/> 
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> 
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.multicast.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

안드로이드이며, PC는 모두 라우터를 통해 연결되어

239.255.255.253 및 224.0.0.2에 패킷을 수신하는 서버 코드를 실행중인 PC iam에서.

서버 코드는 PC에있는 모든 패킷을 수신 할

import java.io.*; 
import java.net.*; 


class receiver 
{ 

    public static void main(String args[]) 
    { 
     try 
     {  
      //get the multicast ip 
      InetAddress ip1 = InetAddress.getByName("239.255.255.253"); 
      InetAddress ip2 = InetAddress.getByName("224.0.0.2"); 
      int port=4270; 

      MulticastSocket sock=new MulticastSocket(port); 

      //join the multicast group 
      sock.joinGroup(ip1); 
      sock.joinGroup(ip2); 
      while(true) 
      { 
      //create a datagram packet in which u will receive the msg 
       byte[] buffer=new byte[100]; 
       DatagramPacket pack=new DatagramPacket(buffer,buffer.length); 
       sock.receive(pack); 
       InetAddress ip= pack.getAddress(); 
       System.out.print(ip+":"); 
       System.out.println("the message received from the sender is "+new String(buffer)); 
      } 
      //sock.close();  
     } 
     catch(Exception e){ 

     } 


    } 



} 

스피없는,하지만 난 "224.0.0.2"로 클라이언트의 멀티 캐스트 주소를 변경 할 때 잘 작동합니다. 누구든지이 문제를 해결하도록 도울 수 있습니까

답변

관련 문제