|
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?
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)
{
}
int numBytes = 1024 * bytesPerFrame;
byte[] audioBytes = new byte[numBytes];
try
{
int numBytesRead = 0;
int numFramesRead = 0;
while ((numBytesRead = aio.read(audioBytes)) != -1)
{
numFramesRead = numBytesRead / bytesPerFrame;
totalFramesRead += numFramesRead;
sendPacket = new DatagramPacket(audioBytes, numFramesRead, hostAddress, numFramesRead);
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);
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
|
|
|
|
|
vidzdas wrote: However, i am unable to write back the same file to the destination. The client side does not receive the audio file properly.
Perhaps you could explain exactly what happens when you try this. There is no real difference in sending a text file or an audio file from one computer to another; a file is just a stream of bytes. It may be that you should read the file as a simple BufferedInputStream rather than using the AudioSystem object.
MVP 2010 - are they mad?
|
|
|
|
|
Hey thanks..but i already tried that.
A file is created on the other computer but only garbage is displayed. The audio comes as only bytes.
Can u please suggest something else.??
|
|
|
|
|
vidzdas wrote: Hey thanks..but i already tried that.
Uh, tried what?
vidzdas wrote: A file is created on the other computer but only garbage is displayed. The audio is comes as only bytes.
I don't understand, there is nothing to display from an audio file.
MVP 2010 - are they mad?
|
|
|
|
|
|
4277480 wrote: Try this
That's what I was suggesting. I wish I had a £ for every time I have tried to get people to understand that a file is merely a stream of bytes, and transferring from one place to another without the need to understand the content is what keeps the entire world running these days.
MVP 2010 - are they mad?
|
|
|
|
|
haha I know but the thought of sending stream of garbage which is supposed to be an audio file really is an interesting theory
|
|
|
|
|
4277480 wrote: the thought of sending stream of garbage which is supposed to be an audio file really is an interesting theory
It fits the model of so many issues posted here these days!
btw welcome back, haven't seen input from you for some time.
MVP 2010 - are they mad?
|
|
|
|
|
Thank you, now I am back to idle mode until the next project, so from time to time I will be referencing here.
|
|
|
|
|
hey
try to use ObjectInputStream and ObjectOutputStream
am always use it to transfer any type of files.
/*********************************************
* simple FTP(Client Side/upload) *
* Written By : Salah I. Qasem *
* E-mail: tormentor701@hotmail.com *
*********************************************/
import java.io.*;
import java.net.*;
public class Client
{
public static void main(String args[])
{
try{
InputStream file = new FileInputStream("fileName.xxx");
Socket socket = new Socket("127.0.0.1" , 2008);
byte[] bfrArray = new byte[file.available()];
file.read(bfrArray);
ObjectOutputStream obj = new ObjectOutputStream(socket.getOutputStream());
obj.writeObject(bfrArray);
}
catch(Exception ex){System.err.println(ex);}
}
}
/*********************************************
* simple FTP(Server Side/receive) *
* Written By : Salah I. Qasem *
* E-mail: tormentor701@hotmail.com *
*********************************************/
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String args[])
{
try{
ServerSocket serverSocket = new ServerSocket(2008);
Socket socket= serverSocket.accept();
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
byte[] bfrArray = (byte[])input.readObject();
OutputStream file = new FileOutputStream("newFileName.xxx");
DataOutputStream writer = new DataOutputStream(file);
writer.write(bfrArray);
}
catch(Exception ex)
{
System.err.println(ex);
}
}
}
|
|
|
|
|
Hi,
I have to create ssh window.And i've mindterm.jar and following code
<APPLET CODE="com.mindbright.application.MindTerm.class" ARCHIVE="mindterm312.jar?r1" WIDTH=800 HEIGHT=600>
<PARAM NAME="line-space-delta" value="0">
<PARAM NAME="local-echo" value="false">
<PARAM NAME="rev-video" value="false">
<PARAM NAME="ignore-null" value="true">
<PARAM NAME="crlf" value="false">
<PARAM NAME="font-size" value="12">
<PARAM NAME="autowrap" value="true">
<PARAM NAME="delete-send" value="bs">
<PARAM NAME="ascii-line" value="false">
<PARAM NAME="local-pgkeys" value="false">
<PARAM NAME="font-name" value="Monospaced">
<PARAM NAME="visual-bell" value="false">
<PARAM NAME="geometry" value="80x24">
<PARAM NAME="backspace-send" value="del">
<PARAM NAME="paste-button" value="middle">
<PARAM NAME="rev-autowrap" value="false">
<PARAM NAME="passthru-prn-enable" value="true">
<PARAM NAME="copy-select" value="true">
<PARAM NAME="save-lines" value="512">
<PARAM NAME="term-type" value="xterm">
<PARAM NAME="key-mappings" value="">
<PARAM NAME="insert-mode" value="false">
<PARAM NAME="username" value="">
<PARAM NAME="copy-crnl" value="false">
<PARAM NAME="bg-color" value="white">
<PARAM NAME="server" value="apps0.toronto.edu">
<PARAM NAME="print-font-size" value="8">
<PARAM NAME="input-charset" value="none">
<PARAM NAME="repos-output" value="true">
<PARAM NAME="resizable" value="true">
<PARAM NAME="repos-input" value="true">
<PARAM NAME="map-ctrl-space" value="true">
<PARAM NAME="print-font-name" value="Courier">
<PARAM NAME="encoding" value="iso-8859-1">
<PARAM NAME="virtual-kbd" value="">
<PARAM NAME="cursor-color" value="i_blue">
<PARAM NAME="scrollbar" value="right">
<PARAM NAME="auto-linefeed" value="false">
<PARAM NAME="select-delim" value="\"\"">
<PARAM NAME="visible-cursor" value="true">
<PARAM NAME="fg-color" value="black">
</APPLET>
When i run this html, i got ssh window but this following error will occur.
Error connecting to apps0.toronto.edu, reason:
-> access denied (java.net.SocketPermission apps0.toronto.edu resolve)
Is there any bug in applet code? please help me.........
|
|
|
|
|
sangeethanarayan wrote: Error connecting to apps0.toronto.edu, reason:
-> access denied
Take a look at the message; it would appear that the server at apps0.toronto.edu does not trust you.
MVP 2010 - are they mad?
|
|
|
|
|
Hello Friends
I am applying texture to a object with clamp option means no repetition of that texture to object.
My code is below:
TextureLoader textureLoader = new TextureLoader(textureImage, TextureLoader.GENERATE_MIPMAP);
Texture texture = textureLoader.getTexture();
texture.setBoundaryModeS(Texture.CLAMP); texture.setBoundaryModeT(Texture.CLAMP);
texture.setBoundaryColor(1.0f,0.0f,0.0f,1.0f);
First of all I mapplying a color to opject suppose red then I am applying texture Image without repetition of yellow color.Then I am setting boundary color(red) too as I read in some Docs.
After that the color of that object doesnt remain red.It becomes light yeloow.
Any Ideas??
Thanks In Advance.
Yogesh
|
|
|
|
|
I need to implement the TF-IDF and calculate the cosine value of a document from a file.Please help urgently.
|
|
|
|
|
See here[^].
MVP 2010 - are they mad?
|
|
|
|
|
You can also read this[^]
|
|
|
|
|
A contact of mine is trying to learn java programming and has started with the Herbert Schildt book Java A Beginners Guide.
His OS is Windows Vista onto which he has installed NetBeans and Eclipse.
He has asked me for help but even though I am a programmer I only use Linux so am not sure that my Vista skills are adequate other than fixing his PATH environment.
His Java version is:-
java version "1.6.0_18"
Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
Java HotSpot(TM) Client VM (build 16.0-b13, mixed mode, sharing)
His 1st program is:-
[code]
/* This demonstrates a variable.
call this file Example2.java.
*/
Class Example2 {
public static void main(String args[ ]) {
int var1; // this declares a variable
int var2; // this declares another variable
var1 = 1024 // this assigns 1024 to variable 1
System.out.println("var1 contains" + var1);
var2 = var1 / 2;
System.out.println("var2 contains var1 / 2: ");
System.out.println(var2);
}
}
[/code]
but
javac Example2
error: Class names, 'Example2', are only accepted if annotation processing
is explicitly requested
How does he fix this?
BTW The above compiles and runs fine on my Ubuntu system....
Regards,
Alf Stockton
|
|
|
|
|
The javac command takes the name of the source file, not the name of the class:
javac Example2.java
|
|
|
|
|
Thank you and the problem was Class rather than class.
Regards,
Alf Stockton
|
|
|
|
|
It is spelled class not Class.....
and all command lines should end with a ;
Regards,
Alf Stockton
|
|
|
|
|
there are not even commands needed - eclipse / netbeans should do the work.
greets
Torsten
I never finish anyth...
|
|
|
|
|
plz help me to get codes for capturing agroup of system desktop using a single server
|
|
|
|
|
Hello Friends
I am loading a image Using TextureLoader then I am setting its BoundaryModes too.
But i want to do tiling of that Image,I am having UScale value and Vscale value.
IS this possible with TextureCoordinate?
Any Idea?
Thanks In Advance
Regards
Yogesh
|
|
|
|
|
Hello Friends
I am using TextureLoader to load the Image then I am setting TextureMode.
Now,I want to change the size of texture.I want to make that texture bigger on my current object.Is there any different function for TextureAttribute to do that?
Please Advice Me.
Any Ideas??
Thanks In Advance.
Regards
Yogesh
TextureLoader textureLoader = new TextureLoader(textureImage, TextureLoader.GENERATE_MIPMAP);
currentAppearance.setTexture(textureLoader.getTexture());
TextureAttributes texAtt = new TextureAttributes();
texAtt.setTextureMode(TextureAttributes.ALLOW_BLEND_COLOR_READ);
currentAppearance.setTextureAttributes(texAtt);
|
|
|
|
|
I got one function of textureAttribute that is SetTextureTransform.
but what should b the value for tt.
As I am having U-scale and V-scale values but dont know how to apply that on this texture.
Any Ideas?
transform3D tt = new Transform3D();
texAtt.setTextureTransform(transform);
|
|
|
|