Click here to Skip to main content
15,888,610 members
Home / Discussions / Java
   

Java

 
GeneralRe: Java Array Sorting Problem Pin
bmnot26-Jan-10 13:40
bmnot26-Jan-10 13:40 
GeneralRe: Java Array Sorting Problem Pin
harold aptroot27-Jan-10 1:10
harold aptroot27-Jan-10 1:10 
GeneralRe: Java Array Sorting Problem Pin
harold aptroot1-Feb-10 1:06
harold aptroot1-Feb-10 1:06 
GeneralRe: Java Array Sorting Problem Pin
Luc Pattyn26-Jan-10 15:24
sitebuilderLuc Pattyn26-Jan-10 15:24 
QuestionCan a Java application call a DotNet WCF service? Pin Pin
ashishtango24-Jan-10 17:33
ashishtango24-Jan-10 17:33 
AnswerRe: Can a Java application call a DotNet WCF service? Pin Pin
427748024-Jan-10 18:14
427748024-Jan-10 18:14 
AnswerRe: Can a Java application call a DotNet WCF service? Pin Pin
Richard MacCutchan24-Jan-10 22:23
mveRichard MacCutchan24-Jan-10 22:23 
QuestionAudio Files in socket programming. [modified] Pin
vidzdas24-Jan-10 2:08
vidzdas24-Jan-10 2:08 
Hello..
I am trying to write a code in socket programming using java that would transfer audio files from 1 destination to another.
I am able to read the audio file successfully using AudioInputStream and AudioSystem methods.
However, i am unable to write back the same file to the destination. The client side does not receive the audio file properly.
Please can u help me? Smile | :)
I was able to successfully transfer a simple text file..but it just doesn't seem to work for audio files..

Thanks n regards..
here is my source code..

Server code
 public class UdpSend1
   {
	   private DatagramSocket socket = null; 
       private DatagramPacket recvPacket, sendPacket; 
       private int hostPort;
    
       public static void main(String args[]) 
       {
    	   
           DatagramSocket socket = null;
           DatagramPacket recvPacket, sendPacket;
           try
           {
               socket = new DatagramSocket();
               InetAddress hostAddress = InetAddress.getLocalHost();
               
               
               File fr = new File("C:\\Taal_Theme.wav");
               AudioInputStream aio = AudioSystem.getAudioInputStream(fr);
               int totalFramesRead = 0;
   			
            	while (socket != null)
            	{ 
            		int bytesPerFrame = aio.getFormat().getFrameSize();
            		if (bytesPerFrame == AudioSystem.NOT_SPECIFIED) 
            		{
            	   
            	    //bytesPerFrame = 1;
            		} 
            	            		int numBytes = 1024 * bytesPerFrame; 
            		byte[] audioBytes = new byte[numBytes];
            	
            		try
            		{
            		    int numBytesRead = 0;
            		    int numFramesRead = 0;
            		    // Try to read numBytes bytes from the file.
            		    while ((numBytesRead = aio.read(audioBytes)) != -1) 	
            	    {
            	      // Calculate the number of frames actually read.
            	      numFramesRead = numBytesRead / bytesPerFrame;
            	      totalFramesRead += numFramesRead;
            	      
            	      
            	      sendPacket = new DatagramPacket(audioBytes, numFramesRead, hostAddress, numFramesRead);
            	      //sendPacket = new DatagramPacket(audioBytes, audioBytes.length, hostAddress, 4545 );
                      socket.send( sendPacket );  
                      recvPacket= new DatagramPacket(new byte[512], 512, hostAddress, 0);
                      socket.receive(recvPacket); 
                      
                      System.out.write(recvPacket.getData(), 0,recvPacket.getLength());
                      System.out.print("\n"); 
            	    }
            	
            	}
            	            	      
                catch (SocketException se)
                {
                	System.out.println("Error in SimpleDatagramClient: " + se);
                }
            }
           }
           catch (IOException ioe)
           {
               System.out.println("Error in SimpleDatagramClient: " + ioe);
           }
           catch (UnsupportedAudioFileException e) 
           {
        	   System.out.println("Error!!! "+e);
			e.printStackTrace();
		}
       }
}



The Client code
   public class UdpReceive1
   {
       public static void main(String args[]) 
       {
           DatagramSocket socket = null;
           DatagramPacket recvPacket, sendPacket;
           
           try
           {
        	   InetAddress hostAddress = InetAddress.getLocalHost();
        	   FileOutputStream fout = new FileOutputStream("C:\\f.wav");
        	   

        	   AudioInputStream aio;
        	   socket = new DatagramSocket(4545);
               while (socket != null)
               { 
            	   System.out.println("HELLO!!!");
            	   recvPacket= new DatagramPacket(new byte[512], 512, 

hostAddress, 0);
                   socket.receive(recvPacket); 
                   System.out.println("HELLO!!!");
                   byte[] str = new byte[512];
                   str = recvPacket.getData();
                   
                   System.out.println(str);
                  
                   ByteArrayInputStream bis = new ByteArrayInputStream(str);
                   System.out.println(bis);
                   aio = new AudioInputStream(bis, null, 512);
                   AudioSystem.write(aio, AudioFileFormat.Type.WAVE, fout);
                   
                   
                  
                   //System.out.write(recvPacket.getData(), 

0,recvPacket.getLength());
                   System.out.print("\n");
                   
                   sendPacket = new DatagramPacket(recvPacket.getData(), 

recvPacket.getLength(),recvPacket.getAddress(),recvPacket.getPort());
                   socket.send( sendPacket );
                   
                  
               } 
           }
           catch (SocketException se)
           {
               System.out.println("Error in DatagramClient: " + se);
           }
           catch (IOException ioe)
           {
               System.out.println("Error in DatagramServer: " + ioe);
           }
       }
   } 


modified on Monday, January 25, 2010 9:58 AM

AnswerRe: Audio Files in socket programming. Pin
Richard MacCutchan24-Jan-10 2:30
mveRichard MacCutchan24-Jan-10 2:30 
GeneralRe: Audio Files in socket programming. Pin
vidzdas24-Jan-10 6:19
vidzdas24-Jan-10 6:19 
GeneralRe: Audio Files in socket programming. Pin
Richard MacCutchan24-Jan-10 9:19
mveRichard MacCutchan24-Jan-10 9:19 
GeneralRe: Audio Files in socket programming. Pin
427748024-Jan-10 18:15
427748024-Jan-10 18:15 
GeneralRe: Audio Files in socket programming. Pin
Richard MacCutchan24-Jan-10 22:07
mveRichard MacCutchan24-Jan-10 22:07 
GeneralRe: Audio Files in socket programming. Pin
427748024-Jan-10 22:20
427748024-Jan-10 22:20 
GeneralRe: Audio Files in socket programming. Pin
Richard MacCutchan24-Jan-10 23:10
mveRichard MacCutchan24-Jan-10 23:10 
GeneralRe: Audio Files in socket programming. Pin
427748024-Jan-10 23:27
427748024-Jan-10 23:27 
AnswerRe: Audio Files in socket programming. Pin
san-shiro{701}10-Feb-10 11:43
san-shiro{701}10-Feb-10 11:43 
QuestionREG: SSH Pin
sangeethanarayan22-Jan-10 2:08
sangeethanarayan22-Jan-10 2:08 
AnswerRe: REG: SSH Pin
Richard MacCutchan22-Jan-10 3:59
mveRichard MacCutchan22-Jan-10 3:59 
QuestionProblem in SetBoundaryModes with Option Clamp Pin
002comp20-Jan-10 20:32
002comp20-Jan-10 20:32 
Questionimplementing term frequency and inverse document frequency in java Pin
Abiodun Modupe19-Jan-10 12:46
Abiodun Modupe19-Jan-10 12:46 
AnswerRe: implementing term frequency and inverse document frequency in java Pin
Richard MacCutchan19-Jan-10 22:39
mveRichard MacCutchan19-Jan-10 22:39 
AnswerRe: implementing term frequency and inverse document frequency in java Pin
Cedric Moonen19-Jan-10 22:53
Cedric Moonen19-Jan-10 22:53 
Questionjava and windows vista Pin
StrayGrey18-Jan-10 3:58
StrayGrey18-Jan-10 3:58 
AnswerRe: java and windows vista Pin
David Skelly18-Jan-10 4:31
David Skelly18-Jan-10 4:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.