SERWER:
package test1;
import java.io.*;
import java.io.FileOutputStream;
import java.net.*;
public class udp_server
{
public static void main(String args[])
{
DatagramSocket sock = null;
try
{
//1. creating a server socket, parameter is local port number
sock = new DatagramSocket(7777);
//buffer to receive incoming data
byte[] buffer = new byte[65536];
DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
//2. Wait for an incoming data
echo("Server socket created. Waiting for incoming data...");
//communication loop
while(true)
{
sock.receive(incoming);
byte[] data = incoming.getData();
String s = new String(data, 0, incoming.getLength());
//echo the details of incoming data - client ip : client port - client message
echo(incoming.getAddress().getHostAddress() + " : " + incoming.getPort() + " - " + s);
s = "OK : " + s;
DatagramPacket dp = new DatagramPacket(s.getBytes() , s.getBytes().length , incoming.getAddress() , incoming.getPort());
sock.send(dp);
File file = new File("C:/INNE/test.mp4");
FileOutputStream fos = new FileOutputStream(file);
DatagramPacket rpacket = new DatagramPacket(buffer, buffer.length);
while (true) {
sock.receive(rpacket);
fos.write(rpacket.getData(), 0, rpacket.getLength());
}
}
}
catch(IOException e)
{
System.err.println("IOException " + e);
}
}
//simple function to echo data to terminal
public static void echo(String msg)
{
System.out.println(msg);
}
}
KLIENT:
package test2;
import java.io.*;
import java.net.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
public class udp_client
{
public static void main(String args[])
{
DatagramSocket sock = null;
int port = 7777;
String s;
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
try
{
sock = new DatagramSocket();
InetAddress host = InetAddress.getByName("localhost");
while(true)
{
//take input and send the packet
echo("Enter message to send : ");
s = (String)cin.readLine();
byte[] b = s.getBytes();
DatagramPacket dp = new DatagramPacket(b , b.length , host , port);
sock.send(dp);
//now receive reply
//buffer to receive incoming data
byte[] buffer = new byte[65536];
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
sock.receive(reply);
byte[] data = reply.getData();
s = new String(data, 0, reply.getLength());
//echo the details of incoming data - client ip : client port - client message
// URL url = new URL("https://www.google.com/robots.txt");
// InputStream stream = url.openStream();
// BufferedReader reader = new BufferedReader(
// new InputStreamReader(stream));
// String line = reader.readLine();
// while (line !=null)
// {
// System.out.println(line);
// line = reader.readLine();
// }
// System.out.println("Done");
echo(reply.getAddress().getHostAddress() + " : " + reply.getPort() + " - " + s);
echo("dupa");
File file = new File("C:/INNE/test.mp4");
FileInputStream fis = new FileInputStream(file);
DatagramPacket pack;
int size = 0;
while (true) {
size = fis.read(buffer);
pack = new DatagramPacket(buffer, buffer.length);
sock.send(pack);
}
}
}
catch(IOException e)
{
System.err.println("IOException " + e);
}
}
//simple function to echo data to terminal
public static void echo(String msg)
{
System.out.println(msg);
}
}
Prosił bym o wskazanie co jest nie tak w moim kodzie. Powtarzam w programie chodzi o stream video z uzyciem UDP.
Wydaje mi sie ze bleblujac internet cos chociaz podobnego tworze, lecz na ta chwile jest taki blad:
w kliencie:
Exception in thread "main" java.lang.NullPointerException: null address || null buffer
at java.net.DualStackPlainDatagramSocketImpl.send(Unknown Source)
at java.net.DatagramSocket.send(Unknown Source)
at test2.udp_client.main(udp_client.java:79)
zas po wlaczeniu serwera jest:
"Server socket created. Waiting for incoming data..."
wiec chyba dziala..
Jeszcze raz prosze o wskazowki, poprawe i dziekuje za wszelka pomoc.