Nirmal_stack
Nirmal_stack

Reputation: 310

Audio File Transfer using UDP in JAVA

I have written code for audio file(mp3) transfer between client and server.First server starts and waits for datagrampackets to receive,then client starts and send UDP Datagram-packets to server.

\Server

            serverSocket = new DatagramSocket(4000);
            int packetsize=1024;
            FileOutputStream fos = null;
           try {
               fos = new FileOutputStream("zz.wav");
               BufferedOutputStream bos = new BufferedOutputStream(fos);
               double nosofpackets=Math.ceil(((int) (new File("F:\\nirmal\\1.wav")).length())/packetsize);
                byte[] mybytearray = new byte[packetsize];
                DatagramPacket receivePacket = new DatagramPacket(mybytearray,mybytearray.length);

                System.out.println(nosofpackets+" "+mybytearray+" "+ packetsize);

                for(double i=0;i<nosofpackets+1;i++)
               {

                   serverSocket.receive(receivePacket); 
                   byte audioData[] = receivePacket.getData();
                   System.out.println("Packet:"+(i+1));
                   bos.write(audioData, 0,audioData.length);
               }

\client

   File myFile = new File("F:\\nirmal\\1.wav");
   DatagramSocket ds=new DatagramSocket(9000);
   DatagramPacket dp;
   int packetsize=1024;
   double nosofpackets;
   noofpackets= =Math.ceil(((int) myFile.length())/packetsize);

   BufferedInputStream bis;
   bis = new BufferedInputStream(new= FileInputStream(myFile));        
   for(double i=0;i<nosofpackets+1;i++) 
       {
           byte[] mybytearray = new byte[packetsize];
           bis.read(mybytearray, 0, mybytearray.length);
           System.out.println("Packet:"+(i+1));
         dp=new DatagramPacket(mybytearray,mybytearray.length,InetAddress.getByName("172.17.13.46"),4000);
                    }

When both client ans server are running then server get stuck at serverSocket.receive(receivePacket) line like server is not receiving any packets.I don't know where i am doing mistake.

Upvotes: 1

Views: 3289

Answers (1)

jmarques
jmarques

Reputation: 131

The problem is on the client side.

You need to send your DatagramPackets throught the DatagramSocket to been received on the server side. The DatagramScoket is a client socket so you don't need to reserve a port number for it. If you run your code on your local computer better use "localhost" or "127.0.0.1"

public class UDPClient {

    public void send() throws IOException, InterruptedException {
        File myFile = new File("aa.hex");
        DatagramSocket ds = null;
        BufferedInputStream bis = null;
        try {
            ds = new DatagramSocket();
            DatagramPacket dp;
            int packetsize = 1024;
            double nosofpackets;
            nosofpackets = Math.ceil(((int) myFile.length()) / packetsize);

            bis = new BufferedInputStream(new FileInputStream(myFile));
            for (double i = 0; i < nosofpackets + 1; i++) {
                byte[] mybytearray = new byte[packetsize];
                bis.read(mybytearray, 0, mybytearray.length);
                System.out.println("Packet:" + (i + 1));
                dp = new DatagramPacket(mybytearray, mybytearray.length, InetAddress.getByName("127.0.0.1"), 4000);
                ds.send(dp);                    
            }
        }finally {
            if(bis!=null)
                bis.close();
            if(ds !=null)
                ds.close();
        }

    }
}

When I prooved the code the server wasn't able to receive all the packets from the client. I fixed it by adding a Thread.sleep(10L) instruction after ds.send(dp).

Upvotes: 1

Related Questions