gistfile1.txt /** * 往指定ip、port转发数据 * @author Jeek * */public class Server {private static DatagramSocket socket;public static void udpServer(String clientIp , int clientPort) {try {System.out.println("======推流开始=====
/** * 往指定ip、port转发数据 * @author Jeek * */ public class Server { private static DatagramSocket socket; public static void udpServer(String clientIp , int clientPort) { try { System.out.println("======推流开始======"); socket = new DatagramSocket(6666); //缓存 socket.setReceiveBufferSize(2*1024*1024); socket.setSendBufferSize(2*1024*1024); while(true){ byte[] data = new byte[1500]; DatagramPacket packet= new DatagramPacket(data,data.length); socket.receive(packet); //System.out.println("客户端说:"+Arrays.toString(packet.getData())); //转发 packet= new DatagramPacket(packet.getData(),packet.getLength(),InetAddress.getByName(clientIp),clientPort); socket.send(packet); } } catch (Exception e) { e.printStackTrace(); }finally{ socket.close(); } } @SuppressWarnings("static-access") public static void main(String[] args) { Server s = new Server(); //System.out.println("请求rtpPort:"+Integer.parseInt(rtpPort.trim())); s.udpServer("192.168.0.107",6666); } } //udp往指定循环端口发送数据 private DatagramSocket socket; private Socket socket1; private DatagramPacket packet; InputStream in = null; try { //1 socket = new DatagramSocket(); //2 in = new FileInputStream(new File("D:/java/2017022012033239.ts")); byte[] buf = new byte[188]; int len; while((len=in.read(buf))!=-1) { //paramIoSession.write(buf); packet= new DatagramPacket(buf,len,InetAddress.getByName("localhost"),port-1); socket.send(packet); } } catch (Exception e) { System.out.println("ts异常"+e); // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { in.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } ---------------------------UDP服务端 package stream; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /** * UDP通讯的服务端 * @author Administrator * */ public class Server { public static void main(String[] args) { try { /* * 接收数据的大致步骤: * 1:创建DatagramSocket * 2:创建一个接收数据用的包 * 包创建时要设置一个字节数组, * 接收的数据就存放在这个数组中. * 所以这个数组应当足够大. * 3:通过DatagramSocket接收数据 * 当接收数据后,接收包会有一些变化: * 1:包中有远端计算机发送过来的数据 * 2:包也记录了数据从哪里来(远端计算 * 机的地址信息) * 4:获取包中的数据(字节数组中保存) */ /* * TCP协议与UDP协议的端口是不冲突的 */ //1 DatagramSocket socket = new DatagramSocket(8088); /*socket.setReceiveBufferSize(2*1024*1024); socket.setSendBufferSize(2*1024*1024);*/ //2 byte[] data = new byte[100]; DatagramPacket packet = new DatagramPacket( data,data.length ); /* * 3 * 该方法是一个阻塞方法,直到远程计算机 * 发送数据过来为止,该方法才会解除阻塞 * 并将数据等信息设置到接收用的包中 */ socket.receive(packet); /* * 4 下面的方法可以获取包中的数组 * 这个数组就是上面定义的data */ // packet.getData(); /* * packet.getLength方法可以获取本次 * 接收的数据的长度. */ String message = new String( data,0,packet.getLength(),"UTF-8" ); System.out.println("客户端说:"+message); /* * 服务端回复客户端 */ message = "你好客户端!"; data = message.getBytes("UTF-8"); /* * 3 通过接收包得到远程计算机地址信息 InetAddress address = packet.getAddress(); int port = packet.getPort(); //4 packet = new DatagramPacket( data, data.length, address, port ); //5 socket.send(packet);*/ //循环发送数据给客户端 while(true){ InetAddress address= packet.getAddress(); int port = packet.getPort(); packet = new DatagramPacket( data, data.length, address, port ); Thread.sleep(3000); socket.send(packet); } } catch (Exception e) { e.printStackTrace(); } } } ---------------------------Udp客户端 package stream; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Arrays; /** * UDP通讯的客户端 * @author Administrator * */ public class Client { public static void main(String[] args) { try { /* * UDP通讯大致过程 * 1:创建DatagramSocket * 2:准备要发送的数据 * 3:准备发送的目标地址信息(远端 * 计算机地址信息) * 4:打包 * 创建DatagramPacket并且将 * 数据与地址信息设置进去 * 5:通过DatagramSocket将包发送 * 至远端 * 若需要再次发送数据,重复2-5 * */ //1 DatagramSocket socket = new DatagramSocket(); //2 String message = "你好服务端!"; byte[] data = message.getBytes("UTF-8"); //3 InetAddress address = InetAddress.getByName("localhost"); int port = 8088; //4 DatagramPacket packet = new DatagramPacket( data, data.length, address, port ); //5 socket.send(packet); /* * 接收服务端发送回来的消息 */ data = new byte[188]; packet = new DatagramPacket( data,data.length ); /* * 3 * 该方法是一个阻塞方法,直到远程计算机 * 发送数据过来为止,该方法才会解除阻塞 * 并将数据等信息设置到接收用的包中 socket.receive(packet); * 4 下面的方法可以获取包中的数组 * 这个数组就是上面定义的data message = new String( data,0,packet.getLength(),"UTF-8" ); System.out.println("服务端说:"+message);*/ //循环接收服务端发送的数据 while(true){ socket.receive(packet); System.out.println("服务端说:"+Arrays.toString(packet.getData())); } } catch (Exception e) { e.printStackTrace(); } } }