Click here to Skip to main content
15,900,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new in socket programming. i am using Multicast socket for broadcasting through wifi hotspot network.But i am facing problem at the reciver side.
Since So many days i am trying.

What I have tried:

Here is my code Server Side



Java
import java.io.IOException;
 import java.net.DatagramPacket;
import java.net.DatagramSocket;
 import java.net.InetAddress;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class SenderService extends Service
{
SenderThread currentSender;

public class SenderThread extends Thread
{

    public void run()
    {
        Log.d("in thread","in service");
        try
        {

            InetAddress group = InetAddress.getByName("224.0.0.3");
              int port=10000;
              DatagramSocket sock=new DatagramSocket();
              sock.setBroadcast(true);
                String msg="hello";
                byte []b3=new byte[1024];
                DatagramPacket packet;
                b3=msg.getBytes();


                while (true)
                {   

                try
                {


                    packet = new DatagramPacket(b3, b3.length,group, port); 

                            sock.send(packet);
                            Log.d("MSG:", "sent");

                } catch (IOException e)
                {
                    Log.d("Send Excp:", e+"");
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                finally
                {
                    sock.close();
                }

            }

        } catch (Exception e)
        {

            e.printStackTrace();
        }

    }
}
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
}

@Override
public IBinder onBind(Intent arg0)
{
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{

    new SenderThread().start();

    return Service.START_STICKY;
}

}




My Client Side Code

Java
 import java.io.IOException;
 import java.net.DatagramPacket;
 import java.net.DatagramSocket;
 import java.net.InetAddress;
import java.net.InetSocketAddress;
 import java.net.MulticastSocket;
 import java.net.NetworkInterface;
 import java.util.Enumeration;

 import android.app.Activity;
import android.content.Context;
 import android.net.ConnectivityManager;
   import android.net.NetworkInfo;
 import android.net.wifi.WifiManager;
 import android.net.wifi.WifiManager.MulticastLock;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
 import android.util.Log;
import android.view.View;
  import android.widget.Button;
import android.widget.Toast;

  public class ReceiverActivity extends Activity
   {
    Button b;
    Handler h;
    class MyRTh extends Thread
    {
        InetAddress group=null ;
        WifiManager wifi =    (WifiManager)getSystemService(Context.WIFI_SERVICE);
        MulticastLock lock = wifi.createMulticastLock("HelloAndroid");

        public void run()
        {

            try
            {

            lock.acquire();
            }
            catch (Exception e) {
                // TODO: handle exception
                Log.d("lock",e+"");
            }
            try
            {

                final MulticastSocket sock=new MulticastSocket(10000);
                group= InetAddress.getByName("224.0.0.3");
                //sock.setSoTimeout(15000);
                try
                {
                sock.joinGroup(group);
                }
                catch(Exception e)
                {
                    Log.d("join",""+e.toString());

                }

                while(true)
               {
                try
                {

                    Log.d("in try","l4");






                            byte[] data = new byte[1024]; 

                            // TODO Auto-generated method stub
                            DatagramPacket packet = new DatagramPacket(data, data.length);

                            Log.d("in run","l4");
                            try {
                                    sock.receive(packet);
                                String s = new String(packet.getData());

                                Log.e("MSG:", "Received");

                                Log.d("msg",s);

                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                Log.d("err",e.toString());
                            }



                }
                catch (Exception e)
                {
                    Log.d("Excp", e.toString()+"");
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                }
                }catch (Exception e) {
                // TODO: handle exception
            }



        }

    }

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_receiver);
    Toast.makeText(getApplicationContext(), "Receiving", Toast.LENGTH_SHORT).show();
    Log.d("1","l1");

    b=(Button)findViewById(R.id.buttonr);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0)
        {

            MyRTh th=new MyRTh();
            th.start();
        }
    });
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900