hi everyone,

i'm kinda new to java but am eager to learn the networking aspects. i have written code for both client and server to send and receive an image .jpg less than 20KB on the same PC. but now i'd like to send an mp3 file using the same code. how do i do this, please anyone? here is the code for the image transfer.

Client

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;

class Client1 extends JFrame implements ActionListener {
JLabel t = new JLabel("Text to send over socket:");
JButton b = new JButton("Receive");
JTextField tf = new JTextField(20);
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
DataInputStream in1 = null;

Client1(){
b.addActionListener(this);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setTitle("Client1");
this.setLayout(new FlowLayout());
this.add(t);
this.add(tf);
this.add(b);
this.setSize(200,200);
this.setVisible(true);
}

public void actionPerformed(ActionEvent event){
if(event.getSource() == b){
String text = tf.getText();
out.println(text);
tf.setText("");
try {
// String line = in.readLine();
// tf.setText(line);
OutputStream f2 = new FileOutputStream("C:\\Users\\erieyovwe.umusu\\Desk top\\2.mp3");
byte[] b = new byte[1];
while (in1.available()>0) {
in1.read(b);
f2.write(b);
}
f2.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}

public void listenSocket() throws UnknownHostException, IOException{
socket = new Socket("127.0.0.1", 5000);
out = new PrintWriter(socket.getOutputStream(), true);
// in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
in1 = new DataInputStream(socket.getInputStream());
}

public static void main(String[] args) throws UnknownHostException, IOException{
Client1 f = new Client1();
f.listenSocket();
}
}



Server


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.io.*;
import java.net.*;

class Server1 extends JFrame implements ActionListener {
JLabel l = new JLabel("Text received over socket:");
JTextArea t = new JTextArea();
JButton bl= new JButton("load");
ServerSocket server = null;
Socket client = null;
BufferedReader in = null;
PrintWriter out = null;
DataOutputStream out1 = null;
String line;

Server1() {
this.setTitle("Server1");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
this.add(l);
this.add(t);
this.add(bl);
bl.addActionListener(this);
this.setVisible(true);
this.setSize(200,200); // to fit all the elements
}

public void listenSocket() throws IOException{
server = new ServerSocket(5000);
client = server.accept();
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
out1 = new DataOutputStream(client.getOutputStream());

while(true){
try{
line = in.readLine();
t.setText(line);
out.println("response");
} catch (IOException e) {
System.exit(-1);
}
}
}

protected void finalize() throws IOException{
in.close();
out.close();
server.close();
}

public static void main(String[] args) throws IOException{
Server1 f = new Server1();
f.listenSocket();
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==bl) {
l.setText("pressed");
File fl = new File("C:\\Users\\erieyovwe.umusu\\Desktop\\Don't Trust Me.mp3");
InputStream is = null;
try {
is = new FileInputStream(fl);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
byte[] data = new byte[(int)fl.length()];
long offset = 0;
byte i1;
try {
is.read(data);
out1.write(data);

} catch (IOException e1) {
e1.printStackTrace();
}
}
}

}

thanks.