day 020 udp
TOMCAT
8080默认端口
启动start.bat,默认会找到webapps中的文件,访问TOOT中的index.html默认
client 和server
客户端和服务端
客户端创建Socket new Socket(InetAddress,port)
连接上之后创建输出流 socket.getOutoutStream
os.write(bytes) 字节输出数据
服务端创建服务
ServerSocket (port)指定端口
连接上后监听获取socket = serverSocket.accpt()
获取输入流,读取流中数据 ,outputstram输出流写出
udp双向的不存在客户端服务端
DataGramPackt 发送包
DataGramSocket 接收包
-
UDPreceive
-
package com.udp; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; /** * @ Author :wwwzhqwww * @ Date :Created in 17:22 2021/1/24 * @ Description:接收udp * @ Modified By: * @Version: $version$ */ public class UdpReceive { public static void main(String[] args) { DatagramSocket socket = null; try { socket = new DatagramSocket(9999); while (true){ byte[] bytes = new byte[600]; DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);//此处大小需要注意,当发送的行字符超出bytes的长度,,内容将丢失 socket.receive(packet); System.out.println(packet.getData().length+"******packet.getdata.length"); System.out.println(packet.getLength()+"******packet.getlength"); String str = new String(packet.getData(),0,packet.getLength()); System.out.println(str); if ("bye".equals(str)){ break; } } } catch ( Exception e) { e.printStackTrace(); }finally { if (socket!=null){ socket.close(); } } // DatagramPacket packet; // DatagramSocket socket = null; // int port = 6666; // try { // socket = new DatagramSocket(port); // // while (true){ // byte[] bytes = new byte[600]; // packet = new DatagramPacket(bytes,0,bytes.length); // socket.receive(packet); // byte[] data = packet.getData(); // String msg = new String(data,0, data.length); // System.out.println(msg); // if (msg.equals("bye")){ // break; // } // } // // } catch (Exception e) { // e.printStackTrace(); // }finally { // if (socket!=null){ // socket.close(); // } // // } } }
-
udp send
-
package com.udp; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /** * @ Author :wwwzhqwww * @ Date :Created in 17:22 2021/1/24 * @ Description:发送udp * @ Modified By: * @Version: $version$ */ public class UdpSend { public static void main(String[] args) { DatagramSocket socket =null; try { socket = new DatagramSocket(8888); while (true){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String msg = br.readLine(); byte[] bytes = msg.getBytes(); DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,InetAddress.getByName("localhost"),9999); socket.send(packet); if ("bye".equals(msg)){ break; } } } catch (Exception e) { e.printStackTrace(); }finally { if (socket!=null){ socket.close(); } } // DatagramSocket socket = null; // DatagramPacket packet; // InetAddress ia ; // int port = 8888; // String msg; // // { // try { // ia= InetAddress.getByName("127.0.0.1"); // socket = new DatagramSocket(port); // while (true) { // BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // msg = br.readLine(); // byte[] datas = msg.getBytes(); // packet = new DatagramPacket(datas,0,datas.length,ia,6666); // socket.send(packet); // if (msg.equals("bye")){ // br.close(); // break; // } // } // // } catch ( Exception e) { // e.printStackTrace(); // }finally { // socket.close(); // } // } } }