Video Streaming using VLCJ - java

Hi I have been using vlcj to stream videos. My server is able to stream the video using http. How can i create a client side program to capture the streaming video and dsiplay?
Server_main.java
public class Server_main {
public static InetAddress Server_Address;
public static int port;
public static int maxqueue=10;
public static int sleep;
public static void main(String args[]) throws Exception
{
Connection c=new Connection();
Server_Address=c.getAddress();
port=c.getPort();
ServerSocket server =new ServerSocket(port,maxqueue,Server_Address);
System.out.println("server started");
Socket client;
while(true)
{
client=server.accept();
NewThread newThread = new NewThread(client);
}
}
static class NewThread implements Runnable
{
Socket client;
Thread t;
NewThread(Socket client)
{
this.client=client;
t=new Thread(this);
t.start();
}
#Override
public void run() {
try
{
String path=Recieve_data(client);
//send_data(client,new File(path));
playmedia(path);
}
catch(Exception e)
{
}
}
private void send_data(Socket client, File file) throws IOException {
OutputStream os=client.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
System.out.println(file.getPath());
bw.write(file.getPath());
}
private String Recieve_data(Socket client) throws IOException {
InputStream is=client.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String path = br.readLine();
System.out.println(path);
return path;
}
}
public static void playmedia(String path)
{
String media = path;
String options = formatHttpStream("127.0.0.1", 5555);
System.out.println("Streaming '" + media + "' to '" + options + "'");
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(path);
HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
mediaPlayer.playMedia(media, options);
}
private static String formatHttpStream(String serverAddress, int serverPort) {
StringBuilder sb = new StringBuilder(60);
sb.append(":sout=#duplicate{dst=std{access=http,mux=ts,");
sb.append("dst=");
sb.append(serverAddress);
sb.append(':');
sb.append(serverPort);
sb.append("}}");
return sb.toString();
}
}
Can anyone explain how to do client side code?

Related

OutputStreamWriter and InputStreamReader for a Socket in Java, What am I doing wrong?

Using Sockets in java,
public class Server {
private static ServerSocket server;
private static int port = 9876;
static Socket p1 = null;
static Socket p2 = null;
public static void main(String args[]) throws IOException, ClassNotFoundException{
server = new ServerSocket(port);
Thread p1t = new Thread(new Runnable() {
#Override
public void run() {
try {
System.out.println("[Server] Waiting for connection");
p1 = server.accept();
System.out.println("[Server] Client connected");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(p1.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(p1.getInputStream()));
String msg = String.valueOf(in.readLine());
System.out.println(msg);
out.write(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
});
p1t.start();
}
}
and
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException, InterruptedException{
Socket socket = new Socket(InetAddress.getLocalHost().getHostName(), 9876);
if(socket.isConnected()) {
System.out.println("[Client] Connected");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
new Thread(new Runnable() {
#Override
public void run() {
while(true) {
try {
System.out.println(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
while(true) {
Scanner s = new Scanner(System.in);
out.write(s.nextLine());
}
}
}
}
The client is supposed to send a message to the server, and the server is supposed to relay that message back to the client; but either the BufferedReader for Client and Server are not reading anything that is sent, or the BufferedWriter for Client and Server is not sending anything.
I've also tried manually sending text using out.write("test"); in both classes.
What am I doing wrong in this situation?

String not going through PrintWriter

My problem is that for some reason, whenever I want to pipe a string to the server console, the server receives nothing. I've tried to track it down with debugging, but I'm not able to find the issue. I passed a PrintWriter through from MCSERVERPROZESS to the SERVER Runnable and the string I want to pipe through is not null. Here are the codes:
public class MCSERVERPROZESS extends Thread {
private SERVERGUI servergui = null;
private SERVER server = null;
private String serverPath = "/home/mint/mc_server";
private ProcessBuilder builder = new ProcessBuilder("java", "-Xmx1024M", "-Xms1024M", "-jar", "server.jar", "nogui");
public MCSERVERPROZESS(SERVER server, SERVERGUI servergui) throws IOException {
this.servergui = servergui;
this.server = server;
}
#Override
public void run() {
try {
builder.directory(new File(serverPath));
final Process pr = builder.start();
OutputStream stdin = pr.getOutputStream();
InputStream stderr = pr.getErrorStream();
InputStream stdout = pr.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(stdout));
this.server.setProcessOutputStream(new PrintWriter(stdin));
String serverOutputText;
do {
serverOutputText= in.readLine();
this.servergui.updateServerArea(serverOutputText+ "\n");
} while (serverOutputText.contains("Stopping server"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
public class SERVER implements Runnable {
private SERVERGUI servergui = null;
private int port = 10001;
private ServerSocket serverSocket = null;
private BufferedReader in = null;
private PrintWriter out = null;
private MCSERVERPROZESS mcserver = null;
//private Socket clientSocket = null;
private ArrayList<CLIENTPROZESS> clients = new ArrayList<>();
public SERVER(int Port, SERVERGUI server) throws IOException {
this.servergui = server;
this.port = port;
}
public void run() {
try {
mcserver = new MCSERVERPROZESS(this, this.servergui);
mcserver.start();
servergui.setServer(this);
startServer();
} catch (IOException e) {
e.printStackTrace();
}
}
public void startServer() throws IOException {
this.serverSocket = new ServerSocket(port);
while(true) {
waitForConnection();
}
}
public void waitForConnection() throws IOException {
Socket clientSocket = serverSocket.accept();
this.startConnection(clientSocket);
}
public synchronized void startConnection(Socket s) throws IOException {
new CLIENTPROZESS(s, this).start();
}
void setProcessOutputStream(PrintWriter pw) {
this.out = pw;
}
void sendTextToConsole(String s) throws IOException {
out.write(s);
System.out.println(s);
}
SERVER.sendTextToConsole is used for piping through String to console.

Send object via socket

I want to send x object over socket but when I run this code i got nothings.
it is stop at new ObjectInputStream(socket.getInputStream())
and don't do any thing else.
Server class:
public class Server {
private static final int PORT = 9001;
ServerSocket listener;
private Handler h[] = new Handler[5];
private int clientCount = 0;
public Server() throws Exception{
System.out.println("The server is running.");
listener = new ServerSocket(PORT);
run();
}
public void run(){
while (true) {
try {
addClient(listener.accept());
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void addClient(Socket socket) throws Exception{
h[clientCount] = new Handler(this, socket,clientCount);
h[clientCount].open();
clientCount++;
}
public static void main(String[] args) throws Exception {
Server s = new Server();
}
}
Handler class // Handle class:
public class Handler extends Thread {
private Server server;
private Socket socket;
private int ID = -1;
private ObjectInputStream obIn = null;
private ObjectOutputStream obOut = null;
public Handler(Server _server, Socket _socket, int i){
super();
server = _server;
socket = _socket;
ID = i;
}
public void open()
{
try {
obIn = new ObjectInputStream(socket.getInputStream());
obOut = new ObjectOutputStream(socket.getOutputStream());
x= ob.readObject();
} catch (Exception e) {
e.printStackTrace();
}
}
}
The client:
public class Client {
ObjectInputStream oin;
ObjectOutputStream oot;
private Socket socket = null;
public Client() {
String serverAddress = "127.0.0.1";
try {
socket = new Socket(serverAddress, 9001);
oin = new ObjectInputStream(socket.getInputStream());
oot = new ObjectOutputStream(socket.getOutputStream());
System.out.println("hello i am a client");
oot.writeObject(x);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Client client = new Client();
}
}
You must declare the ObjectOutputStream before you declare the ObjectInputStream.
When you create an ObjectInputStream, it waits for data from an ObjectOutputStream. It's waiting on that data (the header).
public ObjectInputStream(InputStream in) throws IOException {
verifySubclass();
bin = new BlockDataInputStream(in);
handles = new HandleTable(10);
vlist = new ValidationList();
enableOverride = false;
readStreamHeader(); //this is whats causing it to block
bin.setBlockDataMode(true);
}
protected void readStreamHeader() throws IOException, StreamCorruptedException {
short s0 = bin.readShort();
short s1 = bin.readShort();
if (s0 != STREAM_MAGIC || s1 != STREAM_VERSION) {
throw new StreamCorruptedException(String.format("invalid stream header: %04X%04X", s0, s1));
}
}
If you declare ObjectOutputStream first, it sends the data, which isn't blocking

send from client to server and from server to client in java

I develop code to send form client to server then the server has to send what it receives from client again to client. but in my code the server only receives the message but does not resend it again. I do not know what is the problem
This is my client code
public class Client implements Runnable{
private static Socket s = null;
//private static BufferedOutputStream fromUser = null;
private static DataInputStream fromServer = null;
private static InputStreamReader input = new InputStreamReader(System.in);
private static InputStreamReader inputstreamreader = null;
private static BufferedReader bufferedreader = null;
private static DataOutputStream fromUser = null;
private static int chara = 0;
private static String line = null;
static int port = 0;
static String host = null;
//connect to server
#Override
public void run() {
try {
inputstreamreader = new InputStreamReader(s.getInputStream());
bufferedreader = new BufferedReader(inputstreamreader);
//charr = fromClient.read();
while(true){
if ((line = bufferedreader.readLine()) != null){
System.out.println(line);}
if(line.equals(-1)){
break;
}
}//end while
}catch(NullPointerException e) {
// do something other
}
catch (IOException e) {
System.out.println(e);
}
}//end the run
//constructor with two arguments
public Client(String host, int port){
try{
s = new Socket (host,port);
}
catch(Exception e){}
}
//send message to from Client to Server
public static void sendToServer(){
try{
fromUser =new DataOutputStream (new BufferedOutputStream(s.getOutputStream()));
chara =input.read();
while(true){
if (chara == '~'){
break;}
fromUser.write(chara);
fromUser.flush();
chara =input.read();
}//end while
}
catch (IOException e) {
System.out.println(e);
}
}//end send message
I tried to use thread to receive message but also does not work. I tried without thread it does not work too.
public static void main(String [] args){
host = args[0];
port = Integer.parseInt(args[1]);
System.out.println("Start connection .....");
Client client = new Client(host,port);
Thread thread = new Thread(client);
thread.start();
//connect(host,port);
client.sendToServer();
client.close();
}//end main
and this is my server code
public class Server extends Thread{
private static Socket s = null;
private static ServerSocket ss = null;
private static DataOutputStream fromUser = null;
private static DataInputStream fromClient = null;
private static InputStreamReader input = new InputStreamReader(System.in);
private static InputStreamReader inputstreamreader = null;
private static BufferedReader bufferedreader = null;
static String line=null;
static String sendC;
private static int chara = 0;
static int port = 0;
//connect to server
static void connect(int port){
try{
ss = new ServerSocket (port);
System.out.println("Listening on port "+port+"...");
s = ss.accept();
System.out.println("Has been connected .....");
}
catch (IOException e) {
System.out.println(e);
}
}//end of the connection method
//send message to from Client to Server
public static void sendToClient(String text){
try{
fromUser =new DataOutputStream (new BufferedOutputStream(s.getOutputStream()));
sendC =text;
fromUser.write(sendC.getBytes());
fromUser.flush();
}
catch (IOException e) {
System.out.println(e);
}
}//end send message
public static void receiveFromClient(){
try {
inputstreamreader = new InputStreamReader(s.getInputStream());
bufferedreader = new BufferedReader(inputstreamreader);
//charr = fromClient.read();
while(true){
if ((line = bufferedreader.readLine()) != null){
System.out.println(line);
sendToClient(line);}
if(line.equals(-1)){
break;
}
}//end while
}catch(NullPointerException e) {
// do something other
}
catch (IOException e) {
System.out.println(e);
}
}//end send message
this is main method for server
public static void main(String [] args){
port = Integer.parseInt(args[0]);
System.out.println("Start connection .....");
connect(port);
receiveFromClient();
//sendToClient();
close();
}//end main
I do not have alot of knowledge in java especially about the socket
thanks for your help
a simply search for a java echo server shows this
public class EchoServer {
public static void main(String[] args) throws Exception {
// create socket
int port = 4444;
ServerSocket serverSocket = new ServerSocket(port);
System.err.println("Started server on port " + port);
// repeatedly wait for connections, and process
while (true) {
// a "blocking" call which waits until a connection is requested
Socket clientSocket = serverSocket.accept();
System.err.println("Accepted connection from client");
// open up IO streams
In in = new In (clientSocket);
Out out = new Out(clientSocket);
// waits for data and reads it in until connection dies
// readLine() blocks until the server receives a new line from client
String s;
while ((s = in.readLine()) != null) {
out.println(s);
}
// close IO streams, then socket
System.err.println("Closing connection with client");
out.close();
in.close();
clientSocket.close();
}
}
}
see http://introcs.cs.princeton.edu/java/84network/EchoServer.java.html

Java - i am making a server which sends client latest updates

So i have this class, this class has the string i want in my other file(String Message)
Which picks up the message from the server. I am not sure how to get the string into another package and class. any help would be amazing
public class Client
{
private static Socket socket;
public static void main(String args[])
{
try
{
String host = "localhost";
int port = 43594;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
//String number = "2";
String number = ClientSettings.ClientSettings.ClientVersion;
String sendMessage = number + "\n";
bw.write(sendMessage);
bw.flush();
System.out.println(""+sendMessage);
//Get the return message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine(); //this is the string i need to get...
System.out.println("" +message);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Define in the class that need to use the "message" a private String (with getter/setter methods)
public class UseMessage{
private String message;
private static UseMessage instance;
private UseMessage(){
}
public static UseMessage getInstance(){
if(instance==null){
instance = new UseMessage();
}
return instance;
}
public String getMessage(){
return message;
}
public String setMessage(String message){
this.message = message;
}
}
Then in class Client:
UseMessage.getInstance().setMessage(br.readLine());
If you need in another class:
String message = UseMessage.getInstance().getMessage();
Thake a look here

Categories