Hi I have to write a simple client server application using TCP sockets to implement a movie server application (for MPEG files) whereby clients can register to a main system (server) and then watch movies.

The problem is I don't know how to send video files over tcp, can someone please tell me what functions I can use to break the video file at the server and send it to the client or is there any tutorial about how to send audio, video over tcp sockets? Please I am really stuck. Thank You!

What does the client expect to receive when you send a video file?
I wouldn't think sending a video file would be any different from sending any type of file. The server reads and sends the bytes of the file to the client.

For the time being I am just trying to have a single client connect to the server and the server send the movie file as an array of bytes. The client then saves it.

This is what I wrote:

Server:

import java.io.*;
import java.net.*;
public class Server {
	public static void main(String [] args)
	{
		ServerSocket MyServer = null;
		
		try{
			MyServer = new ServerSocket(6517);
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		Socket clientSocket = null;
		try{
			clientSocket = MyServer.accept();
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		File myFile = new File("Bear.wmv");//create an instance of file class
		byte [] buf = new byte [(int)myFile.length()];//create an array of same length as the file
		FileInputStream fis = null;
		OutputStream out = null;
		try {
			fis = new FileInputStream(myFile);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}//FileInputStream is used to read streams of raw bytes
		BufferedInputStream bis = new BufferedInputStream(fis);
		try {
			bis.read(buf, 0, buf.length);//reads bytes from input stream into array buf
			out = clientSocket.getOutputStream();//output stream of bytes
			out.write(buf,0,buf.length);//writes length bytes from buf to out
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	
				
	}

}

Client:

import java.io.*;
import java.net.*;
public class Client {

	public static void main(String [] args)
	{
		Socket MyClient = null;
		try{
			 MyClient = new Socket("localhost",6517);
			}
		catch(IOException e)
		{
			System.out.println(e);
			
		}
		int size = 4046848 ;
		byte[] buf = new byte[size];
		InputStream in = null;
		int num_bytes_read = 0;
		int current = 0;
		try {
			 in = MyClient.getInputStream();//an input stream of bytes
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		FileOutputStream fos = null;
		try {
			 fos = new FileOutputStream("BearCopy.wmv");//output stream to write data to file
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		
		try {
			num_bytes_read = in.read(buf, 0, buf.length);
			System.out.println(num_bytes_read);
			current = num_bytes_read;
			do{
				num_bytes_read = in.read(buf, current, (buf.length-current));
				if(num_bytes_read >= 0) current += num_bytes_read;
				}while(num_bytes_read > -1);
			bos.write(buf, 0, current);
			
			in.close();
			bos.close();
			MyClient.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
	
		}

}

The result is:

-1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at java.io.BufferedOutputStream.write(Unknown Source)
at Client.main(Client.java:45)

in.read(buf, current, (buf.length-current)) at line 38 is returning -1. How do I correct this or is what I am doing totally wrong?

Okie I misplaced the location of my movie file. It works but now please how can I get to play the file directly instead of saving it at the client. I have no idea of how to do this. Some help will be much appreciated. Thanks!

how can I get to play the file directly

What class "plays" the file? Does that class have an inputstream that you can connect to the bytes that are being read by your program?

I have decided to save the copy of the movie in the temp folder,play it using JMF player and then delete it.

This is the new version of the client(in this case I sent an MPEG from the server:

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;
import javax.media.*;

public class Client {

	public static void main(String [] args)
	{
		Socket MyClient = null;
		try{
			 MyClient = new Socket("localhost",2217);
			}
		catch(IOException e)
		{
			System.out.println(e);
			
		}
		int size = 589824 ;
		byte[] buf = new byte[size];
		InputStream in = null;
		int num_bytes_read = 0;
		int current = 0;
		try {
			 in = MyClient.getInputStream();//an input stream of bytes
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		FileOutputStream fos = null;
		try {
			 fos = new FileOutputStream("movie1_copy.mpg");//output stream to write data to file
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		try {
			num_bytes_read = in.read(buf, 0, buf.length);
			System.out.println(num_bytes_read);
			current = num_bytes_read;
			do{
				num_bytes_read = in.read(buf, current, (buf.length-current));
				if(num_bytes_read >= 0) current += num_bytes_read;
				}while(num_bytes_read > -1);
			bos.flush();
			bos.write(buf, 0, current);
			in.close();
			bos.close();
			fos.close();
			MyClient.close();
										    				
		}
		catch(IOException e)
		{
			System.out.println(e);
		}		
		play();
	
}

	

public static void play(){
	JFrame frame = new JFrame("Movie"); //Creates a new invisible frame with specified title 
	URL url = null;
	try {
		url = new URL("file:///C:/Users/User/workspace2/Assign/movie1_copy.mpg");
	} catch (MalformedURLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}//pointer to a resource
	
	//Manager is responsible for creating player instance
	
	Player player = null;
	try {
		player = Manager.createRealizedPlayer(url);
	} catch (CannotRealizeException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}//create player for the datasource a url in this case
 catch (NoPlayerException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
    Component visual = player.getVisualComponent();//get visual component of the media file
    Component control = player.getControlPanelComponent();//get component that provides playback and controls
	//add video and control component only if they exist that is depending on the media
	//by checking values of video and controls respectively
    if (visual != null)
    	frame.getContentPane().add(visual, "Center");
    if(control!=null)
    	frame.getContentPane().add(control, "South");
    
    frame.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    	File myFile = new File("movie1_copy.mpg");
    	myFile.delete();
        	
    }
    });
    frame.pack();
    frame.setVisible(true);
    player.start();//starts the player
   
}}

Although it works and plays the movie I am having one problem; I can't delete the file after playing it. I want to delete the file after closing the window. How can I fix this?

p.s My full application is much longer with multithreading at the server and other security options at the client etc but the new limited client version I posted here works fine with the sever in post 3(for an mpeg file in this case). It just I can't delete the copy on the client after I played it in JMF player. Thank You!

I can't delete the file

Are you sure the File class refers to the file to be deleted?
Print out the path of myFile to be sure.

hi dark Byte, can see u using a class Player. wat's there? (the codes i mean)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.